0

I'm trying to use VBA on Word 2013.

I'm on Win10.

I created a macro a long time ago to format documents to how I want them.

But,now, when I click to run the macro, I get: "OBJECT VARIABLE OR BLOCK VARIABLE NOT SET"

Of course, Office's Help doesn't "help" at all. I have no idea what the problem is.

Could someone, please, take a look below at the macro, and tell me what the problem might be?

I will repeat: It worked fine before. Now, I get this error.

    Sub FormatEverything()  
    '    
    ' FormatEverything Macro    
    ' Formats just about everything in text: removes hyperlinks, replaces spaces, removes graphics, regularizes paragraphs, etc.    
    '
    
        Selection.WholeStory    
        Selection.ClearFormatting    
        WordBasic.ClearAllFormatting    
        Selection.Fields.Unlink    
        Selection.WholeStory    
        Selection.Font.Name = "Times New Roman"    
        Selection.Font.Size = 12   
        Selection.Find.ClearFormatting    
        Selection.Find.Replacement.ClearFormatting
    
        With Selection.Find    
            .Text = " "    
            .Replacement.Text = " "    
            .Forward = True    
            .Wrap = wdFindStop    
            .Format = False    
            .MatchCase = False    
            .MatchWholeWord = False    
            .MatchWildcards = False    
            .MatchSoundsLike = False    
            .MatchAllWordForms = False    
        End With
    
        Selection.Find.Execute Replace:=wdReplaceAll   
        Selection.Find.ClearFormatting    
        Selection.Find.Replacement.ClearFormatting
    
        With Selection.Find    
            .Text = "^l"    
            .Replacement.Text = "^p"    
            .Forward = True    
            .Wrap = wdFindStop    
            .Format = False    
            .MatchCase = False    
            .MatchWholeWord = False    
            .MatchWildcards = False    
            .MatchSoundsLike = False    
            .MatchAllWordForms = False    
        End With
    
        Selection.Find.Execute Replace:=wdReplaceAll    
        Selection.Find.ClearFormatting    
        Selection.Find.Replacement.ClearFormatting
    
        With Selection.Find    
            .Text = "^g"    
            .Replacement.Text = ""    
            .Forward = True    
            .Wrap = wdFindStop    
            .Format = False    
            .MatchCase = False    
            .MatchWholeWord = False    
            .MatchWildcards = False
            .MatchSoundsLike = False    
            .MatchAllWordForms = False    
        End With
    
        Selection.Find.Execute Replace:=wdReplaceAll
    
        With Selection.Find    
            .Text = "'"    
            .Replacement.Text = "'"    
            .Forward = True    
            .Wrap = wdFindStop    
            .Format = False    
            .MatchCase = False    
            .MatchWholeWord = False    
            .MatchWildcards = False    
            .MatchSoundsLike = False    
            .MatchAllWordForms = False    
        End With
    
        Selection.Find.Execute Replace:=wdReplaceAll
    
        With Selection.Find    
            .Text = """"    
            .Replacement.Text = """"    
            .Forward = True    
            .Wrap = wdFindStop    
            .Format = False    
            .MatchCase = False    
            .MatchWholeWord = False    
            .MatchWildcards = False    
            .MatchSoundsLike = False    
            .MatchAllWordForms = False    
        End With
    
        Selection.Find.Execute Replace:=wdReplaceAll
    
        With Selection.ParagraphFormat    
            .SpaceBefore = 0    
            .SpaceBeforeAuto = False    
            .SpaceAfter = 0    
            .SpaceAfterAuto = False    
            .LineSpacingRule = wdLineSpaceSingle    
            .LineUnitBefore = 0    
            .LineUnitAfter = 0    
        End With
    
        Selection.Find.ClearFormatting    
        Selection.Find.Replacement.ClearFormatting   
 
        With Selection.Find    
            .Text = " ^p"    
            .Replacement.Text = "^p"    
            .Forward = True    
            .Wrap = wdFindStop    
            .Format = False    
            .MatchCase = False    
            .MatchWholeWord = False    
            .MatchWildcards = False    
            .MatchSoundsLike = False    
            .MatchAllWordForms = False    
        End With
    
        Selection.Find.Execute Replace:=wdReplaceAll    
        ActiveWindow.ActivePane.VerticalPercentScrolled = 19    
        Selection.Find.ClearFormatting    
        Selection.Find.Replacement.ClearFormatting
    
        With Selection.Find    
            .Text = "^p"    
            .Replacement.Text = "^p^p"    
            .Forward = True    
            .Wrap = wdFindStop    
            .Format = False    
            .MatchCase = False    
            .MatchWholeWord = False    
            .MatchWildcards = False    
            .MatchSoundsLike = False    
            .MatchAllWordForms = False    
        End With
    
        Selection.Find.Execute Replace:=wdReplaceAll
    
        With Selection.Find    
            .Text = "^p^p^p"    
            .Replacement.Text = "^p^p"    
            .Forward = True    
            .Wrap = wdFindStop    
            .Format = False    
            .MatchCase = False    
            .MatchWholeWord = False    
            .MatchWildcards = False    
            .MatchSoundsLike = False    
            .MatchAllWordForms = False    
        End With
    
        Selection.Find.Execute Replace:=wdReplaceAll    
        Selection.Find.Execute Replace:=wdReplaceAll    
        Selection.Find.Execute Replace:=wdReplaceAll    
        Selection.Find.Execute Replace:=wdReplaceAll    
        Selection.Find.Execute Replace:=wdReplaceAll    
        ActiveWindow.ActivePane.VerticalPercentScrolled = 4
    
    End Sub
    Sub ReformatTextBetweenMarkers()    
    '    
    ' ReformatTextBetweenMarkers Macro    
    ' Re-formats & changes text between two markers (e. g., [ ] )    
    '    
        Selection.Find.ClearFormatting    
        Selection.Find.Replacement.ClearFormatting
    
        With Selection.Find.Replacement.Font    
            .SmallCaps = False    
            .AllCaps = True    
        End With
    
        With Selection.Find    
            .Text = "(\[)(*)(\])"    
            .Replacement.Text = "\1\2\3"    
            .Forward = True    
            .Wrap = wdFindContinue    
            .Format = True    
            .MatchCase = False    
            .MatchWholeWord = False    
            .MatchAllWordForms = False    
            .MatchSoundsLike = False    
            .MatchWildcards = True    
        End With
    
        Selection.Find.Execute Replace:=wdReplaceAll
    
    End Sub
4
  • 1
    With the VBE open click first "active" line inside the code block you wish to debug, set a breakpoint, then launch the code as usual. Continue execution step by step to find the problem (shift F8) Commented Aug 25 at 8:18
  • Where does the error occur? Show the steps needed to reproduce the problem. stackoverflow.com/help/how-to-ask Commented Aug 25 at 8:27
  • 1
    When you get the error, click "Debug" and tell us which line is highlighted Commented Aug 25 at 16:13
  • You should be using paragraph styles for your uniform paragraph formatting. Then to change, simply modify the style. Here is my page on the importance of styles in Word. addbalance.com/usersguide/stylesImportance.htm Commented Aug 26 at 4:18

1 Answer 1

2

That's some very inefficient & verbose code you're using, plus your brute-force approach to paragraph formatting really isn't the way things should be done. I also can't see the point of your first Find/Replace - all it does is relace all single spaces with single spaces.

As it stands, your code could be reduced and made much more efficient - and with no screen flicker - with:

Sub FormatEverything()
'
' FormatEverything Macro
' Formats just about everything in text: removes hyperlinks, replaces spaces, removes graphics, regularizes paragraphs, etc.
Application.ScreenUpdating = False
With ActiveDocument.Range
  .Font.Name = "Times New Roman"
  .Font.Size = 12
  With .ParagraphFormat
    .SpaceBefore = 0
    .SpaceAfter = 0
    .LineSpacingRule = wdLineSpaceSingle
    .LineUnitBefore = 0
    .LineUnitAfter = 0
  End With
  .Fields.Unlink
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Wrap = wdFindContinue
    .MatchWildcards = False
    .Forward = True
    .Format = False
    .Text = " "
    .Replacement.Text = " "
    .Execute Replace:=wdReplaceAll
    .Text = "^l"
    .Replacement.Text = "^p"
    .Execute Replace:=wdReplaceAll
    .Text = "^g"
    .Replacement.Text = ""
    .Execute Replace:=wdReplaceAll
    .Text = "'"
    .Replacement.Text = "'"
    .Execute Replace:=wdReplaceAll
    .Text = """"
    .Replacement.Text = """"
    .Execute Replace:=wdReplaceAll
    .Text = " ^p"
    .Replacement.Text = "^p"
    .Execute Replace:=wdReplaceAll
    .Text = "^p"
    .Replacement.Text = "^p^p"
    .Execute Replace:=wdReplaceAll
    .MatchWildcards = True
    .Text = "[^13]{3,}"
    .Replacement.Text = "^p^p"
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
End Sub

Sub ReformatTextBetweenMarkers()
'
' ReformatTextBetweenMarkers Macro
' Re-formats & changes text between two markers (i.e. [ ])
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Replacement.Font.SmallCaps = False
    .Replacement.Font.AllCaps = True
    .Wrap = wdFindContinue
    .MatchWildcards = True
    .Forward = True
    .Format = True
    .Text = "\[[!\[]@\]"
    .Replacement.Text = "^&"
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
End Sub

In the first macro, everything between 'With ActiveDocument.Range' and '.Fields.Unlink' should be replaced with a line like:

.Style = "Normal"

where 'Normal' is the name of a Style having all those paragraph attributes. If you create such a Style and add it to the document's template, then all you need do is reference the Style as shown above.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.