• IF statement not working – Word VBA

    Author
    Topic
    #480721

    Hello oh mighty geniuses of VBA!

    I need some more help.

    I’m trying to code a macro which will run through a document changing styles over to another code. I have about 80 documents which I need to reformat which are being sent to me by people with no idea of word formatting, so they’re all over the place and no two are the same.

    Therefore, the styles in each document are not identical. To combat that problem, I was trying to incorporate If statements into the code, however it doesn’t seem to like it, so I’m obviously doing it wrong.

    This is the code I have:

    Code:
    If Selection.Find.Style = ActiveDocument.Styles(“Appendix Header”) = True Then
        Selection.Find.Replacement.ClearFormatting
        Selection.Find.Replacement.Style = ActiveDocument.Styles(“Heading 6”)
       With Selection.Find
            .Text = “”
            .Replacement.Text = “”
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
        Else: GoTo 1
        End If

    However, it’s not doing what it should. When I run the macro, I get a run time error 5941 on the first line saying the requested member of the collection doesn’t exist. Shouldn’t it just be skipping that section of code if AppendixHeader doesn’t exist? “1” points to the next lot of replacing formatting. Help!!

    Viewing 3 reply threads
    Author
    Replies
    • #1312452

      I think you should leave out the IF statement because you haven’t run the Find yet. If the style doesn’t exist nothing will change. You could search for the style, then run the replace if you want, but it’s really double handling. I’ve also moved all operations into the With.
      You can probably leave the 2 replace text lines out, they do nothing.
      Not sure you should have a colon after the Else, but that goes anyway.

      Note: not tested.

      Code:
      With Selection.Find
          .Replacement..Style = “Appendix Header”
          .Replacement.Style = “Heading 6”
          .Forward = True
          .Wrap = wdFindContinue
          .Format = True
          .MatchCase = False
          .MatchWholeWord = False
          .MatchWildcards = False
          .MatchSoundsLike = False
          .MatchAllWordForms = False
      End With
      Selection.Find.Execute Replace:=wdReplaceAll

      cheers, Paul

    • #1312453

      I think that should be more like:

      Code:
      With Selection.Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Style = "Appendix Header"
          .Replacement.Style = "Heading 6"
          .Forward = True
          .Wrap = wdFindContinue
          .Format = True
          .MatchCase = False
          .MatchWholeWord = False
          .MatchWildcards = False
          .MatchSoundsLike = False
          .MatchAllWordForms = False
          .Execute Replace:=wdReplaceAll
      End With

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1312454

        Thanks for that guys!

        The issue is, is that if that particular style is not in the document I’m working on, the macro won’t run properly. I just get a 5834: Item with specified name does not exist error. That’s why I figured I needed the If statement.

    • #1312457

      Hi caracolesa,

      There are at least three ways you could handle that:
      1. Add an ‘On Error Resume Next’ statement before the ‘With Selection.Find’ line;
      2. Add an ‘On Error GoTo Errhanlder’ statement before the ‘With Selection.Find’line and some error-handling code introduced by ‘Errhanlder:’ after the ‘End With’ line; or
      3. Test for the style’s existence in the Styles collection, which involves wrapping the existing code in something like:

      Code:
      Dim oSty As Style
      For Each oSty In ActiveDocument.Styles
        If oSty.NameLocal = "Appendix Header" Then
          ' Existing code
          Exit For
        End If
      Next

      The course you take depends on what you want to do if the Style isn’t found. The last of the above options is the slowest.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1312463

      You need to be careful checking for a style as it may be defined but not used. I’d give “on error resume next” a run because the find should only fail if the style is not in use, in which case we skip the replace.

      cheers, Paul

    Viewing 3 reply threads
    Reply To: IF statement not working – Word VBA

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: