Results 1 to 6 of 6
  1. #1
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can't Find Style in Headers (Word XP)

    Hi again. And before I start, I want to thank everyone who has been helping me with my questions in this forum. Sometimes I latch onto the answer and dive right back into my code and forget to politely thank those who help me.
    Anyway... I have yet another problem that I can't figure out.
    I'm writing a function that will tell me if a style is being used anywhere in a document. (The .InUse property plays with semantics, at least as far as I would interpret that property name!) I'm posting my code below, and hoping that someone can figure out what I might be missing. The code is supposed to take the name of a style (in the form of a string) and search everywhere in the document, including headers and footers, to see if it is applied to any paragraphs. But what I'm finding is that, while it searches the main story, and the headers and footers (as is evident by the lines of debugging clues), it just won't find the style, even in the main story.
    I should mention that, in testing this code, I found that it WILL find any text specified by the .Find.Text argument; it just won't find styles... or so it appears to me.
    Please help!

    <font face="Georgia">
    Sub test()
    'This tests the function listed immediately after
    If StyleInUse("Mike") = True Then
    MsgBox "In use"
    Else
    MsgBox "Not in use"
    End If
    End Sub

    Function StyleInUse(StyleName As String) As Boolean
    Dim myStoryRange As Range
    Dim dStyle As Style
    Dim sFound As Boolean

    Set dStyle = ActiveDocument.Styles(StyleName)
    StyleInUse = True

    For Each myStoryRange In ActiveDocument.StoryRanges
    Debug.Print "Searching " & myStoryRange.StoryType & ": " & myStoryRange.Text
    With myStoryRange.Find
    .Format = True
    .Style = dStyle
    .Text = ""
    .Replacement.Text = ""
    .Wrap = wdFindStop
    .Execute
    If .Found = True Then
    sFound = True
    Exit For
    End If
    End With
    Do While Not (myStoryRange.NextStoryRange Is Nothing)
    Set myStoryRange = myStoryRange.NextStoryRange
    Debug.Print "Searching " & myStoryRange.StoryType & ": " & myStoryRange.Text
    With myStoryRange.Find
    .Format = True
    .Style = dStyle
    .Text = ""
    .Replacement.Text = ""
    .Wrap = wdFindContinue
    .Execute
    If .Found = True Then
    sFound = True
    Exit For
    End If
    End With
    Loop
    Next myStoryRange

    StyleInUse = sFound

    End Function
    </font face=georgia>
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Can't Find Style in Headers (Word XP)

    Your code seems to work OK in my version of Word 2002 (XP). Perhaps you should use ClearFormatting before specifying the style to be searched for, to clear any remnants of previous format searches:

    .ClearFormatting
    .Style = dStyle

  3. #3
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Can't Find Style in Headers (Word XP)

    Bizarre.
    I am attaching a document that contains a paragraph style called "Mike". This style is used only in the header. Even after I added the .ClearFormatting line and switched to Print Layout view (I always work in Normal view), it still could not find it.


    Sub test()
    If StyleInUse("Mike") = True Then
    MsgBox "In use"
    Else
    MsgBox "Not in use"
    End If
    End Sub
    Attached Files Attached Files
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Can't Find Style in Headers (Word XP)

    I'm merely guessing here, but I think it works as follows:
    - The page header contains a single paragraph, formatted as "Mike".
    - The "Mike" format is stored in the paragraph mark of the single paragraph.
    - The outer loop (For ... Next) has .Wrap = wdFindStop.
    - This forces the Find process to stop before the final paragraph mark in the header.
    - So the "Mike" style is not found.

    If the header contains more than one paragraph (the second one may be in any style), "Mike" will be found.
    If the outer loop has .Wrap = wdFindContinue, "Mike" will be found too, even if the header has only one paragraph.

  5. #5
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Can't Find Style in Headers (Word XP)

    It behaves the same for me as for you. If the style "Mike" is in the main story then it finds it, but in the header Find.Found always seems to be false.

    If I add a second paragraph to the header, either Normal style or Milke style then it behaves as expected.

    StuartR

  6. #6
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Can't Find Style in Headers (Word XP)

    Most excellent! Although it's hardly intuitive (but then again, neither is Word's definition of "InUse"), the .FindAndContinue argument sure beats my workaround. I found if I set .MatchWildcards to True and specified "*" as the search text (it's an asterisk, if you can't tell), it also worked.

    Do you suppose (he wondered rhetorically) it's a bug or a feature?
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

Posting Permissions

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