Searching for StyleRef Fields (STYLEREF 1 \n) in Headers

A

andreas

Dear Experts:
Below macro should do the following task

- Search for the first occurence of a certain StyleRef Field
{ STYLEREF 1 \n } in all the headers
- If the first ocurrence of above field is found, a MsgBox should say
STYLEREF FIELD found and the macro is to be exited.
- If no such field is found, then a MsgBox should say: No such fields
found and the macro is to be exited.

Below macro is not working properly. Although I got these fields in my
headers, the MsgBox tells me there are none.

Any help is much appreciated. Thank you very much. Regards, Andreas

P.S.: to get the outline numbering of the numbered heading 1 as a
field in the header it is sufficient to do it the way I did it
{ STYLEREF 1 \n }. Most people use { STYLEREF Heading 1 \n }. But this
way if you share this file with somebody with another language version
of Word, you will get an error message.







Sub Search_StyleRef_1_N_Header()
Dim rng As range
Dim AskText As String
Dim AskReplacementText As String
Dim sec As Section
Dim hdr As HeaderFooter

ActiveWindow.View.ShowFieldCodes = True

For Each sec In ActiveDocument.Sections
For Each hdr In sec.Headers
Set rng = hdr.range
With rng.Find
.Text = "^0019^wSTYLEREF^w1^w\n"
.Forward = True
.Wrap = wdFindStop 'Find only one occurrence
If Not .Execute() Then
MsgBox "No StyleRef Fields (STYLEREF 1 \n) found",
vbInformation, "Searching StyleRef Fields"
Exit Sub
Else
MsgBox "StyleRef field (STYLEREF 1 \n) has been found"
Exit Sub
End If
End With

Next hdr
Next sec

ActiveWindow.View.ShowFieldCodes = False
End Sub
 
G

Graham Mayor

A simpler approach would be to test the field types and then their content
eg

Sub Search_StyleRef_1_N_Header()
Dim rng As Range
Dim AskText As String
Dim AskReplacementText As String
Dim sec As Section
Dim hdr As HeaderFooter
Dim fld As Field

For Each sec In ActiveDocument.Sections
For Each hdr In sec.Headers
For Each fld In hdr.Range.Fields
If fld.Type = wdFieldStyleRef Then
If InStr(1, fld.Code, "1 \n") Then
MsgBox "StyleRef field (STYLEREF 1 \n) has been
found"
Exit Sub
End If
End If
Next fld
Next hdr
Next sec
MsgBox "No StyleRef Fields (STYLEREF 1 \n) found"
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
A

andreas

A simpler approach would be to test the field types and then their content
eg

Sub Search_StyleRef_1_N_Header()
    Dim rng As Range
    Dim AskText As String
    Dim AskReplacementText As String
    Dim sec As Section
    Dim hdr As HeaderFooter
    Dim fld As Field

    For Each sec In ActiveDocument.Sections
        For Each hdr In sec.Headers
            For Each fld In hdr.Range.Fields
                If fld.Type = wdFieldStyleRef Then
                    If InStr(1, fld.Code, "1 \n") Then
                        MsgBox "StyleRef field (STYLEREF 1 \n) has been
found"
                        Exit Sub
                    End If
                End If
            Next fld
       Next hdr
    Next sec
    MsgBox "No StyleRef Fields (STYLEREF 1 \n) found"
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>













- Show quoted text -

Dear Graham,

great code. It is working as desired. Thanks for your terrific help. I
really appreciate it. Regards, Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top