Home > database >  How can I apply a VBA macro to multiple docx files?
How can I apply a VBA macro to multiple docx files?

Time:01-21

I would like to apply a VBA Macro to multiple docx files. My macro find a text with a specific font and then hide it.

This is the macro that works when you execute it on a single docx file :

Sub color()

    Dim Rng As Range
    Dim Fnd As Boolean

G:
    Set Rng = ActiveDocument.Range
    Rng.Find.ClearFormatting
    Rng.Find.Font.color = RGB(191, 191, 191)
    Rng.Find.Replacement.ClearFormatting

    With Rng.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
     Fnd = .Found
    End With
    If Fnd = True Then
        With Rng
            .MoveStart wdWord, 0
            .Select
            With .Font
                .Hidden = True
            End With
        End With
        GoTo G
    End If

End Sub

And I've found a macro on a forum that can loop on all files in a folder and I've combined to mine :

Sub Documentos()

Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
strDocNm = ActiveDocument.FullName: strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
  If strFolder & "\" & strFile <> strDocNm Then
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    With wdDoc
      Dim Rng As Range
    Dim Fnd As Boolean

G:
    Set Rng = ActiveDocument.Range
    Rng.Find.ClearFormatting
    Rng.Find.Font.color = RGB(191, 191, 191)
    Rng.Find.Replacement.ClearFormatting

    With Rng.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
     Fnd = .Found
    End With
    If Fnd = True Then
        With Rng
            .MoveStart wdWord, 0
            .Select
            With .Font
                .Hidden = True
            End With
        End With
        GoTo G
    End If
      .Close SaveChanges:=True
    End With
  End If
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub


Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function

When I execute this macro, I opens the document but does nothing.

Can someone help to combine them ?

CodePudding user response:

You set wdDoc to the open document, but then set Rng to ActiveDocument. I would also make the Save command explicit:

    With wdDoc
        Dim Rng As Range
        Dim Fnd As Boolean
G:
        Set Rng = wdDoc.Range 'Change from ActiveDocument to wdDoc
        Rng.Find.ClearFormatting
        Rng.Find.Font.Color = RGB(191, 191, 191)
        Rng.Find.Replacement.ClearFormatting

        With Rng.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute
            Fnd = .Found
        End With
        If Fnd = True Then
            With Rng
                .MoveStart wdWord, 0
                .Select
                With .Font
                    .Hidden = True
                End With
            End With
            GoTo G
        End If
        .Save    'Explicit Save
        .Close   'Then Close
    End With
  End If

CodePudding user response:

I guess it's because of Set Rng = ActiveDocument.Range. in the loop you set Rng to active document. open a file doesn't make it activated automatically. And I see you have already assigned the opened file to wdDoc. Maybe use 'Set Rng = .Range' instead. see if it works for you.

  •  Tags:  
  • Related