Visual Basic edit Word documents in bulk - vba

I have hundreds of docx-documents which I would like to edit in bulk with a visual basic macro. They all share an id on the first line which looks like this:
9-ZKB-S
Or
12-JK-17
I would like to remove the '-' from the id so it will become like this:
9ZKBS
Or
12JK17
Then somewhere in the document I have a word followed by a number like this:
Productionnumber. 42-563-12
And I also would like to remove the minus character:
Productionnumber. 4256312
I've found a visual basic script which enables me to select a folder containing word-documents and to perform a search and replace. But I don't know how to do the specific things I've mentioned such as:
In each document, remove the - and the space between characters on the first line
In each document, remove the - and the space between characters after Productionnumber.
Sub CommandButton1_Click()
Dim MyDialog As FileDialog, GetStr(1 To 500) As String '100 files is the maximum applying this code
On Error Resume Next
Set MyDialog = Application.FileDialog(msoFileDialogFilePicker)
With MyDialog
.Filters.Clear
.Filters.Add "All WORD File ", "*.docx", 1
.AllowMultiSelect = True
i = 1
If .Show = -1 Then
For Each stiSelectedItem In .SelectedItems
GetStr(i) = stiSelectedItem
i = i + 1
Next
i = i - 1
End If
Application.ScreenUpdating = False
For j = 1 To i Step 1
Set Doc = Documents.Open(FileName:=GetStr(j), Visible:=True)
Windows(GetStr(j)).Activate
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "search" 'Find What
.Replacement.Text = "find" 'Replace With
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Application.Run macroname:="NEWMACROS"
ActiveDocument.Save
ActiveWindow.Close
Next
Application.ScreenUpdating = True
End With
MsgBox "operation end, please view", vbInformation
End Sub

Look into Using wildcards and test the pattern in word first. For example (not tested):
With Doc.Range.Find
.MatchWildcards = True
.Text = "<(?*)>\-<(?*)>\-<(?*)>"
.Replacement.Text = "\1\2\3" 'Replace With
' ... the rest of the options
End With

Related

Word Macro to select paragraph with specific words and copy to new document

I am trying to create a Word macro that will:
Search for a specific word (i.e. "see")
Select the entire paragraph where that word appears
Make the whole paragraph a different style (i.e. make it all red text)
Do the same thing with a second word (i.e. "blacklist")
Select that whole paragraph and apply a different style (i.e. again, make the paragraph red text)
Copy all paragraphs with the red text style and paste them in to a new word document
Unfortunately, I'm no VBA expert and I'm trying to cobble things together from what I can find online. I have found a great example that will select to the start of the paragraph, but I can't seem to figure out how to select the entire paragraph. Any help is appreciated!
** Sorry - here is the code I currently have. It will find all instances of the word "see" and selects to the start of the paragraph, then changes the color to red... but that's as far as I've gotten, as I am stuck on trying to figure out how to get it to select to the end of the paragraph.
Sub TestOne()
'
' TestOne Macro
'
'
If MsgBox(Prompt:="Would you like to update selected paragraph styles?", Buttons:=vbYesNo + vbQuestion, _
Title:="Format MD Report") = vbNo Then
Exit Sub
End If
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "see"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False
End With
Do While .Find.Execute
i = i + 1
.Start = .Paragraphs.First.Range.Start
.Font.Color = wdColorRed
.Start = .Paragraphs.First.Range.End
Loop
End With
Application.ScreenUpdating = True
MsgBox i & " instances processed."
End Sub
For example, without needing to create a second document:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, StrFnd As String
StrFnd = "see|blacklist"
With ActiveDocument.Range
.Font.Hidden = True
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Text = "^&"
.Font.Hidden = True
.Replacement.Font.Hidden = False
.Format = True
.Forward = True
.MatchWildcards = True
.Wrap = wdFindContinue
For i = 0 To UBound(Split(StrFnd, "|"))
.Text = "[!^13]#" & Split(StrFnd, "|")(i) & "*^13"
.Execute Replace:=wdReplaceAll
Next
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
End With
Application.ScreenUpdating = True
End Sub
You could, of course, add a line of code before the final 'End With' to save the document with a new name.
To select the entire paragraph, following the line
.Start = .Paragraphs.First.Range.Start
add
.End = .Paragraphs.First.Range.End
... then to match only whole words, after
.MatchWildcards = False
add
.MatchWholeWord = True
And to run the code for multiple words you should add a parameter to your Sub eg
Sub TestOne(theWord As String)
then replace
.Text = "see"
with
.Text = theWord
And to run your code for each required word, add a Sub such as
Sub RunMe()
TestOne "see"
TestOne "blacklist"
End Sub
... optionally, move your MsgBoxes into RunMe()

VBA Word: Insert text before found text in word document

