B
brymail
Hi All,
I am having a heck of a time trying to figure out what I am doing
wrong. I recently upgraded to Office 2007 and my previous macro is
behaving oddly. I am trying to go through and find and replace key
words with information obtained from a database such as name, file
number, etc. I have different headers and footers so my macro goes
through each of the stories and separately replaces in each one. I
had no problems in Word 2003 but in Word 2007 it is hit or miss.
Here is the guts of the macro
Sub Test()
Dim myRange As Range
Dim myStoryRange As Range
Dim FindReplaceArray(20, 2) As Variant
Dim i As Integer
Dim ArraySize As Integer
FindReplaceArray(0, 0) = "FILENAME"
FindReplaceArray(0, 1) = "John Doe"
FindReplaceArray(1, 0) = "FILENO"
FindReplaceArray(1, 1) = "100000"
FindReplaceArray(2, 0) = "DATE"
FindReplaceArray(2, 1) = Format(Date, "mmmm d, yyyy")
ArraySize = 2
For Each myStoryRange In ActiveDocument.StoryRanges
For i = 0 To ArraySize
With myStoryRange.Find
.Text = FindReplaceArray(i, 0)
.Replacement.Text = FindReplaceArray(i, 1)
.MatchCase = True
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
Next i
While Not (myStoryRange.NextStoryRange Is Nothing)
Set myStoryRange = myStoryRange.NextStoryRange
For i = 0 To ArraySize
With myStoryRange.Find
.Text = FindReplaceArray(i, 0)
.Replacement.Text = FindReplaceArray(i, 1)
.MatchCase = True
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
Next i
Wend
Next myStoryRange
End Sub
I actually load the FindReplaceArray with data from a database but
have simplified here. I created a document with FILENAME, FILENO and
DATE in the main text, front page headers and footer and second page
headers and footers. When I run the macro, the main text is replaced
perfectly. In the first page header, the FILENAME and DATE are
replaced, but not the FILENO. In the first page footer and second
page header and footer, only the FILENO is replaced.
Does any of this make sense or can you offer any suggestions?
Thanks,
Bryan
I am having a heck of a time trying to figure out what I am doing
wrong. I recently upgraded to Office 2007 and my previous macro is
behaving oddly. I am trying to go through and find and replace key
words with information obtained from a database such as name, file
number, etc. I have different headers and footers so my macro goes
through each of the stories and separately replaces in each one. I
had no problems in Word 2003 but in Word 2007 it is hit or miss.
Here is the guts of the macro
Sub Test()
Dim myRange As Range
Dim myStoryRange As Range
Dim FindReplaceArray(20, 2) As Variant
Dim i As Integer
Dim ArraySize As Integer
FindReplaceArray(0, 0) = "FILENAME"
FindReplaceArray(0, 1) = "John Doe"
FindReplaceArray(1, 0) = "FILENO"
FindReplaceArray(1, 1) = "100000"
FindReplaceArray(2, 0) = "DATE"
FindReplaceArray(2, 1) = Format(Date, "mmmm d, yyyy")
ArraySize = 2
For Each myStoryRange In ActiveDocument.StoryRanges
For i = 0 To ArraySize
With myStoryRange.Find
.Text = FindReplaceArray(i, 0)
.Replacement.Text = FindReplaceArray(i, 1)
.MatchCase = True
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
Next i
While Not (myStoryRange.NextStoryRange Is Nothing)
Set myStoryRange = myStoryRange.NextStoryRange
For i = 0 To ArraySize
With myStoryRange.Find
.Text = FindReplaceArray(i, 0)
.Replacement.Text = FindReplaceArray(i, 1)
.MatchCase = True
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
Next i
Wend
Next myStoryRange
End Sub
I actually load the FindReplaceArray with data from a database but
have simplified here. I created a document with FILENAME, FILENO and
DATE in the main text, front page headers and footer and second page
headers and footers. When I run the macro, the main text is replaced
perfectly. In the first page header, the FILENAME and DATE are
replaced, but not the FILENO. In the first page footer and second
page header and footer, only the FILENO is replaced.
Does any of this make sense or can you offer any suggestions?
Thanks,
Bryan