Word 2010 Macro Only Runs when Initially Created

I created a macro in Word 2010, by recording and then editing it in VB. But it only ran once. It's stored in the normal template and is visible when I view macros in any other document. But it won't run. Nothing happens when I view and then attempt to run it.

When I run it initially it works perfectly. Here's the code, in case that helps:

Sub RemoveRequirements()
'
' RemoveRequirements Macro
' Remove all requirements table titles and tables from a document.
'
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "requirements"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    While Selection.Find.Found
        Selection.Find.Execute
        Selection.HomeKey Unit:=wdLine
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
        Selection.Cut
        Selection.MoveRight Unit:=wdCharacter, Count:=1
        Selection.Tables(1).Select
        Selection.Cut
    Wend
End Sub

Answer
Answer

While editing your macro, you inadvertently deleted the line Selection.Find.Execute that would have been after End With and before While Selection.Find.Found. Here's a revised version that works:

Sub RemoveRequirements()
  With Selection.Find
    .ClearFormatting
    .Text = "requirements"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute  'This command was missing
  End With
  While Selection.Find.Found
    With Selection
      .Find.Execute
      .HomeKey Unit:=wdLine
      .EndKey Unit:=wdLine, Extend:=wdExtend
      .Cut
      .MoveRight Unit:=wdCharacter, Count:=1
      .Tables(1).Select
      .Cut
    End With
  Wend
End Sub

Author of "OOXML Hacking - Unlocking Microsoft Office's Secrets", ebook now out
John Korchok, Production Manager
production@brandwares.com

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 232 Applies to: