0
votes

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
2

2 Answers

1
votes

issues found with the code

  1. strFile = Dir() used instead of sFile = Dir()
  2. Either WdDoc document object to be passed as an argument to Sub ApplyFormat Or the Code chunk is to be directly used in the file loop

it worked Like this

Sub FormatAllDocxInFolder()
Dim sFolder As String, sFile As String, wdDoc As Document
sFolder = "C:\Users\user\desktop\folder1\"

sFile = Dir(sFolder & "\*.docx", vbNormal)
While sFile <> ""
  Set wdDoc = Documents.Open(FileName:=sFolder & "\" & sFile, AddToRecentFiles:=False, Visible:=False)
  With wdDoc
    Call ApplyFormat(wdDoc)
    .Close SaveChanges:=True
  End With
  sFile = Dir
Wend
Set wdDoc = Nothing
End Sub
Sub ApplyFormat(Xdoc As Document)
    With Xdoc.Content.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "abc"
        .Replacement.Text = " def"
        .Wrap = wdFindContinue
        .Execute Replace:=wdReplaceAll
    End With
End Sub
1
votes

Your "(without open them since Visible=false)" assumption is incorrect - the files are being opened. However, since Visible=False, nothing is being selected. Accordingly, you need to (and should, anyway) work with the document directly, as in:

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)
  Call ApplyFormat(wdDoc)
  wdDoc.Close SaveChanges:=True
  sFile = Dir()
Wend
Set wdDoc = Nothing
End Sub

Sub ApplyFormat(wdDoc As Document)
With wdDoc.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = "abc"
  .Replacement.Text = " def"
  .Wrap = wdFindContinue
  .Execute Replace:=wdReplaceAll
End With
End Sub

In any event, your code could be reduced to:

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
    With .Range.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "abc"
        .Replacement.Text = " def"
        .Wrap = wdFindContinue
        .Execute Replace:=wdReplaceAll
    End With
    .Close SaveChanges:=True
  End With
  sFile = Dir()
Wend
Set wdDoc = Nothing
End Sub