Replacing Found Text with AutoText Entry

G

Greg Maxey

Russ,
You're right. Does Jerem really need to insert fields?

Really I don't know. I just saw what he was doing with his code and assumed
he had a reason for them.

Good point on LBound. I like Split as it means typing less double quotes
when I write the code ;-)

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Greg,
You're right. Does Jerem really need to insert fields?
What advantage does he gain by inserting fields?

Your code should be faster, if there are less lines of code to
process. In my MacWord, I don't have the built-in commands of the
newer versions.

Greg, another thing I noticed in your code.
I'm now under the impression that it would better most times to use:
For i = LBound(myArray) To UBound(myArray)
...not knowing how a potential users Option Base is set up.
Ed,

Yes I think I would do it something like that myself if the jerem
doesn't want the AutoText fields. That is if I would have
remembered InsertAutoText without seeing your code ;-)

I still think I would use the array though instead of having all of
the routines and passing variables:

Sub ScrathMacroII()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
myArray = Split("[A]|", "|") '[C] etc.
For i = 0 To UBound(myArray)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = myArray(i)
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End With
Next i
End Sub
 
E

Ed

Hi Russ,
It looks brilliant

That must be why I posted it twice - on the grounds that you can't have too
much of a good thing :-|

Regards.

Ed

Russ said:
Ed,
It looks brilliant to me!
Hello All,

I may be misunderstanding the requirement -- not for the first time :)
but would something along these lines work? ...

-------------------------------------------------
Sub Test()
ReplaceStuff ("[A]")
ReplaceStuff ("")
...
...
End Sub


Sub ReplaceStuff(pText As String)
Dim MyRange As Range
Dim FindString As String

FindString = Replace(Replace(pText, "[", "\["), "]", "\]")

With Selection.Find
Do While .Execute(FindText:=FindString, _
MatchWildcards:=True, _
Wrap:=wdFindContinue, _
Forward:=True) = True
Set MyRange = Selection.Range
MyRange.InsertAutoText
Loop
End With
End Sub
-------------------------------------------------

Regards.

Ed

jerem said:
No I need to replace a bunch of text [A], , etc. with autotext entries
named similarly.

Any help you can supply will be greatly appreciated but here was my response
to Greg so that you can get a fuller picture of what I need:

Downloaded the addin and it works fine. Originally I was trying to do my
replacements of all bracketed items, [A], , [C], etc. with a Find [A],
highlight all occurrences, Close and then hit the autotext entry to replace
all occurrences. However, that did not work because it would only replace
the first occurrence of [A] -this method works if you want to format all
occurrences (i.e., bold, italicize, underscore, etc.) but does not work to
put AutoText entries in. Your method is pretty novel and does just that. I
like it and I'm sure I will use it in the future, however, it does not solve
my current problem because I have about 30 instances of bracketed items in
about 10 documents. I want to be able to replace the bracketed items with
its corresponding Auto Text entries. Right now I am using this code:

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Dim i as Integer = 4
With Selection.Find
.Text = "[A]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT [A] ", PreserveFormatting:=True
Loop until i = 0
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[AB]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

and at each bracketed item where there is more than one to find and replace
I use a counter Dim i=(however number of entries for that bracketed item) and
at the end of the replace with autotext, I loop until i=0. This works
perfectly, however primitively, and goes through my document in 2 seconds
flat, however, I don't want to have to tell it how many occurrences there are
and it is probably not efficient code (as Graham Mayor mentions in one of his
responses and he hits the nail right on the head - I'm a beginner VBA
code-wise. I scavenge code where I can, try to make some sense of it, tweak
it to make it work for me and run with it, however efficient or inefficient.
The code you see above I'd gotten from the macro recorder in word (that's
where I try to get most of my VBA code from). Again, what I don't like
about the code above is that I have to tell it how many occurrences there are
of each bracketed item.

This code: Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll
I do like because it captures every occurrence without me having to say how
many occurences there are and without me having to set a counter, however, I
don't know how to say ReplaceWith an auto text entry. I've tried grabbing
this sentence from my original code -- Selection.Fields.Add
Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT [A] ", PreserveFormatting:=True -- but I get a message
something to the effect of improper argument????

So, all this boils down to is I need one sentence to replace
ReplaceWith:="hello" with ReplaceWith:=an autotext entry.

Thanks for your help.


:

You appear to be replacing with an Autotext Field rather than an Autotext
Entry?
The simpler method of replacing with a field is to copy the completed field
to the clipboard and then your searched item with ^c (clipboard content)
If you want to replace something with an *autotext entry* then you need the
macro at
http://www.gmayor.com/Autotext_replace.htm
For some reason (maybe to do with the problems he is having with his PC)
Greg finds an error when this code is run on Word 2003. If you find an error
let me know and in the meantime, change


If Application.Version = 12 Then
'Word 2007 - Use the Building Blocks Organizer
Dialogs(wdDialogBuildingBlockOrganizer).Show
sType = "Building Block"
Else
'Not Word 2007 - Use the Autotext dialog.
Dialogs(wdDialogEditAutoText).Show
sType = "Autotext"
End If


for
Dialogs(wdDialogEditAutoText).Show


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

jerem wrote:
Thanks Greg. I'll look that up. Please see my reply to some code
you gave me in my inquiry "Macro to insert file, pause, get
response". I tried it but very weird things were happening.

:

I'm trying to take this code and alter it - instead of replacing
text with text, I want to replace the text everywhere it appears in
the document with an AutoText entry so I want to substitute the
"hello" part of the ReplaceWith statement below with the AutoText
entry [A]:

Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll

I've tried to place in the ReplaceWith:="hello",
Replace:=wdReplaceAll area the following:

ReplaceWith:=wdFieldEmpty, Text:= _
"AUTOTEXT [A] ", PreserveFormatting:=False, _
Replace:=wdReplaceAll

but apparently VBA doesn't like my argument - tells me "Named
argument not found.

Any suggestions......

 
E

Ed

Hi Greg,
That is if I would have remembered InsertAutoText without seeing your code ;-)

Been there, done that, got whole rooms full of T-shirts :-(

Regards.

Ed

:



Ed,

Yes I think I would do it something like that myself if the jerem doesn't
want the AutoText fields. That is if I would have remembered InsertAutoText
without seeing your code ;-)

I still think I would use the array though instead of having all of the
routines and passing variables:

Sub ScrathMacroII()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
myArray = Split("[A]|", "|") '[C] etc.
For i = 0 To UBound(myArray)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = myArray(i)
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End With
Next i
End Sub

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Hello All,

I may be misunderstanding the requirement -- not for the first time
:) but would something along these lines work? ...

-------------------------------------------------
Sub Test()
ReplaceStuff ("[A]")
ReplaceStuff ("")
...
...
End Sub


Sub ReplaceStuff(pText As String)
Dim MyRange As Range
Dim FindString As String

FindString = Replace(Replace(pText, "[", "\["), "]", "\]")

With Selection.Find
Do While .Execute(FindText:=FindString, _
MatchWildcards:=True, _
Wrap:=wdFindContinue, _
Forward:=True) = True
Set MyRange = Selection.Range
MyRange.InsertAutoText
Loop
End With
End Sub
-------------------------------------------------

Regards.

Ed

jerem said:
No I need to replace a bunch of text [A], , etc. with autotext
entries named similarly.

Any help you can supply will be greatly appreciated but here was my
response to Greg so that you can get a fuller picture of what I need:

Downloaded the addin and it works fine. Originally I was trying to
do my replacements of all bracketed items, [A], , [C], etc. with
a Find [A], highlight all occurrences, Close and then hit the
autotext entry to replace all occurrences. However, that did not
work because it would only replace the first occurrence of [A] -this
method works if you want to format all occurrences (i.e., bold,
italicize, underscore, etc.) but does not work to put AutoText
entries in. Your method is pretty novel and does just that. I like
it and I'm sure I will use it in the future, however, it does not
solve my current problem because I have about 30 instances of
bracketed items in about 10 documents. I want to be able to replace
the bracketed items with its corresponding Auto Text entries. Right
now I am using this code:

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Dim i as Integer = 4
With Selection.Find
.Text = "[A]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _ "AUTOTEXT [A] ", PreserveFormatting:=True
Loop until i = 0
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[AB]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

