Find/replace in all areas of document

M

Martin

As a solution to a problem I'm having with a bar code font, I'm creating a
macro to find/replace throughout a document. However, I would like to
perform the same find/replace in every area of the document: the main body,
headers and footers (current, odd/even, first page), footnotes and endnotes,
text boxes, etc.

I've sorted out the correct code to run the find/replace I need but I'm
looking for a way in VBA to access all of these different areas. Ideas I've
had include SeekView and Story but they don't seem to have constants for odd
headers/footers (though they curiously have ones for even pages!). Is there
a collection - with a corresponding object variable - that can be
conveniently used? Or any other methods (I guess I can always use an error
handler to cope with documents that don't contain every particular area)?
Text boxes seem to be the toughest to write generic code for.

My plan is to then run this macro on each of several hundred templates we
need to change so I need it to be pretty flexible.

Hope you can help!
 
C

Charles Kenyon

At http://www.gmayor.com/installing_macro.htm you'll find a macro for
updating fields in all stories. It may help you in writing your own.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide




--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
G

Greg Maxey

Doug,

I didn't realize that link was still around. I thought this was the
link for replacing text anywhere:

http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm

Looking at the category, I don't know why it is there vice in the
MacrosVBA and I thought it was intended to replace the earlier article
as I think we found that that method would not fing text located in a
textbox that is located in a header or footer.
 
M

Martin

Thanks everyone - that really helped!

I've adapted the code from Greg's link to remove erroneous spaces, tabs and
especially hard-returns that now show as bar codes when using our bar code
font (these never appeared in 97 for some reason). I'll paste it here in
case it's useful to anyone:

Public Sub RemoveBCErrors()
On Error GoTo handler
Dim rngStory As Word.Range
Dim pFindTxt As String
Dim pReplaceTxt As String
Dim lngJunk As Long
Dim oShp As Shape
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
For Each rngStory In ActiveDocument.StoryRanges
Do
ReplaceBC rngStory
On Error Resume Next
Select Case rngStory.StoryType
Case 6, 7, 8, 9, 10, 11
If rngStory.ShapeRange.Count > 0 Then
For Each oShp In rngStory.ShapeRange
If oShp.TextFrame.HasText Then
ReplaceBC oShp.TextFrame.TextRange
End If
Next
End If
Case Else
End Select
On Error GoTo 0
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
Dim tbl As Table
Dim myRow As Row
Dim myCell As Cell
For Each tbl In ActiveDocument.tables
For Each myRow In tbl.Rows
For Each myCell In myRow.Cells
If myCell.Range.Font.Name = "BC Code 3 of 9 HR" Then
myCell.Select
Selection.EndKey Unit:=wdLine
Selection.Font.Reset
End If
Next
Next
Next
Exit Sub
handler:
If Err.Number = 4248 Then
Exit Sub
Else
MsgBox Err.Number & ": " & Err.Description
End If
End Sub

Public Sub ReplaceBC(ByVal rngStory As Word.Range)
With rngStory.Find
.Text = "[^13^9 ]"
.Font.Name = "BC Code 3 of 9 HR"
.Format = True
.Replacement.Text = ""
.Replacement.Font.Name = "Garamond"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub
 
D

Doug Robbins - Word MVP

Until you mentioned it and I clicked on the link, I thought that it pointed
to the later article which was intended to replace it. I will ping John
McGhie and to get him to delete it.

--
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