I have this MS Word macro to run over all files docx in folder (without open them since Visible=false) and call another macro for each file.
The second macro called is "Sub ApplyFormat()" and as you can see, I use With Selection, so when the macro calls the 2nd macro it doesn´t make
any changes, I think because my 2nd macro uses "Selection" and to use Selection I think only works if file is opened and visible.
I try to open each document with Visible:=False in order to enhance performance.
How can I fix this? Maybe using some variable instead of "Selection" in my 2nd macro but I don't know how.
Thanks for any help.
Sub FormatAllDocxInFolder()
Dim sFolder As String, sFile As String, wdDoc As Document
sFolder = "C:\PATH\TO\FILES"
sFile = Dir(sFolder & "\*.docx", vbNormal)
While sFile <> ""
Set wdDoc = Documents.Open(FileName:=sFolder & "\" & sFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc
Call ApplyFormat
.Close SaveChanges:=True
End With
strFile = Dir()
Wend
Set wdDoc = Nothing
End Sub
Sub ApplyFormat()
Selection.HomeKey wdStory
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "abc"
.Replacement.Text = " def"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
End Sub