Macro to find whether a named style is used on the current page

Trying to write a macro for Word 2010 that will detect whether a specific style (called "Heading 1,UM H1") is used for any of the text on the current page. Here is my attempt. It doesn't work. What am I doing wrong?

Sub DetectUMH1Style()

    ' Select current page
    ActiveDocument.Bookmarks("\page").Range.Select
   
    ' Search for target style
    TargetStyle = ActiveDocument.Styles("Heading 1,UM H1")
    MsgBox ("Target style name = '" & TargetStyle & "'")
    Selection.Find.Style = TargetStyle
    Selection.Find.Execute
   
    If Selection.Find.Found = True Then
        MsgBox ("At least one instance of target style found")
    Else
        MsgBox ("No instances of target style found")
    End If

End Sub

The macro as you posted it does work for me. What happens when you try it? Do you get an error message, or do you get the wrong result?

There are two different ways you could make the macro "more correct" according to the rules of VBA.

  1. Because you haven't declared the variable TargetStyle, VBA considers it to be a Variant type. When you assign ActiveDocument.Styles("Heading 1,UM H1") to it and don't use the Set keyword at the beginning of the statement, VBA says "You can't assign a Style object without the Set keyword, so you must mean to assign the object's default property, .NameLocal, which is a String value." It's usually a bad idea to rely on implicit use of default properties, as it tends to hide bugs in your logic. To make this explicit, you could put ".NameLocal" at the end of the statement. If you do this, it would be best to add the statement Dim TargetStyle As String at the beginning of the macro.
  2. You could insert the Set keyword at the beginning of the "TargetStyle = " statement, so that TargetStyle is assigned the actual Style object. Then you should change the later use of it to Selection.Find.Style = TargetStyle.NameLocal, for the same reason as in item 1. Also, you should in this case declare Dim TargetStyle As Style.

I recommend reading Why variables should be declared properly.

_____________________________
https://jay-freedman.info

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.

Hmm, could it be something as simple as a missing Selection.Find.Wrap = wdFindStop in the code, to ensure that Word doesn't search beyond the selected page?
Stefan Blom
Microsoft 365 Word MVP since 2005
Volunteer Moderator (Office)
MS 365, Win 11 Pro
~~~~
Please note that I do not work for Microsoft
MVP program info: https://mvp.microsoft.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.

Stefan Blom
Microsoft 365 Word MVP since 2005
Volunteer Moderator (Office)
MS 365, Win 11 Pro
~~~~
Please note that I do not work for Microsoft
MVP program info: https://mvp.microsoft.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.

Hmm, could it be something as simple as a missing Selection.Find.Wrap = wdFindStop in the code, to ensure that Word doesn't search beyond the selected page?

No, that isn't it. As a test, I created a two-page document in which the entire first page is Normal style, and one paragraph near the end of the second page has the "Heading 1,UM H1" style. When I put the cursor on page 1 and run the macro (as posted, without an Option Explicit statement), it correctly says there are no instances found. If the Find was searching beyond the first page, it would have said there was at least one instance.

Just to check, I put the cursor in Normal text on the second page and ran the macro again, and it did find the correct paragraph.

_____________________________
https://jay-freedman.info

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.

Bruce

There may be several places where your code is failing.

Firstly, the name of the style you are looking for is "Heading 1" - the UM H1 is an alias which may or may not exist in your document. The first msgbox shows this alias is being ignored anyway but it is cleaner to avoid it in the first place. You could amend the code to search for the stylename without the alias. Jay has pointed to a more correct solution for this variable so we can move in that direction.

Secondly, the find dialog has a large number of sticky settings and you need to ensure that none of these are getting in the way of your search. The following code should solve these issues.

Sub DetectUMH1Style()
  Dim TargetStyle As Style
  ' Select current page
  ActiveDocument.Bookmarks("\page").Range.Select
  
  ' Search for target style
  Set TargetStyle = ActiveDocument.Styles("Heading 1")
  MsgBox ("Target style name = '" & TargetStyle & "'")
  
  With Selection.Find
    .Text = ""
    .ClearFormatting
    .Style = TargetStyle
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
    If .Found Then
      MsgBox ("At least one instance of target style found")
    Else
      MsgBox ("No instances of target style found")
    End If
  End With
End Sub

Andrew Lockton
Melbourne Australia

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.

Thanks for the follow-up.
Stefan Blom
Microsoft 365 Word MVP since 2005
Volunteer Moderator (Office)
MS 365, Win 11 Pro
~~~~
Please note that I do not work for Microsoft
MVP program info: https://mvp.microsoft.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 1,661 Applies to: