0

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.

7
  • 3
    The error should lead to a specific line being highlighted in the editor. Which line is it? Everything in your posted code is inside of a procedure so it seems like there's something you're not showing us. Commented Oct 3 at 16:37
  • 2
    FYI you probably should make sure that folderPath ends with \ (backslash). Are you running this in Word? If Yes then there's no need to use late binding and you should (eg) Dim wdApp As Word.Application Commented Oct 3 at 16:48
  • 2
    The AI overview here google.com/search?q=invalid+outside+procedure+vba will give you a bunch of things to check for in your code. Commented Oct 3 at 17:03
  • 3
    Oh if only people would learn to use Word correctly! Applying formatting in this way just makes things more difficult. If the documents had styles applied you would only need to modify the properties of a single style in each document, instead of this awful brute force formatting approach. If document themes had been used changing the theme would automatically change the font throughout the whole document. Commented Oct 3 at 18:57
  • 1
    Thank you for your replies. ("If only people . . . " The reason for the format change is to replace OLEBound doc. files created 9 years ago before OLE was discouraged as it is now. The font and layouts had to be made to fit little boxes!) Thank you for the revised code (to replace the awful code from AI). The new code is certainly not causing any error notifications but it does not appear to work althougn on one occasion I did recieve a message to say it had ended. Many thanks. Commented Oct 4 at 14:54

1 Answer 1

3

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'

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.