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