Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2007

Tip: Looking for answers? Try searching our database.

Replacing Found Text with AutoText Entry

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jerem - 25 Aug 2007 04:08 GMT
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......
Greg Maxey - 25 Aug 2007 04:19 GMT
Code for doing that is in my VBA Find and Replace AddIn:
http://gregmaxey.mvps.org/VBA_Find_And_Replace.htm

or a stand alone macro:
http://www.gmayor.com/Autotext_replace.htm

Signature

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

> 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
[quoted text clipped - 16 lines]
>
> Any suggestions......
jerem - 25 Aug 2007 04:50 GMT
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
[quoted text clipped - 16 lines]
>
> Any suggestions......
jerem - 25 Aug 2007 05:06 GMT
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
[quoted text clipped - 20 lines]
> >
> > Any suggestions......
Greg Maxey - 25 Aug 2007 05:48 GMT
Disrcard last post.  I am having trouble saving files tonight.  I will post
a fix on my site in a few minutes.

Signature

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

> 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
[quoted text clipped - 29 lines]
>>>
>>> Any suggestions......
Greg Maxey - 25 Aug 2007 06:01 GMT
OK, try downloading the addin now.

Signature

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

> 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
[quoted text clipped - 29 lines]
>>>
>>> Any suggestions......
jerem - 25 Aug 2007 18:36 GMT
Downloaded the addin and it works fine.  Originally I was trying to do my
replacements of all bracketed items, [A], [B], [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.
>
[quoted text clipped - 31 lines]
> >>>
> >>> Any suggestions......
Greg Maxey - 25 Aug 2007 19:24 GMT
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]|[B]|[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

Signature

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], [B], [C], etc. with a
[quoted text clipped - 115 lines]
>>>>>
>>>>> Any suggestions......
jerem - 27 Aug 2007 03:36 GMT
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], [B], etc. with autotext field
codes or the autotext field entries.  I need to replace text within a
document- what text: [A], [B], [C], etc. with autotext entries, not autotext
field codes.  Why are there [A], [B], [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], [B], etc.    This text - [A], [B],
[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 [B]'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 [B]'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
[quoted text clipped - 156 lines]
> >>>>>
> >>>>> Any suggestions......
Graham Mayor - 27 Aug 2007 07:00 GMT
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?

Signature

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> By the way fellows:  jerem is a she not a he.  Common mistake.
>
[quoted text clipped - 248 lines]
>>>>>>>
>>>>>>> Any suggestions......
Greg Maxey - 27 Aug 2007 12:29 GMT
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]|[B]|[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

Signature

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

> By the way fellows:  jerem is a she not a he.  Common mistake.
>
[quoted text clipped - 263 lines]
>> >>>>>
>> >>>>> Any suggestions......
jerem - 27 Aug 2007 21:36 GMT
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 = "[B]"
.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 [B] ", 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 [B], etc., etc. - how can I get this code to repeat
its function until it finishes all [A] replacements then get it to move onto
[B]'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.

> Jerem,
>
[quoted text clipped - 271 lines]
> >> >>
> >> >> --
Greg Maxey - 27 Aug 2007 23:06 GMT
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 [B] 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 [B] 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 = "[B]"
 While .Execute
   oRng.InsertAutoText
   oRng.Collapse wdCollapseEnd
 Wend
End With
End Sub

Or you could just load [A] and [B] 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]|[B]|[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

Signature

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
[quoted text clipped - 373 lines]
>>>>>>
>>>>>> --
Russ - 28 Aug 2007 06:51 GMT
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]|[B]|[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,
>
[quoted text clipped - 80 lines]
> Next i
> End Sub

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Greg Maxey - 28 Aug 2007 12:09 GMT
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 :-(.

Signature

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

> Jerem,
>
[quoted text clipped - 124 lines]
>> Next i
>> End Sub
Russ - 28 Aug 2007 18:15 GMT
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,
>
[quoted text clipped - 134 lines]
>>> Next i
>>> End Sub

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

jerem - 25 Aug 2007 18:42 GMT
One more thing:  again, I like your macro, but if I use it for my problem
I'll have to do each [A], [B], [C], etc. individually and I want a one sweep
method.  I meant to point that out in my previous response.

> OK, try downloading the addin now.
>
[quoted text clipped - 31 lines]
> >>>
> >>> Any suggestions......
Graham Mayor - 25 Aug 2007 07:44 GMT
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

Signature

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> 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
[quoted text clipped - 21 lines]
>>
>> Any suggestions......
Greg Maxey - 25 Aug 2007 12:56 GMT
Graham,

I found what was causing the error in Word2003.  I use the "Option Explicit"
statement at the top of all of my VBA projects.  That is what causes the
complilation error to be thrown on wdDialogsBuildingBlocks.  I suppose one
could just leave that out in this case or use:

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

Then after the End Sub add:

Function GetDialog () As String
 GetDialog - wdDialogBuildingBlockOrganizer
End Function
Signature

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

> You appear to be replacing with an Autotext Field rather than an
> Autotext Entry?
[quoted text clipped - 46 lines]
>>>
>>> Any suggestions......
Graham Mayor - 25 Aug 2007 15:17 GMT
I see your point - but I wonder if this would confuse beginners even more.
Experienced users probably wouldn't need the web page? There were however a
couple of insignificant bugs in my code which I have now finished. I'll
think further about including your suggested workaround :)

Signature

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Graham,
>
[quoted text clipped - 69 lines]
>>>>
>>>> Any suggestions......
Graham Mayor - 25 Aug 2007 15:38 GMT
OK I've thought - and updated the web page ;)

Signature

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> I see your point - but I wonder if this would confuse beginners even
> more. Experienced users probably wouldn't need the web page? There
[quoted text clipped - 75 lines]
>>>>>
>>>>> Any suggestions......
jerem - 25 Aug 2007 18:40 GMT
No I need to replace a bunch of text [A], [B], 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], [B], [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?
[quoted text clipped - 45 lines]
> >>
> >> Any suggestions......
Ed - 25 Aug 2007 20:34 GMT
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 ("[B]")
  ...
  ...
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

-----------------------------------------------------

> No I need to replace a bunch of text [A], [B], etc. with autotext entries
> named similarly.
[quoted text clipped - 126 lines]
> > >>
> > >> Any suggestions......
Ed - 25 Aug 2007 20:36 GMT
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 ("[B]")
  ...
  ...
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

> No I need to replace a bunch of text [A], [B], etc. with autotext entries
> named similarly.
[quoted text clipped - 126 lines]
> > >>
> > >> Any suggestions......
Russ - 25 Aug 2007 22:10 GMT
Ed,
It looks brilliant to me!

> Hello All,
>
[quoted text clipped - 161 lines]
>>>>>
>>>>> Any suggestions......

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Ed - 26 Aug 2007 18:32 GMT
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

> Ed,
> It looks brilliant to me!
[quoted text clipped - 164 lines]
> >>>>>
> >>>>> Any suggestions......
Greg Maxey - 25 Aug 2007 22:25 GMT
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]|[B]", "|") '[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

Signature

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

> Hello All,
>
[quoted text clipped - 175 lines]
>>>>>
>>>>> Any suggestions......
Russ - 25 Aug 2007 22:54 GMT
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,
>
[quoted text clipped - 24 lines]
> Next i
> End Sub

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 25 Aug 2007 23:17 GMT
A small tweak:
Also it might be simpler and faster to use the Array() function:
myArray = Array("[A]","[B]"...)

> Greg,
> You're right. Does Jerem really need to insert fields?
[quoted text clipped - 36 lines]
>> Next i
>> End Sub

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Greg Maxey - 25 Aug 2007 23:52 GMT
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 ;-)

Signature

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?
[quoted text clipped - 37 lines]
>> Next i
>> End Sub
Ed - 26 Aug 2007 18:34 GMT
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,
>
[quoted text clipped - 204 lines]
> >>>>>
> >>>>> Any suggestions......
jerem - 27 Aug 2007 04:02 GMT
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], [B], etc. with autotext field
codes or the autotext field entries. I need to replace text within a
document- what text: [A], [B], [C], etc. with autotext entries, not autotext
field codes. Why are there [A], [B], [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], [B], etc. This text - [A], [B],
[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 [B]'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 [B]'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.

> 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
[quoted text clipped - 16 lines]
>
> Any suggestions......
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.