PDA

View Full Version : Solved: Search header and if text found, replace with field



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

fumei
03-07-2007, 01:33 PM
You are using Selection.InsertDateTime.

The selection is not in the header.

rngPane.Find.Execute does not select the item.

To insert a field you need a range, either using Selection, or a Range object. In your code, you do have a Range, but it is the entire header range, NOT just " date ".

What to do?

Use Selection on the header, in which case Selection.Find - not rngPane.Find - will select the Found item. Then you can delete it, and insert a field.

You can NOT use a field in a Find and Replace operation.

You can find and replace TEXT with just a Range.Find, but not a field.

Well, you can not use a field for a replace with any Find and Replace. However, using selection, you can find, delete, and insert.

This is what you are doing in the document body. You are using selection.Find to find " date", replacing it with "" (in essense a delete), and as the Selection is still actibe (and has a range) you can insert the date field.

To me, it is kind of a bug (sort of), but I do not know of a way to return the location values of found text with a Range.Find. It does not select it, and I don't know a way of getting the values, OR selecting it. Found is Boolean. So you can certainly find out if "date" is in a range, and you can replace it - but only with text . It would be good if you could collapse the Range to whatever the range is of the found object...but you can't AFAIK.

clhare
03-08-2007, 01:32 PM
Okay, I couldn't get that macro to work, but I just thought of an easy way around this.

Usually I have code that unlinks the fields in the text, then unlinks the fields in the headers. So....... I put the date fields in the headers from the start and just didn't unlink the headers!

There was one reference field in the headers that I didn't want to stay as a field (since the header fields would now not be unlinked), so I just changed that to text, bookmarked the text in the original location, then used the macros to assign the bookmarked text to a string and did the old search/replace in the headers to update the text.

Phew! My moto is "if there's a will, there's a way!"