and at each bracketed item where there is more than one to find and
replace I use a counter Dim i=(however number of entries for that
bracketed item) and at the end of the replace with autotext, I loop
until i=0. This works perfectly, however primitively, and goes
through my document in 2 seconds flat, however, I don't want to have
to tell it how many occurrences there are and it is probably not
efficient code (as Graham Mayor mentions in one of his responses and
he hits the nail right on the head - I'm a beginner VBA code-wise.
I scavenge code where I can, try to make some sense of it, tweak it
to make it work for me and run with it, however efficient or
inefficient. The code you see above I'd gotten from the macro
recorder in word (that's where I try to get most of my VBA code
from). Again, what I don't like about the code above is that I
have to tell it how many occurrences there are of each bracketed
item.

This code: Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll
I do like because it captures every occurrence without me having to
say how many occurences there are and without me having to set a
counter, however, I don't know how to say ReplaceWith an auto text
entry. I've tried grabbing this sentence from my original code --
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _ "AUTOTEXT [A] ", PreserveFormatting:=True -- but I
get a message
something to the effect of improper argument????

So, all this boils down to is I need one sentence to replace
ReplaceWith:="hello" with ReplaceWith:=an autotext entry.

Thanks for your help.


:

You appear to be replacing with an Autotext Field rather than an
Autotext Entry?
The simpler method of replacing with a field is to copy the
completed field to the clipboard and then your searched item with
^c (clipboard content)
If you want to replace something with an *autotext entry* then you
need the macro at
http://www.gmayor.com/Autotext_replace.htm
For some reason (maybe to do with the problems he is having with
his PC) Greg finds an error when this code is run on Word 2003. If
you find an error let me know and in the meantime, change


If Application.Version = 12 Then
'Word 2007 - Use the Building Blocks Organizer
Dialogs(wdDialogBuildingBlockOrganizer).Show
sType = "Building Block"
Else
'Not Word 2007 - Use the Autotext dialog.
Dialogs(wdDialogEditAutoText).Show
sType = "Autotext"
End If


for
Dialogs(wdDialogEditAutoText).Show


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

jerem wrote:
Thanks Greg. I'll look that up. Please see my reply to some code
you gave me in my inquiry "Macro to insert file, pause, get
response". I tried it but very weird things were happening.

:

I'm trying to take this code and alter it - instead of replacing
text with text, I want to replace the text everywhere it appears
in the document with an AutoText entry so I want to substitute the
"hello" part of the ReplaceWith statement below with the AutoText
entry [A]:

Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll

I've tried to place in the ReplaceWith:="hello",
Replace:=wdReplaceAll area the following:

ReplaceWith:=wdFieldEmpty, Text:= _
"AUTOTEXT [A] ", PreserveFormatting:=False, _
Replace:=wdReplaceAll

but apparently VBA doesn't like my argument - tells me "Named
argument not found.

Any suggestions......

 
J

jerem

By the way fellows: jerem is a she not a he. Common mistake.

Let me give more details because it seems that some of you fellows are
confused as to whether I want to replace [A], , etc. with autotext field
codes or the autotext field entries. I need to replace text within a
document- what text: [A], , [C], etc. with autotext entries, not autotext
field codes. Why are there [A], , [C], etc. dispersed throughout the
document. Because these represent areas that need to have variable
information inserted in these spots. Typically, this is handled by doing a
merge, a base document carrying the standard text merged with a variable
document housing the variables. Why is this not being handled with a merge?
Because sometimes documents that are handled with a merge tend to lock up,
appear checked out and become one pain in the ____ to get unlocked as well as
giving problems (when trying to blackline the document) breaching the
relationship between the base document and the variable document involved in
the merge. (I work with Imanage and whether this is an Imanage issue as far
as merges are concerned, I don't know but sometimes doing merges are, again
one big pain in the ___, sometimes they work smoothly). So, someone came up
with the idea of foregoing the merge and handling this with having one
document that has these tags in it, loading up the variable information in
autotext (and by the way, whoever came up with the automated Autotext loader
- kudos to them - love that program - I use that to load up my autotext
entries) and then manually replacing all of these tags with the autotext
entries which are similarly named - [A], , etc. This text - [A], ,
[C], etc. can appear in a document more than once. It is absurd for me to
have to select each one, call in the autotext entry then go to the next entry
and do the same. At one point I tried to Find [A], highlight all of them and
then hit the autotext entry. Does not work - it will only replace the first
highlighted [A]. Was not really married to that idea anyway because I would
still have to manually find each group (the [A]'s then the 's, etc.) It's
similar to your Find and Replace Autotext entries. If I use that I have to
find all groupings of [A]'s, then do 's and so on and so forth. Too
tedious. So I came up with this code:
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Dim i as Integer = 4
With Selection.Find
..Text = "[A]"
..Replacement.Text = ""
..Forward = True
..Wrap = wdFindContinue
..Format = False
..MatchCase = False
..MatchWholeWord = False
..MatchWildcards = False
..MatchSoundsLike = False
..MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT [A] ", PreserveFormatting:=True
Loop until i = 0

for each and every tag, which works perfectly and does so in 2 seconds flat.
What I didn't like about this was I had to tell it how many times each code
appears (since I didn't know how to loop this so it would keep finding and
replacing each tag) and then I came across this little bit of code: Set
myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll
but I didn't know how to replace the last sentence so that it would replace
the [A] with an autotext entry that was similarly named(not autotext field
code, just wanted to emphasize that once again). I replaced this last
sentence with the one that works in the previous code, but I keep getting
improper argument or something to that effect. Since everyone is suggesting
all kinds of other techniques, I take it that there is no sentence that can
replace the ReplaceWith:="hello", Replace:=wdReplaceAll that will allow the
replacement to be an autotext code. Is that correct? I do find that a
little curious, since my original, primitive (but effective code) does have a
sentence in it that allows for an autotext replacement. That primitive code
works beautifully replacing about 30 tags in a document in 2 second flat.

I'm going to try your array code and see if that does the trick but if
anyone is out there who knows that sentence I'm looking for, would appreciate
it. Thanks all.


Greg Maxey said:
Graham's and my method is the only one I know to replace text with and
AutoText entry. Actually it is just the AutoText entry pasted to the
clipboard and then the found text replaced with the clipboard contents.

Your method is replacing found text with an AutoText field. I suppose you
could adapt Grahams code to write the AutoText field on the the scratchpad,
copy it to the clipboard and then replace all with the clipboard contents.

And alternative would be to load all of your find text items into an array
and then process the array.

Sub ScrathMacro()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
Dim pStr As String
myArray = Split("[A]||[C]", "|") 'These are what you are looking for and
the corresponign AutoText entry names.
For i = 0 To UBound(myArray)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = myArray(i)
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
pStr = "AUTOTEXT " & myArray(i)
While .Execute
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
pStr, PreserveFormatting:=True
oRng.Collapse wdCollapseEnd
Wend
End With
Next i
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Downloaded the addin and it works fine. Originally I was trying to
do my replacements of all bracketed items, [A], , [C], etc. with a
Find [A], highlight all occurrences, Close and then hit the autotext
entry to replace all occurrences. However, that did not work because
it would only replace the first occurrence of [A] -this method works
if you want to format all occurrences (i.e., bold, italicize,
underscore, etc.) but does not work to put AutoText entries in. Your
method is pretty novel and does just that. I like it and I'm sure I
will use it in the future, however, it does not solve my current
problem because I have about 30 instances of bracketed items in about
10 documents. I want to be able to replace the bracketed items with
its corresponding Auto Text entries. Right now I am using this code:

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Dim i as Integer = 4
With Selection.Find
.Text = "[A]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _ "AUTOTEXT [A] ", PreserveFormatting:=True
Loop until i = 0
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[AB]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

and at each bracketed item where there is more than one to find and
replace I use a counter Dim i=(however number of entries for that
bracketed item) and at the end of the replace with autotext, I loop
until i=0. This works perfectly, however primitively, and goes
through my document in 2 seconds flat, however, I don't want to have
to tell it how many occurrences there are and it is probably not
efficient code (as Graham Mayor mentions in one of his responses and
he hits the nail right on the head - I'm a beginner VBA code-wise. I
scavenge code where I can, try to make some sense of it, tweak it to
make it work for me and run with it, however efficient or
inefficient. The code you see above I'd gotten from the macro
recorder in word (that's where I try to get most of my VBA code
from). Again, what I don't like about the code above is that I have
to tell it how many occurrences there are of each bracketed item.

This code: Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll
I do like because it captures every occurrence without me having to
say how many occurences there are and without me having to set a
counter, however, I don't know how to say ReplaceWith an auto text
entry. I've tried grabbing this sentence from my original code --
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _ "AUTOTEXT [A] ", PreserveFormatting:=True -- but I
get a message
something to the effect of improper argument????

So, all this boils down to is I need one sentence to replace
ReplaceWith:="hello" with ReplaceWith:=an autotext entry.

Thanks for your help.



Greg Maxey said:
OK, try downloading the addin now.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


jerem wrote:
Went to your VBA Find and Replace Website, but am getting a compile
error when trying to use it. Also, don't know if that will help me
because I need to accomplish about 30 differnet finds and replaces
and I don't want to have to do these individually. Just need the
proper VBA sentence that says ok, now that you've found all
occurrences of [A], replace each occurrence with the auto text [A]
entry and so on and so forth with the other 30 replacements.

:

Thanks Greg. I'll look that up. Please see my reply to some code
you gave me in my inquiry "Macro to insert file, pause, get
response". I tried it but very weird things were happening.

:

I'm trying to take this code and alter it - instead of replacing
text with text, I want to replace the text everywhere it appears
in the document with an AutoText entry so I want to substitute the
"hello" part of the ReplaceWith statement below with the AutoText
entry [A]:

Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll

I've tried to place in the ReplaceWith:="hello",
Replace:=wdReplaceAll area the following:

ReplaceWith:=wdFieldEmpty, Text:= _
"AUTOTEXT [A] ", PreserveFormatting:=False, _
Replace:=wdReplaceAll

but apparently VBA doesn't like my argument - tells me "Named
argument not found.

Any suggestions......

 
J

jerem

Jerem writes again:
By the way fellows: jerem is a she not a he. Common mistake.

Let me give more details because it seems that some of you fellows are
confused as to whether I want to replace [A], , etc. with autotext field
codes or the autotext field entries. I need to replace text within a
document- what text: [A], , [C], etc. with autotext entries, not autotext
field codes. Why are there [A], , [C], etc. dispersed throughout the
document. Because these represent areas that need to have variable
information inserted in these spots. Typically, this is handled by doing a
merge, a base document carrying the standard text merged with a variable
document housing the variables. Why is this not being handled with a merge?
Because sometimes documents that are handled with a merge tend to lock up,
appear checked out and become one pain in the ____ to get unlocked as well as
giving problems (when trying to blackline the document) breaching the
relationship between the base document and the variable document involved in
the merge. (I work with Imanage and whether this is an Imanage issue as far
as merges are concerned, I don't know but sometimes doing merges are, again
one big pain in the ___, sometimes they work smoothly). So, someone came up
with the idea of foregoing the merge and handling this with having one
document that has these tags in it, loading up the variable information in
autotext (and by the way, whoever came up with the automated Autotext loader
- kudos to them - love that program - I use that to load up my autotext
entries) and then manually replacing all of these tags with the autotext
entries which are similarly named - [A], , etc. This text - [A], ,
[C], etc. can appear in a document more than once. It is absurd for me to
have to select each one, call in the autotext entry then go to the next entry
and do the same. At one point I tried to Find [A], highlight all of them and
then hit the autotext entry. Does not work - it will only replace the first
highlighted [A]. Was not really married to that idea anyway because I would
still have to manually find each group (the [A]'s then the 's, etc.) It's
similar to your Find and Replace Autotext entries. If I use that I have to
find all groupings of [A]'s, then do 's and so on and so forth. Too
tedious. So I came up with this code:
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Dim i as Integer = 4
With Selection.Find
..Text = "[A]"
..Replacement.Text = ""
..Forward = True
..Wrap = wdFindContinue
..Format = False
..MatchCase = False
..MatchWholeWord = False
..MatchWildcards = False
..MatchSoundsLike = False
..MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT [A] ", PreserveFormatting:=True
Loop until i = 0

for each and every tag, which works perfectly and does so in 2 seconds flat.
What I didn't like about this was I had to tell it how many times each code
appears (since I didn't know how to loop this so it would keep finding and
replacing each tag) and then I came across this little bit of code: Set
myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll
but I didn't know how to replace the last sentence so that it would replace
the [A] with an autotext entry that was similarly named(not autotext field
code, just wanted to emphasize that once again). I replaced this last
sentence with the one that works in the previous code, but I keep getting
improper argument or something to that effect. Since everyone is suggesting
all kinds of other techniques, I take it that there is no sentence that can
replace the ReplaceWith:="hello", Replace:=wdReplaceAll that will allow the
replacement to be an autotext code. Is that correct? I do find that a
little curious, since my original, primitive (but effective code) does have a
sentence in it that allows for an autotext replacement. That primitive code
works beautifully replacing about 30 tags in a document in 2 second flat.

I'm going to try your array code and see if that does the trick but if
anyone is out there who knows that sentence I'm looking for, would appreciate
it. Thanks all.


jerem said:
I'm trying to take this code and alter it - instead of replacing text with
text, I want to replace the text everywhere it appears in the document with
an AutoText entry so I want to substitute the "hello" part of the ReplaceWith
statement below with the AutoText entry [A]:

Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll

I've tried to place in the ReplaceWith:="hello", Replace:=wdReplaceAll area
the following:

ReplaceWith:=wdFieldEmpty, Text:= _
"AUTOTEXT [A] ", PreserveFormatting:=False, _
Replace:=wdReplaceAll

but apparently VBA doesn't like my argument - tells me "Named argument not
found.

Any suggestions......
 
G

Graham Mayor

It sounds as thought this application will run and run, whereas if you were
to replace the entries A B C etc with IncludeText fields, you could use a
document to hold your variable information with bookmarked items that match
the bookmarks in the fields. Then you only have one document to maintain -
and the only danger there is that you will delete from it the bookmarks.

Alternatively you could put the variable information in individual
documents, which means more documents to maintain, but no bookmarks to lose.

If you I understand your original requirement correctly, you need Autotext
Fields rather than Entries or you will not be able to update them when you
change the autotext content?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

By the way fellows: jerem is a she not a he. Common mistake.

Let me give more details because it seems that some of you fellows are
confused as to whether I want to replace [A], , etc. with autotext
field codes or the autotext field entries. I need to replace text
within a document- what text: [A], , [C], etc. with autotext
entries, not autotext field codes. Why are there [A], , [C], etc.
dispersed throughout the document. Because these represent areas
that need to have variable information inserted in these spots.
Typically, this is handled by doing a merge, a base document carrying
the standard text merged with a variable document housing the
variables. Why is this not being handled with a merge? Because
sometimes documents that are handled with a merge tend to lock up,
appear checked out and become one pain in the ____ to get unlocked as
well as giving problems (when trying to blackline the document)
breaching the relationship between the base document and the variable
document involved in the merge. (I work with Imanage and whether
this is an Imanage issue as far as merges are concerned, I don't know
but sometimes doing merges are, again one big pain in the ___,
sometimes they work smoothly). So, someone came up with the idea of
foregoing the merge and handling this with having one document that
has these tags in it, loading up the variable information in autotext
(and by the way, whoever came up with the automated Autotext loader -
kudos to them - love that program - I use that to load up my autotext
entries) and then manually replacing all of these tags with the
autotext entries which are similarly named - [A], , etc. This
text - [A], , [C], etc. can appear in a document more than once.
It is absurd for me to have to select each one, call in the autotext
entry then go to the next entry and do the same. At one point I
tried to Find [A], highlight all of them and then hit the autotext
entry. Does not work - it will only replace the first highlighted
[A]. Was not really married to that idea anyway because I would
still have to manually find each group (the [A]'s then the 's,
etc.) It's similar to your Find and Replace Autotext entries. If I
use that I have to find all groupings of [A]'s, then do 's and so
on and so forth. Too tedious. So I came up with this code:
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Dim i as Integer = 4
With Selection.Find
.Text = "[A]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _ "AUTOTEXT [A] ", PreserveFormatting:=True
Loop until i = 0

for each and every tag, which works perfectly and does so in 2
seconds flat. What I didn't like about this was I had to tell it how
many times each code appears (since I didn't know how to loop this so
it would keep finding and replacing each tag) and then I came across
this little bit of code: Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll
but I didn't know how to replace the last sentence so that it would
replace the [A] with an autotext entry that was similarly named(not
autotext field code, just wanted to emphasize that once again). I
replaced this last sentence with the one that works in the previous
code, but I keep getting improper argument or something to that
effect. Since everyone is suggesting all kinds of other techniques,
I take it that there is no sentence that can replace the
ReplaceWith:="hello", Replace:=wdReplaceAll that will allow the
replacement to be an autotext code. Is that correct? I do find that
a little curious, since my original, primitive (but effective code)
does have a sentence in it that allows for an autotext replacement.
That primitive code works beautifully replacing about 30 tags in a
document in 2 second flat.

I'm going to try your array code and see if that does the trick but if
anyone is out there who knows that sentence I'm looking for, would
appreciate it. Thanks all.


Greg Maxey said:
Graham's and my method is the only one I know to replace text with
and AutoText entry. Actually it is just the AutoText entry pasted
to the clipboard and then the found text replaced with the clipboard
contents.

Your method is replacing found text with an AutoText field. I
suppose you could adapt Grahams code to write the AutoText field on
the the scratchpad, copy it to the clipboard and then replace all
with the clipboard contents.

And alternative would be to load all of your find text items into an
array and then process the array.

Sub ScrathMacro()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
Dim pStr As String
myArray = Split("[A]||[C]", "|") 'These are what you are looking
for and the corresponign AutoText entry names.
For i = 0 To UBound(myArray)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = myArray(i)
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
pStr = "AUTOTEXT " & myArray(i)
While .Execute
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
pStr, PreserveFormatting:=True
oRng.Collapse wdCollapseEnd
Wend
End With
Next i
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Downloaded the addin and it works fine. Originally I was trying to
do my replacements of all bracketed items, [A], , [C], etc. with
a Find [A], highlight all occurrences, Close and then hit the
autotext entry to replace all occurrences. However, that did not
work because it would only replace the first occurrence of [A]
-this method works if you want to format all occurrences (i.e.,
bold, italicize, underscore, etc.) but does not work to put
AutoText entries in. Your method is pretty novel and does just
that. I like it and I'm sure I will use it in the future, however,
it does not solve my current problem because I have about 30
instances of bracketed items in about 10 documents. I want to be
able to replace the bracketed items with its corresponding Auto
Text entries. Right now I am using this code:

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Dim i as Integer = 4
With Selection.Find
.Text = "[A]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _ "AUTOTEXT [A] ", PreserveFormatting:=True
Loop until i = 0
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[AB]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

and at each bracketed item where there is more than one to find and
replace I use a counter Dim i=(however number of entries for that
bracketed item) and at the end of the replace with autotext, I loop
until i=0. This works perfectly, however primitively, and goes
through my document in 2 seconds flat, however, I don't want to have
to tell it how many occurrences there are and it is probably not
efficient code (as Graham Mayor mentions in one of his responses and
he hits the nail right on the head - I'm a beginner VBA code-wise.
I scavenge code where I can, try to make some sense of it, tweak it
to make it work for me and run with it, however efficient or
inefficient. The code you see above I'd gotten from the macro
recorder in word (that's where I try to get most of my VBA code
from). Again, what I don't like about the code above is that I
have to tell it how many occurrences there are of each bracketed
item.

This code: Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll
I do like because it captures every occurrence without me having to
say how many occurences there are and without me having to set a
counter, however, I don't know how to say ReplaceWith an auto text
entry. I've tried grabbing this sentence from my original code --
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _ "AUTOTEXT [A] ", PreserveFormatting:=True -- but I
get a message
something to the effect of improper argument????

So, all this boils down to is I need one sentence to replace
ReplaceWith:="hello" with ReplaceWith:=an autotext entry.

Thanks for your help.



:

OK, try downloading the addin now.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


jerem wrote:
Went to your VBA Find and Replace Website, but am getting a
compile error when trying to use it. Also, don't know if that
will help me because I need to accomplish about 30 differnet
finds and replaces and I don't want to have to do these
individually. Just need the proper VBA sentence that says ok,
now that you've found all occurrences of [A], replace each
occurrence with the auto text [A] entry and so on and so forth
with the other 30 replacements.

:

Thanks Greg. I'll look that up. Please see my reply to some
code you gave me in my inquiry "Macro to insert file, pause, get
response". I tried it but very weird things were happening.

:

I'm trying to take this code and alter it - instead of replacing
text with text, I want to replace the text everywhere it appears
in the document with an AutoText entry so I want to substitute
the "hello" part of the ReplaceWith statement below with the
AutoText entry [A]:

Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll

I've tried to place in the ReplaceWith:="hello",
Replace:=wdReplaceAll area the following:

ReplaceWith:=wdFieldEmpty, Text:= _
"AUTOTEXT [A] ", PreserveFormatting:=False, _
Replace:=wdReplaceAll

but apparently VBA doesn't like my argument - tells me "Named
argument not found.

Any suggestions......
 
G

Greg Maxey

Jerem,

Sorry about the gender mix up.

Like I said in the post above, the only way I know of to use "Replace All"
is to use an adaptation of Graham's code and replace each found item with
the content of the clipboard. This seems to work (provided your AutoText
Entries are named the same as the text you want to fiind)>

Sub ReplaceWithAUTOTEXT()

Dim findText As String
Dim ReplaceText As String
Dim myArray As Variant
Dim i As Long

myArray = Split("[A]||[C]", "|")
'Create a scratch pad
For i = LBound(myArray) To UBound(myArray)
Documents.Add
NormalTemplate.AutoTextEntries(myArray(i)).Insert Where:=Selection.Range,
_
RichText:=True
Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
Selection.Cut
'crumple up scratch pad :)
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
'Replace found text with the clipboard contents.
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = myArray(i)
.Replacement.Text = "^c"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
Next i
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

jerem said:
By the way fellows: jerem is a she not a he. Common mistake.

Let me give more details because it seems that some of you fellows are
confused as to whether I want to replace [A], , etc. with autotext
field
codes or the autotext field entries. I need to replace text within a
document- what text: [A], , [C], etc. with autotext entries, not
autotext
field codes. Why are there [A], , [C], etc. dispersed throughout the
document. Because these represent areas that need to have variable
information inserted in these spots. Typically, this is handled by doing
a
merge, a base document carrying the standard text merged with a variable
document housing the variables. Why is this not being handled with a
merge?
Because sometimes documents that are handled with a merge tend to lock up,
appear checked out and become one pain in the ____ to get unlocked as well
as
giving problems (when trying to blackline the document) breaching the
relationship between the base document and the variable document involved
in
the merge. (I work with Imanage and whether this is an Imanage issue as
far
as merges are concerned, I don't know but sometimes doing merges are,
again
one big pain in the ___, sometimes they work smoothly). So, someone came
up
with the idea of foregoing the merge and handling this with having one
document that has these tags in it, loading up the variable information in
autotext (and by the way, whoever came up with the automated Autotext
loader
- kudos to them - love that program - I use that to load up my autotext
entries) and then manually replacing all of these tags with the autotext
entries which are similarly named - [A], , etc. This text - [A],
,
[C], etc. can appear in a document more than once. It is absurd for me to
have to select each one, call in the autotext entry then go to the next
entry
and do the same. At one point I tried to Find [A], highlight all of them
and
then hit the autotext entry. Does not work - it will only replace the
first
highlighted [A]. Was not really married to that idea anyway because I
would
still have to manually find each group (the [A]'s then the 's, etc.)
It's
similar to your Find and Replace Autotext entries. If I use that I have
to
find all groupings of [A]'s, then do 's and so on and so forth. Too
tedious. So I came up with this code:
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Dim i as Integer = 4
With Selection.Find
.Text = "[A]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT [A] ", PreserveFormatting:=True
Loop until i = 0

for each and every tag, which works perfectly and does so in 2 seconds
flat.
What I didn't like about this was I had to tell it how many times each
code
appears (since I didn't know how to loop this so it would keep finding and
replacing each tag) and then I came across this little bit of code: Set
myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll
but I didn't know how to replace the last sentence so that it would
replace
the [A] with an autotext entry that was similarly named(not autotext field
code, just wanted to emphasize that once again). I replaced this last
sentence with the one that works in the previous code, but I keep getting
improper argument or something to that effect. Since everyone is
suggesting
all kinds of other techniques, I take it that there is no sentence that
can
replace the ReplaceWith:="hello", Replace:=wdReplaceAll that will allow
the
replacement to be an autotext code. Is that correct? I do find that a
little curious, since my original, primitive (but effective code) does
have a
sentence in it that allows for an autotext replacement. That primitive
code
works beautifully replacing about 30 tags in a document in 2 second flat.

I'm going to try your array code and see if that does the trick but if
anyone is out there who knows that sentence I'm looking for, would
appreciate
it. Thanks all.


Greg Maxey said:
Graham's and my method is the only one I know to replace text with and
AutoText entry. Actually it is just the AutoText entry pasted to the
clipboard and then the found text replaced with the clipboard contents.

Your method is replacing found text with an AutoText field. I suppose
you
could adapt Grahams code to write the AutoText field on the the
scratchpad,
copy it to the clipboard and then replace all with the clipboard
contents.

And alternative would be to load all of your find text items into an
array
and then process the array.

Sub ScrathMacro()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
Dim pStr As String
myArray = Split("[A]||[C]", "|") 'These are what you are looking for
and
the corresponign AutoText entry names.
For i = 0 To UBound(myArray)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = myArray(i)
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
pStr = "AUTOTEXT " & myArray(i)
While .Execute
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
pStr, PreserveFormatting:=True
oRng.Collapse wdCollapseEnd
Wend
End With
Next i
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Downloaded the addin and it works fine. Originally I was trying to
do my replacements of all bracketed items, [A], , [C], etc. with a
Find [A], highlight all occurrences, Close and then hit the autotext
entry to replace all occurrences. However, that did not work because
it would only replace the first occurrence of [A] -this method works
if you want to format all occurrences (i.e., bold, italicize,
underscore, etc.) but does not work to put AutoText entries in. Your
method is pretty novel and does just that. I like it and I'm sure I
will use it in the future, however, it does not solve my current
problem because I have about 30 instances of bracketed items in about
10 documents. I want to be able to replace the bracketed items with
its corresponding Auto Text entries. Right now I am using this code:

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Dim i as Integer = 4
With Selection.Find
.Text = "[A]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _ "AUTOTEXT [A] ", PreserveFormatting:=True
Loop until i = 0
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[AB]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

and at each bracketed item where there is more than one to find and
replace I use a counter Dim i=(however number of entries for that
bracketed item) and at the end of the replace with autotext, I loop
until i=0. This works perfectly, however primitively, and goes
through my document in 2 seconds flat, however, I don't want to have
to tell it how many occurrences there are and it is probably not
efficient code (as Graham Mayor mentions in one of his responses and
he hits the nail right on the head - I'm a beginner VBA code-wise. I
scavenge code where I can, try to make some sense of it, tweak it to
make it work for me and run with it, however efficient or
inefficient. The code you see above I'd gotten from the macro
recorder in word (that's where I try to get most of my VBA code
from). Again, what I don't like about the code above is that I have
to tell it how many occurrences there are of each bracketed item.

This code: Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll
I do like because it captures every occurrence without me having to
say how many occurences there are and without me having to set a
counter, however, I don't know how to say ReplaceWith an auto text
entry. I've tried grabbing this sentence from my original code --
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _ "AUTOTEXT [A] ", PreserveFormatting:=True -- but I
get a message
something to the effect of improper argument????

So, all this boils down to is I need one sentence to replace
ReplaceWith:="hello" with ReplaceWith:=an autotext entry.

Thanks for your help.



:

OK, try downloading the addin now.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


jerem wrote:
Went to your VBA Find and Replace Website, but am getting a compile
error when trying to use it. Also, don't know if that will help me
because I need to accomplish about 30 differnet finds and replaces
and I don't want to have to do these individually. Just need the
proper VBA sentence that says ok, now that you've found all
occurrences of [A], replace each occurrence with the auto text [A]
entry and so on and so forth with the other 30 replacements.

:

Thanks Greg. I'll look that up. Please see my reply to some code
you gave me in my inquiry "Macro to insert file, pause, get
response". I tried it but very weird things were happening.

:

I'm trying to take this code and alter it - instead of replacing
text with text, I want to replace the text everywhere it appears
in the document with an AutoText entry so I want to substitute the
"hello" part of the ReplaceWith statement below with the AutoText
entry [A]:

Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll

I've tried to place in the ReplaceWith:="hello",
Replace:=wdReplaceAll area the following:

ReplaceWith:=wdFieldEmpty, Text:= _
"AUTOTEXT [A] ", PreserveFormatting:=False, _
Replace:=wdReplaceAll

but apparently VBA doesn't like my argument - tells me "Named
argument not found.

Any suggestions......

 
J

jerem

Okay Greg, I'm going to try this when I get back to work. I'll let you know
if I have any success -- not to belabor the issue but

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Dim i as Integer = 4
With Selection.Find
..Text = "[A]"
..Replacement.Text = ""
..Forward = True
..Wrap = wdFindContinue
..Format = False
..MatchCase = False
..MatchWholeWord = False
..MatchWildcards = False
..MatchSoundsLike = False
..MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT [A] ", PreserveFormatting:=True
Loop until i = 0
Selection.Find.ClearFormatting
Dim i as Integer = 2
With Selection.Find
..Text = ""
..Replacement.Text = ""
..Forward = True
..Wrap = wdFindContinue
..Format = False
..MatchCase = False
..MatchWholeWord = False
..MatchWildcards = False
..MatchSoundsLike = False
..MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT ", PreserveFormatting:=True
Loop until i = 0


Can you tell me leaving out both Dim statements and loop statements above
(that's what's driving this thing to keep finding all occurrences of [A] and
replacing it with similarly named autotext entries (again, not autotext codes
- I mention that again because in Graham's response once again he mentions
codes), then moving to , etc., etc. - how can I get this code to repeat
its function until it finishes all [A] replacements then get it to move onto
's, [C]'s, etc. So, using this code:

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
..Text = "[A]"
..Replacement.Text = ""
..Forward = True
..Wrap = wdFindContinue
..Format = False
..MatchCase = False
..MatchWholeWord = False
..MatchWildcards = False
..MatchSoundsLike = False
..MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT [A] ", PreserveFormatting:=True
LOOP UNTIL ALL THE [A]'S HAVE BEEN FOUND AND REPLACED
THEN MOVE ONTO THE NEXT SET OF INSTRUCTIONS

how can I get it to loop without using the Dim and loop statements - would
this work with While and Wend, and if so where would those statements appear
in the code? Again, I'm a novice so being specific is very helpful.

Also, I'm not going to give up on your set of code involving arrays - I will
try it, however, I don't know exactly what is going on in that code. I do
know exactly what my code is doing which gives me some sense of security.
Don't get me wrong - if yours works better - all the better. I'll use it,
but there are other times that I'll be needing a loop and I'd like to know
how to do it without setting up a counter. Thanks again fellas.


Greg Maxey said:
Jerem,

Sorry about the gender mix up.

Like I said in the post above, the only way I know of to use "Replace All"
is to use an adaptation of Graham's code and replace each found item with
the content of the clipboard. This seems to work (provided your AutoText
Entries are named the same as the text you want to fiind)>

Sub ReplaceWithAUTOTEXT()

Dim findText As String
Dim ReplaceText As String
Dim myArray As Variant
Dim i As Long

myArray = Split("[A]||[C]", "|")
'Create a scratch pad
For i = LBound(myArray) To UBound(myArray)
Documents.Add
NormalTemplate.AutoTextEntries(myArray(i)).Insert Where:=Selection.Range,
_
RichText:=True
Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
Selection.Cut
'crumple up scratch pad :)
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
'Replace found text with the clipboard contents.
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = myArray(i)
.Replacement.Text = "^c"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
Next i
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

jerem said:
By the way fellows: jerem is a she not a he. Common mistake.

Let me give more details because it seems that some of you fellows are
confused as to whether I want to replace [A], , etc. with autotext
field
codes or the autotext field entries. I need to replace text within a
document- what text: [A], , [C], etc. with autotext entries, not
autotext
field codes. Why are there [A], , [C], etc. dispersed throughout the
document. Because these represent areas that need to have variable
information inserted in these spots. Typically, this is handled by doing
a
merge, a base document carrying the standard text merged with a variable
document housing the variables. Why is this not being handled with a
merge?
Because sometimes documents that are handled with a merge tend to lock up,
appear checked out and become one pain in the ____ to get unlocked as well
as
giving problems (when trying to blackline the document) breaching the
relationship between the base document and the variable document involved
in
the merge. (I work with Imanage and whether this is an Imanage issue as
far
as merges are concerned, I don't know but sometimes doing merges are,
again
one big pain in the ___, sometimes they work smoothly). So, someone came
up
with the idea of foregoing the merge and handling this with having one
document that has these tags in it, loading up the variable information in
autotext (and by the way, whoever came up with the automated Autotext
loader
- kudos to them - love that program - I use that to load up my autotext
entries) and then manually replacing all of these tags with the autotext
entries which are similarly named - [A], , etc. This text - [A],
,
[C], etc. can appear in a document more than once. It is absurd for me to
have to select each one, call in the autotext entry then go to the next
entry
and do the same. At one point I tried to Find [A], highlight all of them
and
then hit the autotext entry. Does not work - it will only replace the
first
highlighted [A]. Was not really married to that idea anyway because I
would
still have to manually find each group (the [A]'s then the 's, etc.)
It's
similar to your Find and Replace Autotext entries. If I use that I have
to
find all groupings of [A]'s, then do 's and so on and so forth. Too
tedious. So I came up with this code:
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Dim i as Integer = 4
With Selection.Find
.Text = "[A]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT [A] ", PreserveFormatting:=True
Loop until i = 0

for each and every tag, which works perfectly and does so in 2 seconds
flat.
What I didn't like about this was I had to tell it how many times each
code
appears (since I didn't know how to loop this so it would keep finding and
replacing each tag) and then I came across this little bit of code: Set
myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll
but I didn't know how to replace the last sentence so that it would
replace
the [A] with an autotext entry that was similarly named(not autotext field
code, just wanted to emphasize that once again). I replaced this last
sentence with the one that works in the previous code, but I keep getting
improper argument or something to that effect. Since everyone is
suggesting
all kinds of other techniques, I take it that there is no sentence that
can
replace the ReplaceWith:="hello", Replace:=wdReplaceAll that will allow
the
replacement to be an autotext code. Is that correct? I do find that a
little curious, since my original, primitive (but effective code) does
have a
sentence in it that allows for an autotext replacement. That primitive
code
works beautifully replacing about 30 tags in a document in 2 second flat.

I'm going to try your array code and see if that does the trick but if
anyone is out there who knows that sentence I'm looking for, would
appreciate
it. Thanks all.


Greg Maxey said:
Graham's and my method is the only one I know to replace text with and
AutoText entry. Actually it is just the AutoText entry pasted to the
clipboard and then the found text replaced with the clipboard contents.

Your method is replacing found text with an AutoText field. I suppose
you
could adapt Grahams code to write the AutoText field on the the
scratchpad,
copy it to the clipboard and then replace all with the clipboard
contents.

And alternative would be to load all of your find text items into an
array
and then process the array.

Sub ScrathMacro()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
Dim pStr As String
myArray = Split("[A]||[C]", "|") 'These are what you are looking for
and
the corresponign AutoText entry names.
For i = 0 To UBound(myArray)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = myArray(i)
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
pStr = "AUTOTEXT " & myArray(i)
While .Execute
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
pStr, PreserveFormatting:=True
oRng.Collapse wdCollapseEnd
Wend
End With
Next i
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


jerem wrote:
Downloaded the addin and it works fine. Originally I was trying to
do my replacements of all bracketed items, [A], , [C], etc. with a
Find [A], highlight all occurrences, Close and then hit the autotext
entry to replace all occurrences. However, that did not work because
it would only replace the first occurrence of [A] -this method works
if you want to format all occurrences (i.e., bold, italicize,
underscore, etc.) but does not work to put AutoText entries in. Your
method is pretty novel and does just that. I like it and I'm sure I
will use it in the future, however, it does not solve my current
problem because I have about 30 instances of bracketed items in about
10 documents. I want to be able to replace the bracketed items with
its corresponding Auto Text entries. Right now I am using this code:

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Dim i as Integer = 4
With Selection.Find
.Text = "[A]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _ "AUTOTEXT [A] ", PreserveFormatting:=True
Loop until i = 0
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[AB]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

and at each bracketed item where there is more than one to find and
replace I use a counter Dim i=(however number of entries for that
bracketed item) and at the end of the replace with autotext, I loop
until i=0. This works perfectly, however primitively, and goes
through my document in 2 seconds flat, however, I don't want to have
to tell it how many occurrences there are and it is probably not
efficient code (as Graham Mayor mentions in one of his responses and
he hits the nail right on the head - I'm a beginner VBA code-wise. I
scavenge code where I can, try to make some sense of it, tweak it to
make it work for me and run with it, however efficient or
inefficient. The code you see above I'd gotten from the macro
recorder in word (that's where I try to get most of my VBA code
from). Again, what I don't like about the code above is that I have
to tell it how many occurrences there are of each bracketed item.

This code: Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll
I do like because it captures every occurrence without me having to
say how many occurences there are and without me having to set a
counter, however, I don't know how to say ReplaceWith an auto text
entry. I've tried grabbing this sentence from my original code --
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _ "AUTOTEXT [A] ", PreserveFormatting:=True -- but I
get a message
something to the effect of improper argument????

So, all this boils down to is I need one sentence to replace
ReplaceWith:="hello" with ReplaceWith:=an autotext entry.

Thanks for your help.



:

OK, try downloading the addin now.

--
 
G

Greg Maxey

Jerem,

You are the one that keeps denying the use of AutoText fields but in both
your code examples below that is exactly what you are using. To prove it to
you run your code and after the replacements are made toggle field codes and
you will see something like this in your document each place the replacement
took place:

{ AUTOTEXT [A] }

Those are AUTOTEXT fields. The reason you might want to use them is lets
say that after you have run your code you decide to change the AutoText [A]
from "Jerem is a man" to "Jerem is a woman"

If you did, then you would only have to select the document and update the
fields. Otherwise you would have to run another find and replace routine
find: Jerem is a man and replace with: Jerem is a woman.

I assume that with your dim i and loop statements that you already know that
there are 4 [A] entries and 2 entries. If the goal is to replace all
[A] entries when you don't know how many there are then you don't want to
use a Do/Loop at all.

Are you sure your code as written is actually working. You have two Loops
statements with no preceding Do statement. I doesn't work here.


A simple example of code that you could use to replace all [A] entries with
the AutoText [A] (just the text and not the field code) is:

Sub Test()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[A]"
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub

Look up InsertAutoText in VBA help. As it will only work if you have an
AutoText entry named [A]

If you wanted to do [A] and then you could just add more code like:

Sub Test()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[A]"
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
.Text = ""
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub

Or you could just load [A] and and [C] and so on into an array and then
run the find and replace part of the routine for each member of the array:

Sub Test2()
Dim oRng As Word.Range
Dim myArray As Variant
Dim i As Long
Set oRng = ActiveDocument.Range
myArray = Split("[A]||[C]|[D]", "|") 'Notice the "|" character separating
each member of the array
For i = LBound(myArray) To UBound(myArray)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = myArray(i)
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End With
Next i
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Okay Greg, I'm going to try this when I get back to work. I'll let
you know if I have any success -- not to belabor the issue but

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Dim i as Integer = 4
With Selection.Find
.Text = "[A]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _ "AUTOTEXT [A] ", PreserveFormatting:=True
Loop until i = 0
Selection.Find.ClearFormatting
Dim i as Integer = 2
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _ "AUTOTEXT ", PreserveFormatting:=True
Loop until i = 0


Can you tell me leaving out both Dim statements and loop statements
above (that's what's driving this thing to keep finding all
occurrences of [A] and replacing it with similarly named autotext
entries (again, not autotext codes - I mention that again because in
Graham's response once again he mentions codes), then moving to ,
etc., etc. - how can I get this code to repeat its function until it
finishes all [A] replacements then get it to move onto 's, [C]'s,
etc. So, using this code:

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[A]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _ "AUTOTEXT [A] ", PreserveFormatting:=True
LOOP UNTIL ALL THE [A]'S HAVE BEEN FOUND AND REPLACED
THEN MOVE ONTO THE NEXT SET OF INSTRUCTIONS

how can I get it to loop without using the Dim and loop statements -
would this work with While and Wend, and if so where would those
statements appear in the code? Again, I'm a novice so being specific
is very helpful.

Also, I'm not going to give up on your set of code involving arrays -
I will try it, however, I don't know exactly what is going on in that
code. I do know exactly what my code is doing which gives me some
sense of security. Don't get me wrong - if yours works better - all
the better. I'll use it, but there are other times that I'll be
needing a loop and I'd like to know how to do it without setting up a
counter. Thanks again fellas.


Greg Maxey said:
Jerem,

Sorry about the gender mix up.

Like I said in the post above, the only way I know of to use
"Replace All" is to use an adaptation of Graham's code and replace
each found item with the content of the clipboard. This seems to
work (provided your AutoText Entries are named the same as the text
you want to fiind)>

Sub ReplaceWithAUTOTEXT()

Dim findText As String
Dim ReplaceText As String
Dim myArray As Variant
Dim i As Long

myArray = Split("[A]||[C]", "|")
'Create a scratch pad
For i = LBound(myArray) To UBound(myArray)
Documents.Add
NormalTemplate.AutoTextEntries(myArray(i)).Insert
Where:=Selection.Range, _
RichText:=True
Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
Selection.Cut
'crumple up scratch pad :)
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
'Replace found text with the clipboard contents.
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = myArray(i)
.Replacement.Text = "^c"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
Next i
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

jerem said:
By the way fellows: jerem is a she not a he. Common mistake.

Let me give more details because it seems that some of you fellows
are confused as to whether I want to replace [A], , etc. with
autotext field
codes or the autotext field entries. I need to replace text within
a document- what text: [A], , [C], etc. with autotext entries,
not autotext
field codes. Why are there [A], , [C], etc. dispersed
throughout the document. Because these represent areas that need
to have variable information inserted in these spots. Typically,
this is handled by doing a
merge, a base document carrying the standard text merged with a
variable document housing the variables. Why is this not being
handled with a merge?
Because sometimes documents that are handled with a merge tend to
lock up, appear checked out and become one pain in the ____ to get
unlocked as well as
giving problems (when trying to blackline the document) breaching
the relationship between the base document and the variable
document involved in
the merge. (I work with Imanage and whether this is an Imanage
issue as far
as merges are concerned, I don't know but sometimes doing merges
are, again
one big pain in the ___, sometimes they work smoothly). So,
someone came up
with the idea of foregoing the merge and handling this with having
one document that has these tags in it, loading up the variable
information in autotext (and by the way, whoever came up with the
automated Autotext loader
- kudos to them - love that program - I use that to load up my
autotext entries) and then manually replacing all of these tags
with the autotext entries which are similarly named - [A], ,
etc. This text - [A], ,
[C], etc. can appear in a document more than once. It is absurd
for me to have to select each one, call in the autotext entry then
go to the next entry
and do the same. At one point I tried to Find [A], highlight all
of them and
then hit the autotext entry. Does not work - it will only replace
the first
highlighted [A]. Was not really married to that idea anyway
because I would
still have to manually find each group (the [A]'s then the 's,
etc.) It's
similar to your Find and Replace Autotext entries. If I use that I
have to
find all groupings of [A]'s, then do 's and so on and so forth.
Too tedious. So I came up with this code:
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Dim i as Integer = 4
With Selection.Find
.Text = "[A]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _ "AUTOTEXT [A] ", PreserveFormatting:=True
Loop until i = 0

for each and every tag, which works perfectly and does so in 2
seconds flat.
What I didn't like about this was I had to tell it how many times
each code
appears (since I didn't know how to loop this so it would keep
finding and replacing each tag) and then I came across this little
bit of code: Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll
but I didn't know how to replace the last sentence so that it would
replace
the [A] with an autotext entry that was similarly named(not
autotext field code, just wanted to emphasize that once again). I
replaced this last sentence with the one that works in the previous
code, but I keep getting improper argument or something to that
effect. Since everyone is suggesting
all kinds of other techniques, I take it that there is no sentence
that can
replace the ReplaceWith:="hello", Replace:=wdReplaceAll that will
allow the
replacement to be an autotext code. Is that correct? I do find
that a little curious, since my original, primitive (but effective
code) does have a
sentence in it that allows for an autotext replacement. That
primitive code
works beautifully replacing about 30 tags in a document in 2 second
flat.

I'm going to try your array code and see if that does the trick but
if anyone is out there who knows that sentence I'm looking for,
would appreciate
it. Thanks all.


:

Graham's and my method is the only one I know to replace text with
and AutoText entry. Actually it is just the AutoText entry pasted
to the clipboard and then the found text replaced with the
clipboard contents.

Your method is replacing found text with an AutoText field. I
suppose you
could adapt Grahams code to write the AutoText field on the the
scratchpad,
copy it to the clipboard and then replace all with the clipboard
contents.

And alternative would be to load all of your find text items into
an array
and then process the array.

Sub ScrathMacro()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
Dim pStr As String
myArray = Split("[A]||[C]", "|") 'These are what you are
looking for and
the corresponign AutoText entry names.
For i = 0 To UBound(myArray)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = myArray(i)
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
pStr = "AUTOTEXT " & myArray(i)
While .Execute
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
pStr, PreserveFormatting:=True
oRng.Collapse wdCollapseEnd
Wend
End With
Next i
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


jerem wrote:
Downloaded the addin and it works fine. Originally I was trying
to do my replacements of all bracketed items, [A], , [C], etc.
with a Find [A], highlight all occurrences, Close and then hit
the autotext entry to replace all occurrences. However, that did
not work because it would only replace the first occurrence of
[A] -this method works if you want to format all occurrences
(i.e., bold, italicize, underscore, etc.) but does not work to
put AutoText entries in. Your method is pretty novel and does
just that. I like it and I'm sure I will use it in the future,
however, it does not solve my current problem because I have
about 30 instances of bracketed items in about 10 documents. I
want to be able to replace the bracketed items with its
corresponding Auto Text entries. Right now I am using this code:

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Dim i as Integer = 4
With Selection.Find
.Text = "[A]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range,
Type:=wdFieldEmpty, Text:= _ "AUTOTEXT [A] ",
PreserveFormatting:=True
Loop until i = 0
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[AB]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

and at each bracketed item where there is more than one to find
and replace I use a counter Dim i=(however number of entries for
that bracketed item) and at the end of the replace with autotext,
I loop until i=0. This works perfectly, however primitively,
and goes through my document in 2 seconds flat, however, I don't
want to have to tell it how many occurrences there are and it is
probably not efficient code (as Graham Mayor mentions in one of
his responses and he hits the nail right on the head - I'm a
beginner VBA code-wise. I scavenge code where I can, try to make
some sense of it, tweak it to make it work for me and run with
it, however efficient or inefficient. The code you see above I'd
gotten from the macro recorder in word (that's where I try to get
most of my VBA code from). Again, what I don't like about the
code above is that I have to tell it how many occurrences there
are of each bracketed item.

This code: Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll
I do like because it captures every occurrence without me having
to say how many occurences there are and without me having to set
a counter, however, I don't know how to say ReplaceWith an auto
text entry. I've tried grabbing this sentence from my original
code -- Selection.Fields.Add Range:=Selection.Range,
Type:=wdFieldEmpty, Text:= _ "AUTOTEXT [A] ",
PreserveFormatting:=True -- but I
get a message
something to the effect of improper argument????

So, all this boils down to is I need one sentence to replace
ReplaceWith:="hello" with ReplaceWith:=an autotext entry.

Thanks for your help.



:

OK, try downloading the addin now.

--
 
R

Russ

Jerem,

Anytime you can use a wdReplaceAll, you can speed things up because that
find and replace is more hard-coded or compiled and faster than interpreting
each line in an interrupted loop.
While re-reading your posts, you mentioned a quest for a single line of code
to do a find and replace for the autotext.
Well, I found one way to do it, and incorporated that method into Greg's
outer loop to go through all the autotext 'names'.
It will throw up an error if you give it an autotext 'name' that doesn't
exist as part of the AutoTextEntries collection, but doesn't complain if it
can't find a legitimate name while searching the main body of text.
Again this assumes that the text to search for is the same as an autotext
'name'.

Sub Test2()
Dim oRng As Word.Range
Dim myArray As Variant
Dim i As Long

myArray = Split("[A]||[C]|[D]", "|") 'Notice the "|" character _
separating each member of the array
For i = LBound(myArray) To UBound(myArray)
Set oRng = ActiveDocument.Range
oRng.Find.Execute FindText:=myArray(i), _
ReplaceWith:=NormalTemplate.AutoTextEntries(myArray(i)).Value, _
Replace:=wdReplaceAll
Next i
End Sub
Jerem,

You are the one that keeps denying the use of AutoText fields but in both
your code examples below that is exactly what you are using. To prove it to
you run your code and after the replacements are made toggle field codes and
you will see something like this in your document each place the replacement
took place:

{ AUTOTEXT [A] }

Those are AUTOTEXT fields. The reason you might want to use them is lets
say that after you have run your code you decide to change the AutoText [A]
from "Jerem is a man" to "Jerem is a woman"

If you did, then you would only have to select the document and update the
fields. Otherwise you would have to run another find and replace routine
find: Jerem is a man and replace with: Jerem is a woman.

I assume that with your dim i and loop statements that you already know that
there are 4 [A] entries and 2 entries. If the goal is to replace all
[A] entries when you don't know how many there are then you don't want to
use a Do/Loop at all.

Are you sure your code as written is actually working. You have two Loops
statements with no preceding Do statement. I doesn't work here.


A simple example of code that you could use to replace all [A] entries with
the AutoText [A] (just the text and not the field code) is:

Sub Test()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[A]"
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub

Look up InsertAutoText in VBA help. As it will only work if you have an
AutoText entry named [A]

If you wanted to do [A] and then you could just add more code like:

Sub Test()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[A]"
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
.Text = ""
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub

Or you could just load [A] and and [C] and so on into an array and then
run the find and replace part of the routine for each member of the array:

Sub Test2()
Dim oRng As Word.Range
Dim myArray As Variant
Dim i As Long
Set oRng = ActiveDocument.Range
myArray = Split("[A]||[C]|[D]", "|") 'Notice the "|" character separating
each member of the array
For i = LBound(myArray) To UBound(myArray)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = myArray(i)
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End With
Next i
End Sub
 
G

Greg Maxey

Russ,

Sometimes one can't see the tree for the forest ;-)

I might change NormalTemplate to ActiveDocument.AttachedTemplage.

Unfortunately, simple solutions like this are more complex in Word2007. In
addition to the attached template autotext entries there are the quickparts
and buildingblocks template :-(.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Russ said:
Jerem,

Anytime you can use a wdReplaceAll, you can speed things up because that
find and replace is more hard-coded or compiled and faster than
interpreting
each line in an interrupted loop.
While re-reading your posts, you mentioned a quest for a single line of
code
to do a find and replace for the autotext.
Well, I found one way to do it, and incorporated that method into Greg's
outer loop to go through all the autotext 'names'.
It will throw up an error if you give it an autotext 'name' that doesn't
exist as part of the AutoTextEntries collection, but doesn't complain if
it
can't find a legitimate name while searching the main body of text.
Again this assumes that the text to search for is the same as an autotext
'name'.

Sub Test2()
Dim oRng As Word.Range
Dim myArray As Variant
Dim i As Long

myArray = Split("[A]||[C]|[D]", "|") 'Notice the "|" character _
separating each member of the array
For i = LBound(myArray) To UBound(myArray)
Set oRng = ActiveDocument.Range
oRng.Find.Execute FindText:=myArray(i), _
ReplaceWith:=NormalTemplate.AutoTextEntries(myArray(i)).Value, _
Replace:=wdReplaceAll
Next i
End Sub
Jerem,

You are the one that keeps denying the use of AutoText fields but in both
your code examples below that is exactly what you are using. To prove it
to
you run your code and after the replacements are made toggle field codes
and
you will see something like this in your document each place the
replacement
took place:

{ AUTOTEXT [A] }

Those are AUTOTEXT fields. The reason you might want to use them is lets
say that after you have run your code you decide to change the AutoText
[A]
from "Jerem is a man" to "Jerem is a woman"

If you did, then you would only have to select the document and update
the
fields. Otherwise you would have to run another find and replace routine
find: Jerem is a man and replace with: Jerem is a woman.

I assume that with your dim i and loop statements that you already know
that
there are 4 [A] entries and 2 entries. If the goal is to replace all
[A] entries when you don't know how many there are then you don't want to
use a Do/Loop at all.

Are you sure your code as written is actually working. You have two
Loops
statements with no preceding Do statement. I doesn't work here.


A simple example of code that you could use to replace all [A] entries
with
the AutoText [A] (just the text and not the field code) is:

Sub Test()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[A]"
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub

Look up InsertAutoText in VBA help. As it will only work if you have an
AutoText entry named [A]

If you wanted to do [A] and then you could just add more code like:

Sub Test()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[A]"
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
.Text = ""
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub

Or you could just load [A] and and [C] and so on into an array and
then
run the find and replace part of the routine for each member of the
array:

Sub Test2()
Dim oRng As Word.Range
Dim myArray As Variant
Dim i As Long
Set oRng = ActiveDocument.Range
myArray = Split("[A]||[C]|[D]", "|") 'Notice the "|" character
separating
each member of the array
For i = LBound(myArray) To UBound(myArray)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = myArray(i)
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End With
Next i
End Sub

 
R

Russ

Jerem et al,
I just thought about formatting and with further testing was disappointed to
find that replacing with .AutoTextEntries(myArray(i)).Value does not bring
over any formatting. :-(
So it doesn't look like we can use wdReplaceAll without losing the benefit
of replacing with formatted text.
Russ,

Sometimes one can't see the tree for the forest ;-)

I might change NormalTemplate to ActiveDocument.AttachedTemplage.

Unfortunately, simple solutions like this are more complex in Word2007. In
addition to the attached template autotext entries there are the quickparts
and buildingblocks template :-(.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Russ said:
Jerem,

Anytime you can use a wdReplaceAll, you can speed things up because that
find and replace is more hard-coded or compiled and faster than
interpreting
each line in an interrupted loop.
While re-reading your posts, you mentioned a quest for a single line of
code
to do a find and replace for the autotext.
Well, I found one way to do it, and incorporated that method into Greg's
outer loop to go through all the autotext 'names'.
It will throw up an error if you give it an autotext 'name' that doesn't
exist as part of the AutoTextEntries collection, but doesn't complain if
it
can't find a legitimate name while searching the main body of text.
Again this assumes that the text to search for is the same as an autotext
'name'.

Sub Test2()
Dim oRng As Word.Range
Dim myArray As Variant
Dim i As Long

myArray = Split("[A]||[C]|[D]", "|") 'Notice the "|" character _
separating each member of the array
For i = LBound(myArray) To UBound(myArray)
Set oRng = ActiveDocument.Range
oRng.Find.Execute FindText:=myArray(i), _
ReplaceWith:=NormalTemplate.AutoTextEntries(myArray(i)).Value, _
Replace:=wdReplaceAll
Next i
End Sub
Jerem,

You are the one that keeps denying the use of AutoText fields but in both
your code examples below that is exactly what you are using. To prove it
to
you run your code and after the replacements are made toggle field codes
and
you will see something like this in your document each place the
replacement
took place:

{ AUTOTEXT [A] }

Those are AUTOTEXT fields. The reason you might want to use them is lets
say that after you have run your code you decide to change the AutoText
[A]
from "Jerem is a man" to "Jerem is a woman"

If you did, then you would only have to select the document and update
the
fields. Otherwise you would have to run another find and replace routine
find: Jerem is a man and replace with: Jerem is a woman.

I assume that with your dim i and loop statements that you already know
that
there are 4 [A] entries and 2 entries. If the goal is to replace all
[A] entries when you don't know how many there are then you don't want to
use a Do/Loop at all.

Are you sure your code as written is actually working. You have two
Loops
statements with no preceding Do statement. I doesn't work here.


A simple example of code that you could use to replace all [A] entries
with
the AutoText [A] (just the text and not the field code) is:

Sub Test()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[A]"
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub

Look up InsertAutoText in VBA help. As it will only work if you have an
AutoText entry named [A]

If you wanted to do [A] and then you could just add more code like:

Sub Test()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[A]"
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
.Text = ""
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub

Or you could just load [A] and and [C] and so on into an array and
then
run the find and replace part of the routine for each member of the
array:

Sub Test2()
Dim oRng As Word.Range
Dim myArray As Variant
Dim i As Long
Set oRng = ActiveDocument.Range
myArray = Split("[A]||[C]|[D]", "|") 'Notice the "|" character
separating
each member of the array
For i = LBound(myArray) To UBound(myArray)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = myArray(i)
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End With
Next i
End Sub


 

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