What I want to do is something simple (maybe). I have written a find method to loop around the document. I have find value - ']' and insert value - '[('. In between those two there can be any kind of number most likely from 1 - 100.
My current code is this:
Sub FindBracket()
Dim FindValue As String
Dim ReplaceValue As String
Dim oRange As Range
Set oRange = ActiveDocument.Range
FindValue = "]"
InsertValue = "(["
With oRange.Find
.Text = FindValue
.MatchWholeWord = False
Do While .Execute = True
If .Found Then
oRange.InsertBefore (InsertValue)
End If
oRange.Start = oRange.End
oRange.End = ActiveDocument.Range.End
Loop
End With
End Sub
The find method works good, but I need to modify .InsertBefore method.
Example
I would need to transform ' Dummy text 1] ' to this ' Dummy text [(1] ' but from the code I get ' Dummy text 1[(] ', I cannot seem to figure out how to do that, and I have only previous experience with Excel VBA only.
Would appreciate any help on this, than you.
Try this:
Sub FindBracket()
Dim FindValue As String
Dim ReplaceValue As String
Dim oRange As Range
Set oRange = ActiveDocument.Content
FindValue = "[0-9]#\]"
InsertValue = "[(^&" ' ^& - found number
With oRange.Find
.Text = FindValue
.Replacement.Text = InsertValue
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True ' enabled regular expressions
.Execute Replace:=wdReplaceAll
End With
End Sub
Before:
Dummy text 1234]
Dummy text 4321]
Dummy text 0]
Dummy text 1]
After:
Dummy text [(1234]
Dummy text [(4321]
Dummy text [(0]
Dummy text [(1]
All you need is:
Sub FindBracket()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[0-9]#\]"
.Replacement.Text = "[(^&"
.Forward = True
.Format = False
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
Indeed, you don't even need a macro - it could all be done through the GUI using a wildcard Find/Replace with the same F/R expressions.

How to break clipboard into multiple strings in Microsoft Word Using a VBA Macro?

I'm trying to replace multiple "placeholders" in a single word document by breaking the text in my clipboard into various string.
Sample clipboard text would be something like this:
Placeholder1=
Test1
Placeholder2=
First sentence.
Second Sentence.
Third Sentence.
Placeholder3=
2044 to 2045
Placeholder4=
five
So far, I can take the text my clipboard and paste it to replace a single placeholder. I can also insert the date.
Here's what I have so far:
Sub FillPlaceHolder()
'Prints a new label in bottom left of sticker sheet based on clipboard data
'To use the clipboard you need a reference to the following library
'Go to Tools > References and select Microsoft Forms Object Library
'If it's not visible, click browse and find FM20.dll in your system32 folder
Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
'Set error handling, will skip the code if the clipboard is empty
On Error GoTo Error
'Set variable for clipboard string
Dim myString As String
Dim myDate As Date
'Get data object from clipboard
DataObj.GetFromClipboard
'Set mystring to the first text in the clipboard
myString = DataObj.GetText(1)
myString = ClearFormatting
'Open the Word document
Documents.Open FileName:=GetFolder() & "Auden_perm_template.doc"
'Replaces the PlaceHolder text
With Selection.Find
.Text = "PLACEHOLDER2"
.Replacement.ClearFormatting
.Replacement.Text = myString
.Execute
End With
Selection.Paste
TodaysDate2
InsertDate
TodaysDate
InsertDate
'
'BELOW TO ADD PRINT
' Application.OnTime When:=Now + TimeValue("00:00:10"), Name:="Print_Label"
'Process this error for empty clipboards
Error:
If Err <> 0 Then MsgBox "Data on clipboard is empty"
End Sub
Sub TodaysDate2()
'
' Macro3Date Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "TODAYSDATE2"
.Replacement.Text = "02/25/19"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Find.Execute
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "TODAYSDATE2"
.Replacement.Text = "02/25/19"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
End Sub
Sub InsertDate()
'
' Macro3 Macro
'
'
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldDate
End Sub
Sub TodaysDate()
'
' Macro3Date Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "TODAYSDATE"
.Replacement.Text = "02/25/19"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Find.Execute
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "TODAYSDATE2"
.Replacement.Text = "02/25/19"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
End Sub
I'm struggling to figure out how to break the clipboard into multiple strings. What do you recommend?
You can break the string into an array using Split
For example:
myString = split(DataObj.GetText(1),vblf)
Change the Dim for myString from String to Variant
Then you can loop through the array with something like:
For X = lbound(myString) to ubound(myString)
If myString(X) = "PLACEHOLDER1" then
'Do Something when placeholder1 found
ElseIf myString(X) = "PLACEHOLDER2" then
'Do Something when placeholder2 found
ElseIf myString(X) = "PLACEHOLDER3" then
'Do Something when placeholder3 found
End IF
next
You will need to Dim X as a Long
You can set up a couple of variables to set the start and end of each placeholder then you can cycle through those parts joining each element back together with a vblf as the delimeter in order to create what you want.

How to add table captions in MS Word using macros

I am trying to amend captions to 100 existing tables in MS Word. To avoid this tedious process I was hoping to use the built-in VB macro functionality.
If my table is inside the contents of the document in the section of:
Intro
1.1 Goals
I want the table caption to be amended with "Intro - Goals". If the table caption is already 'Table 1-1' I want it to read:
Table 1-1 Intro Goals
after the macro runs, is that possible? How?
The script below is a batch process that does a find/replace routine on all the Word files in a folder.
Sub FindReplaceAll()
Dim MyDialog As FileDialog, GetStr(1 To 100) As String
'100 files is the maximum applying this code
On Error Resume Next
Set MyDialog = Application.FileDialog(msoFileDialogFilePicker)
With MyDialog
.Filters.Clear
.AllowMultiSelect = True
i = 1
If .Show = -1 Then
For Each stiSelectedItem In .SelectedItems
GetStr(i) = stiSelectedItem
i = i + 1
Next
i = i - 1
End If
Application.ScreenUpdating = False
For j = 1 To i Step 1
Set Doc = Documents.Open(FileName:=GetStr(j), Visible:=True)
Windows(GetStr(j)).Activate
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Marriott International" 'Find What
.Replacement.Text = "Marriott" 'Replace With
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Application.Run macroname:="NEWMACROS"
ActiveDocument.Save
ActiveWindow.Close
Next
Application.ScreenUpdating = True
End With
MsgBox "operation end, please view", vbInformation
End Sub
' The idea comes from here.
' https://www.extendoffice.com/documents/word/1002-word-replace-multiple-files.html

Word Macro - Log changes made by a find and replace

I have the below code that will search through a word document replacing any IDs it finds with a masked version of the number using RegEx (e.g. 412345678900 becomes 4123####8900). Each document could have multiple IDs in it. The IDs are sometimes scattered throughout the document text and not just in tables (so Excel is not an option).
I want to be able to write each of the replaced versions of the text found out to a log file with the file path and file name.
Sub Auto_Masking()
'Start at the very beginning. It's a very good place to start.
Selection.HomeKey Unit:=wdStory
With Selection.Find ' Locate and mask the 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 With
Selection.Find.Execute Replace:=wdReplaceAll
'Put the user back at the beginning of the document
Selection.HomeKey Unit:=wdStory
End Sub
How can I write/append each now masked number to a log file? I would like to have the log file show a list of all the IDs masked and the file they were in, so each line in the log file should look something like this:
filePath\fileName ; maskedID
with a line for each ID number masked (with one 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 a horrible feeling this is going to be impossible based on trying to get the value I want in the log file to display in a msgbox. After days of experimenting, I'm completely out of ideas.
I'm thinking a find and a find/replace may have to be used in a larger loop, one to do the replace, and one to find what was just replaced before moving on. Maybe based on Selection.Find.Found = True
Selection.Find.Text will display the regex
Selection.Text will display only the first character of the ID number string, but no more
Selection.Find.Replacement.Text will display the string as it appears in the With section, without replacing the /1 and /3 with the values it found
Not 10 minutes after giving up, I worked it out.
The code to solve the issue and successfully complete the above task, with logging of each masked ID, is as follows:
Sub mask_card_numbers()
'
Dim Counter As Long
' This next section prepares for log writing
Dim Log1 As 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)
' Get the filename and path for the log file
FileName = ActiveDocument.Path & "\" & ActiveDocument.Name
' Mask IDs ####################################################
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
' The first pass collects a single digit from the text to search for which would artificially increase the counter so reduce it by one in advance
Counter = Counter - 1
Do
With 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 With
' By keeping the selected text after the replacement, the masked
FoundID = Selection.Text
' Write masked ID to a logfile
If Len(FoundID) > 7 Then ' Anything greater than 1 will probably work
LogIDs.WriteLine FileName & ";" & FoundID
End If
Selection.Find.Execute Replace:=wdReplaceOne
Loop While Selection.Find.Found <> False
' Done Masking IDs ###########################################
End Sub
I really don't think you can do this with Word's Find & Replace if you want to intercept the values to log them to a file.
I suggest using the Find and iterating through them to manually mask the numbers and write them to a log file. I also tweaked your regex as it didn't work. The code below only works on one file at a time.
Sub Auto_Masking()
Dim oDoc As Document
Dim oSelection As Range
Dim cc As String
Dim bFound As Boolean
Application.ScreenUpdating = False
'Handle to the relevant document
Set oDoc = ActiveDocument
'Handle to the whole doc's text
Set oSelection = oDoc.Content
'Create your log file. Amend this to cope with Append if needed
Open "C:\Temp\ChangeLog.txt" For Output As #1
With oSelection.Find
.Text = "<([4])([0-9]{15})>" 'NOTE: this will only work for Visa cards
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
bFound = True
While bFound
'Look for the next occurrence
bFound = .Execute
If bFound Then
'Raw text
cc = oSelection.Text
'Manually scramble it
oSelection.Text = Left(cc, 4) & "xxxx" & Right(cc, 4)
Print #1, oDoc.FullName & ";" & oSelection.Text
'*** Remove for Production ***
'Show the result in the Immediate window whilst debugging.
Debug.Print cc & " => " & oSelection.Text
End If
Wend
End With
'Close the log file
Close #1
'Be a good memory citizen
Set oSelection = Nothing
Set oDoc = Nothing
Application.ScreenUpdating = False
End Sub