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.

Re: Finding and replacing in Word using macros by Dave

Dave
Mon May 02 12:52:30 CDT 2005

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

"Donna_82" <Donna_82@discussions.microsoft.com> wrote in message
news:EDF621E7-C98F-4567-8B1C-A2525C6513E5@microsoft.com...
> 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.



Re: Finding and replacing in Word using macros by Donna82

Donna82
Mon May 02 13:11:03 CDT 2005

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


"Dave Lett" wrote:

> 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
>
> "Donna_82" <Donna_82@discussions.microsoft.com> wrote in message
> news:EDF621E7-C98F-4567-8B1C-A2525C6513E5@microsoft.com...
> > 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.
>
>
>

Re: Finding and replacing in Word using macros by Dave

Dave
Tue May 03 07:45:21 CDT 2005

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
"Donna_82" <Donna82@discussions.microsoft.com> wrote in message
news:0D0510A8-8174-495A-8472-BF1E298ACD3D@microsoft.com...
> 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
>
>
> "Dave Lett" wrote:
>
> > 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
> >
> > "Donna_82" <Donna_82@discussions.microsoft.com> wrote in message
> > news:EDF621E7-C98F-4567-8B1C-A2525C6513E5@microsoft.com...
> > > 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.
> >
> >
> >



Re: Finding and replacing in Word using macros by Donna82

Donna82
Tue May 03 09:21:05 CDT 2005

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

"Dave Lett" wrote:

> 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

Re: Finding and replacing in Word using macros by Dave

Dave
Tue May 03 10:01:19 CDT 2005

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

"Donna_82" <Donna82@discussions.microsoft.com> wrote in message
news:047A4282-6D84-435C-BF61-AC972746D902@microsoft.com...
> 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
>
> "Dave Lett" wrote:
>
> > 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



Re: Finding and replacing in Word using macros by Donna82

Donna82
Tue May 03 10:31:10 CDT 2005

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

"Dave Lett" wrote:

> 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
>
> "Donna_82" <Donna82@discussions.microsoft.com> wrote in message
> news:047A4282-6D84-435C-BF61-AC972746D902@microsoft.com...
> > 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
> >
> > "Dave Lett" wrote:
> >
> > > 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
>
>
>

Re: Finding and replacing in Word using macros by Greg

Greg
Tue May 03 11:07:37 CDT 2005

Donna,

See:
http://gregmaxey.mvps.org/Repeating_Data.htm


Re: Finding and replacing in Word using macros by Donna82

Donna82
Fri May 06 06:25:14 CDT 2005

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

"Greg" wrote:

> Donna,
>
> See:
> http://gregmaxey.mvps.org/Repeating_Data.htm
>
>

Re: Finding and replacing in Word using macros by Jay

Jay
Fri May 06 07:01:30 CDT 2005

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

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org

On Fri, 6 May 2005 04:25:14 -0700, "Donna_82"
<Donna82@discussions.microsoft.com> wrote:

>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
>
>"Greg" wrote:
>
>> Donna,
>>
>> See:
>> http://gregmaxey.mvps.org/Repeating_Data.htm
>>
>>


Re: Finding and replacing in Word using macros by Donna82

Donna82
Fri May 06 09:31:07 CDT 2005

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

"Jay Freedman" wrote:

> 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".
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
>
> On Fri, 6 May 2005 04:25:14 -0700, "Donna_82"
> <Donna82@discussions.microsoft.com> wrote:
>
> >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
> >
> >"Greg" wrote:
> >
> >> Donna,
> >>
> >> See:
> >> http://gregmaxey.mvps.org/Repeating_Data.htm
> >>
> >>
>
>

Re: Finding and replacing in Word using macros by Greg

Greg
Fri May 06 10:00:55 CDT 2005

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


Re: Finding and replacing in Word using macros by Jay

Jay
Fri May 06 10:08:55 CDT 2005

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.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org

Donna_82 wrote:
> 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
>
> "Jay Freedman" wrote:
>
>> 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".
>>
>> --
>> Regards,
>> Jay Freedman
>> Microsoft Word MVP FAQ: http://word.mvps.org
>>
>> On Fri, 6 May 2005 04:25:14 -0700, "Donna_82"
>> <Donna82@discussions.microsoft.com> wrote:
>>
>>> 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
>>>
>>> "Greg" wrote:
>>>
>>>> Donna,
>>>>
>>>> See:
>>>> http://gregmaxey.mvps.org/Repeating_Data.htm



Re: Finding and replacing in Word using macros by Donna82

Donna82
Fri May 06 11:16:06 CDT 2005

I will try that... Thanks so much for all your help!

Donna


"Jay Freedman" wrote:

> 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.
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
>
> Donna_82 wrote:
> > 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
> >
> > "Jay Freedman" wrote:
> >
> >> 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".
> >>
> >> --
> >> Regards,
> >> Jay Freedman
> >> Microsoft Word MVP FAQ: http://word.mvps.org
> >>
> >> On Fri, 6 May 2005 04:25:14 -0700, "Donna_82"
> >> <Donna82@discussions.microsoft.com> wrote:
> >>
> >>> 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
> >>>
> >>> "Greg" wrote:
> >>>
> >>>> Donna,
> >>>>
> >>>> See:
> >>>> http://gregmaxey.mvps.org/Repeating_Data.htm
>
>
>