Results 1 to 6 of 6
  1. #1
    Lounger
    Join Date
    Dec 2011
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    IF statement not working - Word VBA

    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!!

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Platinum Lounger
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    3,551
    Thanks
    7
    Thanked 225 Times in 213 Posts
    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

  4. #3
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,879
    Thanks
    0
    Thanked 183 Times in 168 Posts
    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
    [MS MVP - Word]

  5. #4
    Lounger
    Join Date
    Dec 2011
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  6. #5
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,879
    Thanks
    0
    Thanked 183 Times in 168 Posts
    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
    [MS MVP - Word]

  7. #6
    Platinum Lounger
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    3,551
    Thanks
    7
    Thanked 225 Times in 213 Posts
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •