Re: Replacing Found Text with AutoText Entry
- From: "Graham Mayor" <gmayor@xxxxxxxxxxxxxxxxxx>
- Date: Mon, 27 Aug 2007 09:00:21 +0300
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
My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
jerem wrote:
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
but I didn't know how to replace the last sentence so that it wouldmyRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll
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" wrote:
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
--
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], [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
I do like because it captures every occurrence without me having tomyRange.Find.Execute FindText:="[A]", _
ReplaceWith:="hello", Replace:=wdReplaceAll
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" wrote:
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.
"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.
"jerem" wrote:
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......
.
- References:
- Re: Replacing Found Text with AutoText Entry
- From: Greg Maxey
- Re: Replacing Found Text with AutoText Entry
- From: jerem
- Re: Replacing Found Text with AutoText Entry
- From: Greg Maxey
- Re: Replacing Found Text with AutoText Entry
- From: jerem
- Re: Replacing Found Text with AutoText Entry
- Prev by Date: RE: Replacing Found Text with AutoText Entry
- Next by Date: Re: How to Check Section Exists
- Previous by thread: Re: Replacing Found Text with AutoText Entry
- Next by thread: Re: Replacing Found Text with AutoText Entry
- Index(es):
Relevant Pages
|