Finding and replacing in Word using macros

D

Donna_82

Hi there,

You'll have to forgive me because I'm sure there is an incredibly simple
answer to my question. However, I can't seem to figure it out.

I'm using the code found at this link:

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

to replace the words "location"with the specifc location (i.e. "Vancouver").
But instead of replacing with a specific word everytime I need to replace
with a word which is different everytime and entered by the user (the
location needs to be entered in each report separately). I'd rather not have
to go in to change the code for each report as this is inefficient.

Please help!

Thanks in advance.
 
D

Dave Lett

Hi Donna,

You can try using the InputBox function, as in the following:

With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Text = "location"
.Execute
End With
.Text = InputBox("Enter the location.", "Location", "New Location")
End With

HTH,
Dave
 
D

Donna_82

Hi Dave,

I'm sorry but I'm very new to programming in Word. Could you tell me where
that would fit into the code I'm using now? The code is currently as follows:

Public Sub SREntireDoc()
Dim rngStory As Word.Range
' Fix the skipped blank Header/Footer problem
MakeHFValid

' Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges

' Iterate through all linked stories
Do
SearchAndReplaceInStory rngStory, "Location", "New Location"

' Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
End Sub


Public Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, _
ByVal strSearch As String, _
ByVal strReplace As String)
Do Until (rngStory Is Nothing)
With rngStory.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = strSearch
.Replacement.Text = strReplace
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
Set rngStory = rngStory.NextStoryRange
Loop
End Sub

Public Sub MakeHFValid()
Dim lngJunk As Long

' It does not matter whether we access the Headers or Footers property.
' The critical part is accessing the stories range object

lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
End Sub

This code came from a link I found through this site.
Your help is very much appreciated!

Donna
 
D

Dave Lett

Hi Donna,

You could do something with the following line:

SearchAndReplaceInStory rngStory, "Location", "New Location"
by changing it to
SearchAndReplaceInStory rngStory, "Location", InputBox("Enter the
location.", "Location", "New Location")


HTH,
Dave
 
D

Donna_82

Hi Again Dave,

I actually tried that and I end up in a loop where the InputBox just keeps
coming up and will not disappear even when I hit cancel. Any idea why this
is happening?

I really appreciate your help!

Thanks in advance,
Donna
 
D

Dave Lett

Hi Donna,

I'm not positive, but I think it has to do with the fact that you have it
Loop through each story range in both routines:


Public Sub SREntireDoc()
Do
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
End Sub

Public Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, _
ByVal strSearch As String, _
ByVal strReplace As String)
Do Until (rngStory Is Nothing)
Set rngStory = rngStory.NextStoryRange
Loop
End Sub

So you call SearchAndReplaceInStory and cycle through each storyrange for
each storyrange in the routine SREntireDoc.

HTH,
Dave
 
D

Donna_82

Hi Dave,

I'm really sorry to keep bugging you but I'm really no good with programming
and I feel like I've really hit a brick wall. I don't even know if this is
the best way for me to do what I need to do.

Basically I need to enter the "location" on the Title Page and have it enter
itself throughout the report in headers and other specific spots where the
location is specified.

First of all, do you know how I would fix the problem you've discovered?
I've just used code that I found and really don't know how to manipulate it
to do what is necessary.

Secondly, the text that I need to change is always in the same place so it
isn't really necessary for me to check the whole document. Rahter than using
Find and Replace would it be easier for me to somehow link the text within
the document in the specific locations so that when I change the "location"
in on part of the document it changes in all of the designated places
throughout the document. I tried using a table and setting the cells equal
as I would do in Excel, but apparently in Word only numerical formulas will
transfer.

Any suggestions?

Thanks so much for your help,
Donna
 
D

Donna_82

Hi Greg,

Thanks a lot! That link was very helpful.

I'm just wondering if there's any way to link the text with the cross
referenced bookmarks that will only change the words in the bookmark and not
the formatting. For example, the text will be on a title page and in a
header and obviously the formatting would not be the same in both.

Thanks in advance,
Donna
 
J

Jay Freedman

Hi Donna,

Include a \*CHARFORMAT switch in the REF field, and the result of the
field will always have the same character formatting as the R in REF
(which is usually the same as the rest of the field code but doesn't
have to be). See the help topic "Format (\*) field switch".
 
D

Donna_82

Thanks so much! That worked perfectly.

I just have one more question.... hard to believe I know!
Is there anyway to take only the first word of the bookmark and insert it in
one of the references. For example, I will be entering a company name. When
it is entered in the report the whole company name will be inserted (i.e.
"Smith's Services Ltd.") but in the headers I want only the abbreviated name
to show up (i.e. "Smith's")... Is this possible?

Thanks again!
Donna
 
G

Greg

Donna,

Pretty gnarly for my skill set. I suppose you could do this with a bit
of VBA manipulation.

Set a docVariable = to the text of the first word in the bookmark
range. Then use a DocVariable field in the header. Let's call the
bookmark "firmName" and the DocVarialbe shortName. Put a {DocVariable
shortName} field in the header. Something like:

Sub PartialREF()
Dim oRng As Range
Dim pRange As Word.Range
Dim myString As String

Set oRng = ActiveDocument.Bookmarks("firmName").Range
myString = oRng.Words.First.Text
ActiveDocument.Variables("shortName").Value = myString
For Each pRange In ActiveDocument.StoryRanges
Do
pRange.Fields.Update
Set pRange = pRange.NextStoryRange
Loop Until pRange Is Nothing
Next
End Sub
 
J

Jay Freedman

Hi Donna,

There isn't any simple way to extract the first word in a Ref field.

Probably a better method would be to put the long name and the short name
into two separate custom document properties (which you can set up on the
Custom tab of the File > Properties dialog, or create a userform to collect
the information and add the properties as members of the
CustomDocumentProperties collection). Then replace the bookmark with a
DocProperty field pointing to the long name, and replace the cross-reference
with a DocProperty field pointing to the short name.
 

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