none
Word VBA Replace Styles - how to handle "requested member of collection does not exist" RRS feed

  • Question

  • I want to change all the styles in a document from one set of styles to another. i.e. convert StyleA to Style1, StyleB to Style2 and so on. I'm grinding through it with some recorded code like this:

     

        Selection.Find.ClearFormatting

        Selection.Find.Style = ActiveDocument.Styles("StyleA")

        Selection.Find.Replacement.ClearFormatting

        Selection.Find.Replacement.Style = ActiveDocument.Styles("Style1")

        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

     

    But how do I handle the error where from time to time a document does not contain StyleA. Currently the macro ends with a "requested member does not exist" error. If it doesn't find a particular style I want it to carry on and try the next one.

     

    thanks

    WS

    Wednesday, September 21, 2011 7:50 AM

Answers

  • Put this above the code you posted above:

    On Error Resume Next

    This tells VBA to basically ignore errors and continue on the next line of code. This is the most common way people get around such errors in VBA, however you should understand how it works before you start using it flippantly. Consider this from http://www.cpearson.com/excel/errorhandling.htm:

    The second form, On Error Resume Next , is the most commonly used and misused form. It instructs to VBA to essentially ignore the error and resume execution on the next line of code. It is very important to remember that On Error Resume Next does not in any way "fix" the error. It simply instructs VBA to continue as if no error occured. However, the error may have side effects, such as uninitialized variables or objects set to Nothing. It is the responsibility of your code to test for an error condition and take appropriate action. You do this by testing the value of Err.Number and if it is not zero execute appropriate code. For example,

    On Error Resume Next
    N = 1 / 0 ' cause an error
    If Err.Number <> 0 Then
    N = 1
    End If

    This code attempts to assign the value 1 / 0 to the variable N. This is an illegal operations, so VBA will raise an error 11 -- Division By Zero -- and because we have On Error Resume Next in effect, code continues to the If statement. This statement tests the value of Err.Number and assigns some other number to N.


    James Finch MCDST
    • Marked as answer by Wylfryd Smythe Thursday, September 22, 2011 12:55 PM
    Wednesday, September 21, 2011 3:08 PM

All replies

  • Put this above the code you posted above:

    On Error Resume Next

    This tells VBA to basically ignore errors and continue on the next line of code. This is the most common way people get around such errors in VBA, however you should understand how it works before you start using it flippantly. Consider this from http://www.cpearson.com/excel/errorhandling.htm:

    The second form, On Error Resume Next , is the most commonly used and misused form. It instructs to VBA to essentially ignore the error and resume execution on the next line of code. It is very important to remember that On Error Resume Next does not in any way "fix" the error. It simply instructs VBA to continue as if no error occured. However, the error may have side effects, such as uninitialized variables or objects set to Nothing. It is the responsibility of your code to test for an error condition and take appropriate action. You do this by testing the value of Err.Number and if it is not zero execute appropriate code. For example,

    On Error Resume Next
    N = 1 / 0 ' cause an error
    If Err.Number <> 0 Then
    N = 1
    End If

    This code attempts to assign the value 1 / 0 to the variable N. This is an illegal operations, so VBA will raise an error 11 -- Division By Zero -- and because we have On Error Resume Next in effect, code continues to the If statement. This statement tests the value of Err.Number and assigns some other number to N.


    James Finch MCDST
    • Marked as answer by Wylfryd Smythe Thursday, September 22, 2011 12:55 PM
    Wednesday, September 21, 2011 3:08 PM
  • Thanks a million, James, I really appreciate it.

    WS

    Thursday, September 22, 2011 10:00 AM