Replace code in header

S

Sammy

Hi,

The following macro works in the body of the document but not in the Header
or Footer. What am I doing wrong?? Thanks for your help

Dim strCodes As String
Dim iFld As Integer
Dim strFind As String
Dim strRepl As String
Dim pRange As Word.Range


For Each pRange In ActiveDocument.StoryRanges
Do Until pRange Is Nothing
For iFld = ActiveDocument.Fields.Count To 1 Step -1
With ActiveDocument.Fields(iFld)
If .Type = wdFieldDate Then
.Code.Text = Replace(UCase(.Code.Text), _
"DATE", "CREATEDATE")
End If
End With
Next iFld
Set pRange = pRange.NextStoryRange
Loop
Next
 
D

Doug Robbins - Word MVP

For iFld = ActiveDocument.Fields.Count To 1 Step -1

and

With ActiveDocument.Fields(iFld)

limit the scope to the main story range.

Use

Dim prange As Range
Dim iFld As Long
For Each prange In ActiveDocument.StoryRanges
Do Until prange Is Nothing
For iFld = prange.Fields.Count To 1 Step -1
With prange.Fields(iFld)
If .Type = wdFieldDate Then
.Code.Text = Replace(UCase(.Code.Text), _
"DATE", "CREATEDATE")
End If
End With
Next iFld
Set prange = prange.NextStoryRange
Loop
Next
End Sub

In this case, as you are not changing the count of the fields in the
document, there is really no need to go backwards through the fields.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

Sammy

Perfect. Thanks a lot

Doug Robbins - Word MVP said:
For iFld = ActiveDocument.Fields.Count To 1 Step -1

and

With ActiveDocument.Fields(iFld)

limit the scope to the main story range.

Use

Dim prange As Range
Dim iFld As Long
For Each prange In ActiveDocument.StoryRanges
Do Until prange Is Nothing
For iFld = prange.Fields.Count To 1 Step -1
With prange.Fields(iFld)
If .Type = wdFieldDate Then
.Code.Text = Replace(UCase(.Code.Text), _
"DATE", "CREATEDATE")
End If
End With
Next iFld
Set prange = prange.NextStoryRange
Loop
Next
End Sub

In this case, as you are not changing the count of the fields in the
document, there is really no need to go backwards through the fields.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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