Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2008

Tip: Looking for answers? Try searching our database.

Replace code in header

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sammy - 13 Mar 2008 22:04 GMT
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
Doug Robbins - Word MVP - 13 Mar 2008 23:10 GMT
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.

Signature

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

> Hi,
>
[quoted text clipped - 21 lines]
>    Loop
> Next
Sammy - 13 Mar 2008 23:27 GMT
Perfect.  Thanks a lot

> For iFld = ActiveDocument.Fields.Count To 1 Step -1
>
[quoted text clipped - 51 lines]
> >    Loop
> > Next
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.