vbalayoutms-wordfonts

Bulk Document Reformatting - VBA Compile Error


What is wrong with the following VBA module: an attempt to standardise font and layout on over 2000 docx MSWord365 pages to Montserrat SemiBold, 14 pt in justified paragraphs?

Text

Sub ConvertMultipleDocumentsFont()
    Dim folderPath As String
    Dim fileName As String
    Dim wdApp As Object, wdDoc As Object
    Dim para As Object

    folderPath = "D:\VIDEOS\TEST" ' Change to your folder
    Set wdApp = CreateObject("Word.Application")
    wdApp.Visible = False
    fileName = Dir(folderPath & "*.docx")

    Do While fileName <> ""
        Set wdDoc = wdApp.Documents.Open(folderPath & fileName)
        For Each para In wdDoc.Paragraphs
            para.Range.ParagraphFormat.Alignment = wdAlignParagraphJustify
            para.Range.Font.Name = "Montserrat SemiBold"
            para.Range.Font.Size = 14            
        Next para
        wdDoc.Save
        wdDoc.Close
        fileName = Dir
    Loop

    wdApp.Quit
    Set wdApp = Nothing
End Sub

I have read the Microsoft Ignite Invalid Outside Procedure and Lesson 4 of VBA Tutor.


Solution

  • That's some awful code you have there.

    Try the following, which includes a browser so you can just select the folder to process. Plus you'll get a progress report on the status bar.

    Sub ReformatDocuments()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = wdAlertsNone: Application.WordBasic.DisableAutoMacros True
    Dim StrFldr As String, StrNm As String, StrDoc As String, StrFlNm As String, wdDoc As Document, bDisp As Boolean
    StrFldr = GetFolder: If StrFldr = "" Then Exit Sub
    StrDoc = ActiveDocument.FullName: StrNm = Dir(StrFldr & "\*.doc", vbNormal)
    bDisp = Application.DisplayStatusBar: Application.DisplayStatusBar = True
    While StrNm <> ""
      StrFlNm = StrFldr & "\" & StrNm
      If StrFlNm <> StrDoc Then
        Application.StatusBar = "Processing: " & StrNm
        Set wdDoc = Documents.Open(FileName:=StrFlNm, AddToRecentFiles:=False, Visible:=False)
        With wdDoc
          With .Styles(wdStyleNormal)
            .ParagraphFormat.Alignment = wdAlignParagraphJustify
            .Font.Name = "Montserrat SemiBold"
            .Font.Size = 14
          End With
          .Range.Style = wdStyleNormal
          .Close SaveChanges:=True
        End With
        DoEvents
      End If
      StrNm = Dir()
    Wend
    Application.StatusBar = False: Application.DisplayStatusBar = bDisp
    MsgBox "Updates finished.", vbOKOnly
    Application.WordBasic.DisableAutoMacros False: Application.DisplayAlerts = wdAlertsAll
    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
    

    Note: As coded, the macro processes .doc, .docx and .docm files. If you want to restrict it to .docx files, change '.doc' to '.docx'