clhare
03-07-2007, 09:30 AM
Hello!
I have a template with a number of fields in it. The fields get unlinked when the template is done running (so all fields end up actual text). I now need to add a date field on the first page and in some of the headers after the other fields are unlinked so that just these particular dates stay as fields in the document. That way, whenever the resulting file is opened or printed, it will always use the current date.
I've added the following code at the end of the macro (so it comes after the fields are unlinked) to search the text and headers, replacing any occurrences of "(date)" with the date field, but it's not working for the headers. What am I doing wrong?
Here's my code:
Dim intSection As Integer
Dim intHFType As Integer
Dim intSecCount As Integer
Dim rngPane As Range
' Insert date field in text
Selection.Find.ClearFormatting
With Selection.Find
.Text = "(date)"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
End With
Selection.Find.Execute
Selection.InsertDateTime DateTimeFormat:="MMMM d, yyyy", InsertAsField:= _
True, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
InsertAsFullWidth:=False
' Check each header in each section and if found, replace with field
intSecCount = ActiveDocument.Sections.Count
For intSection = 1 To intSecCount
With ActiveDocument.Sections(intSection)
For intHFType = 1 To 3
Set rngPane = .Headers(intHFType).Range
With rngPane.Find
.ClearFormatting
.Execute FindText:="(date)", Forward:=True, _
Wrap:=wdFindContinue, Format:=False
If .Found = True Then
Selection.InsertDateTime DateTimeFormat:="MMMM d, yyyy", InsertAsField:= _
True, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
InsertAsFullWidth:=False
End If
End With
Next intHFType
End With
Next intSection
I have a template with a number of fields in it. The fields get unlinked when the template is done running (so all fields end up actual text). I now need to add a date field on the first page and in some of the headers after the other fields are unlinked so that just these particular dates stay as fields in the document. That way, whenever the resulting file is opened or printed, it will always use the current date.
I've added the following code at the end of the macro (so it comes after the fields are unlinked) to search the text and headers, replacing any occurrences of "(date)" with the date field, but it's not working for the headers. What am I doing wrong?
Here's my code:
Dim intSection As Integer
Dim intHFType As Integer
Dim intSecCount As Integer
Dim rngPane As Range
' Insert date field in text
Selection.Find.ClearFormatting
With Selection.Find
.Text = "(date)"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
End With
Selection.Find.Execute
Selection.InsertDateTime DateTimeFormat:="MMMM d, yyyy", InsertAsField:= _
True, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
InsertAsFullWidth:=False
' Check each header in each section and if found, replace with field
intSecCount = ActiveDocument.Sections.Count
For intSection = 1 To intSecCount
With ActiveDocument.Sections(intSection)
For intHFType = 1 To 3
Set rngPane = .Headers(intHFType).Range
With rngPane.Find
.ClearFormatting
.Execute FindText:="(date)", Forward:=True, _
Wrap:=wdFindContinue, Format:=False
If .Found = True Then
Selection.InsertDateTime DateTimeFormat:="MMMM d, yyyy", InsertAsField:= _
True, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
InsertAsFullWidth:=False
End If
End With
Next intHFType
End With
Next intSection