Re: Replacing Text w/Autotext - Greg, Ed, Graham,Russ

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Oops - something went awry in translation. The relevant part should have
read


While .Execute
Selection.Fields.Add Range:=Selection.Range, _
Type:=wdFieldEmpty, Text:= _
"AUTOTEXT " & vFindText(i), PreserveFormatting:=False
Selection.Collapse wdCollapseEnd
Wend

The preserve formatting switch managed to get itself into the field - It was
ignored, so I didn't notice while testing, but it shouldn't be there :(

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

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


Summer wrote:
Nice field codes - why preserve formatting false?

"Graham Mayor" <gmayor@xxxxxxxxxxxxxxxxxx> wrote in message
news:eszoCt66HHA.2380@xxxxxxxxxxxxxxxxxxxxxxx
The macro does indeed work, but the problem is that it neither does
what the OP wants, nor replaces the text with a field, nor updates
the fields afterwards, which it would need to to comply with the OPs
other intentions. Greg's attempt seems closest, and that could be
modified with an array to choose the set of texts that match the
autotext names quite easily, but I am still not 100% convinced that
this is what the OP is really asking for. However :

Sub Test4()
Dim vFindText As Variant
Dim i As Long

vFindText = Array("[AB]", "[BC]", "etc")
With Selection
With .Find
.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
For i = LBound(vFindText) To UBound(vFindText)
.Text = vFindText(i)
While .Execute
Selection.Fields.Add Range:=Selection.Range, _
Type:=wdFieldEmpty, Text:= _
"AUTOTEXT " & vFindText(i) & " "",
PreserveFormatting:=False" Selection.Collapse
wdCollapseEnd Wend
Next i
End With
.Fields.Update
End With
End Sub

adds the array function.

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

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



Summer wrote:
This code is fine - I have ATe code that does this:

Mr [A] saw Mr [C] and went to [B].

Run macro it does this:

Mr AppleA saw Mr JonesC and went to LondonB.

It is calling Autotext named [A] or [B] or [C] from Normal - you may
need to specify location of ATe.

Do you know how to run a macro?



"Graham Mayor" <gmayor@xxxxxxxxxxxxxxxxxx> wrote in message
news:%23$KIqD56HHA.3716@xxxxxxxxxxxxxxxxxxxxxxx
Before we waste any more time on this, what *EXACTLY* are we
looking for in the Document
and what *EXACTLY* do you want to replace the found string with?

Can you reproduce a sample?

It appears that you have a block of text thus '[AB]' and you want
to replace it with an autotext field of the same name if
{Autotext "[AB"]} and then presumably update that field to show the
content of the autotext?

Is that the full extent, or are there a variety of texts to find
and replace? If so what are they?

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

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



jerem wrote:
Hi Greg, Ed, Russ, etc.

Greg, tried the code you gave me:
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

and it doesn't seem to do anything.

Ed, similar problem with the code below:
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

So.......just one more question with the code below -- rather than
using the Dim i As Integer
i = 4
Do
i = i - 1
and the Loop Until i = 0 to get my set of instructions to loop
until all [A]'s appearing in the document are replaced with
similarly named autotext entries (and the only reason I'm using these
statements is because it's the only way I know how to get the darn
thing to loop) I know all of you have been trying to help me by
giving me other
suggestions (but it's not accomplishing what I need to get done)
but the code I've come up with does accomplish it. I just need
something like

Keep repeating the instructions below
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[AB]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range,
Type:=wdFieldEmpty, Text:= _ "AUTOTEXT [AB] ",
PreserveFormatting:=False Until there are no more to replace then go
to the next set of
instructions following this one

I know there are Whiles and Wends but have tried to fool around
with these but can't seem to get them to work. I just need
someone to get these instructions to loop without use a counter.
?????????


.



Relevant Pages