vba - Word Macro - Log changes made by a find and replace -


i have below code search through word document replacing ids finds masked version of number using regex (e.g. 412345678900 becomes 4123####8900). each document have multiple ids in it. ids scattered throughout document text , not in tables (so excel not option).

i want able write each of replaced versions of text found out log file file path , file name.

sub auto_masking()  'start @ beginning. it's place start.     selection.homekey unit:=wdstory      selection.find  ' locate , mask 12 digit ids         .text = "<([4][0-9]{3})([0-9]{4})([0-9]{4})>"         .replacement.text = "\1####\3"         .forward = true         .wrap = wdfindcontinue         .format = false         .matchcase = false         .matchwholeword = false         .matchallwordforms = false         .matchsoundslike = false         .matchwildcards = true     end     selection.find.execute replace:=wdreplaceall  'put user @ beginning of document     selection.homekey unit:=wdstory end sub 

how can write/append each masked number log file? have log file show list of ids masked , file in, each line in log file should this:

filepath\filename ; maskedid

with line each id number masked (with 1 file potentially containing multiple ids). e.g.:

c:\temp\test.docx;4123####8900 c:\temp\test.docx;4241####7629 c:\location\another.docx;4379####8478 

i have horrible feeling going impossible based on trying value want in log file display in msgbox. after days of experimenting, i'm out of ideas.

i'm thinking find , find/replace may have used in larger loop, 1 replace, , 1 find replaced before moving on. maybe based on selection.find.found = true

  • selection.find.text display regex
  • selection.text display first character of id number string, no more
  • selection.find.replacement.text display string appears in section, without replacing /1 , /3 values found

not 10 minutes after giving up, worked out.

the code solve issue , complete above task, logging of each masked id, follows:

sub mask_card_numbers() '    dim counter long  ' next section prepares log writing     dim log1 object     set fso = createobject("scripting.filesystemobject")     ' forreading = 1, forwriting = 2, forappending = 8     set logids = fso.opentextfile("c:\logdir\ids_masked_with_word.txt", 8, true)  ' filename , path log file     filename = activedocument.path & "\" & activedocument.name   ' mask ids ####################################################     selection.homekey unit:=wdstory     selection.find.clearformatting     selection.find.replacement.clearformatting ' first pass collects single digit text search artificially increase counter reduce 1 in advance     counter = counter - 1              selection.find             .text = "<([4][0-9]{3})([0-9]{4})([0-9]{4})>"             .replacement.text = "\1xxxxxxxx\3"             .forward = true             .wrap = wdfindcontinue             .format = false             .matchcase = false             .matchwholeword = false             .matchallwordforms = false             .matchsoundslike = false             .matchwildcards = true             counter = counter + 1         end ' keeping selected text after replacement, masked         foundid = selection.text  ' write masked id logfile         if len(foundid) > 7  ' greater 1 work             logids.writeline filename & ";" & foundid          end if         selection.find.execute replace:=wdreplaceone     loop while selection.find.found <> false  ' done masking ids ###########################################  end sub 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -