new macro

F

feagin

I am very new at VBA, and found this macro on the web that
will do search and replace everywhere, including headers.
But I cannot make it run on my Word 2002 documents. How do
I set up a dialog box? And Do I need a 'sub' line to begin
it? thanks


Option Explicit
Sub RunAllSearchAndReplaces()
' Call this routine to perform the complete search and
' replace - changed by CLR to use ActiveDocument
instead
' of a passed Document object for simplicity's sake.
Dim rgLoop As Word.Range
Dim secLoop As Word.Section
Dim hfLoop As Word.HeaderFooter

For Each rgLoop In ActiveDocument.StoryRanges
Select Case rgLoop.StoryType
Case wdEvenPagesFooterStory,
wdEvenPagesHeaderStory, _
wdFirstPageFooterStory,
wdFirstPageHeaderStory, _
wdPrimaryFooterStory, wdPrimaryHeaderStory
Case Else
RunSRthroughRange rgLoop
Do Until (rgLoop.NextStoryRange Is Nothing)
Set rgLoop = rgLoop.NextStoryRange
RunSRthroughRange rgLoop
Loop
End Select
Next rgLoop

For Each secLoop In ActiveDocument.Sections
For Each hfLoop In secLoop.Headers
RunSRthroughRange hfLoop.Range
Next hfLoop
For Each hfLoop In secLoop.Footers
RunSRthroughRange hfLoop.Range
Next hfLoop
Next secLoop
End Sub
Private Sub RunSRthroughRange(DoTo As Range)
' This routine performs the actual search and replace -
' any other operation can of course be performed on
the range.
DoTo.Find.Execute FindText:="hi",
ReplaceWith:="hello", Replace:=wdReplaceAll
End Sub
 
M

Mark Tangard

How does it not work? If you get an error message, what does
it say? And if it's from Visual Basic, what line of code does
it highlight? Always give this sort of info when something
"doesn't work." ("It doesn't work" says little.)

Also, how do you see the relevance of a dialog box for this?
If you mean a way to input the find and replace text without
editing the macro, you *could* use a small custom dialog box
("userform" in MS-speak), but unless you were doing this very
often, that might be not worth the effort, and you might get
by with two InputBox lines:

Dim strFind As String, strReplace As String
strFindText = InputBox("Enter the 'Find' text")
If Trim(strFindText) = "" Then Exit Sub
strReplace = InputBox("Enter the 'Replace' text")
If Trim(strReplace) = "" Then Exit Sub

then in each line that calls the RunSRthroughRange macro, add
the two new args, like this:

RunSRthroughRange rgLoop, strFind, strReplace

and adjust the RunSRthroughRange macro so it takes 3 arguments;

Private Sub RunSRthroughRange(DoTo As Range, xFind, xRepl)
' This routine performs the actual search and replace -
' any other operation can of course be performed on the range.
DoTo.Find.Execute FindText:=xFind, ReplaceWith:=xRepl, _
Replace:=wdReplaceAll
End Sub

Info on userforms -- an *extremely* useful feature of VBA, but
perhaps not for the complete newbie -- is at
http://www.mvps.org/word/FAQs/Userforms/CreateAUserForm.htm
and http://www.speakeasy.org/~mtangard/userforms.html
 

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