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

J

jerem

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]|", "|") '[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 ("")
....
....
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. ?????????
 
G

Graham Mayor

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


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


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]|", "|") '[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 ("")
...
...
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. ?????????
 
G

Greg Maxey

Jerem,

If you have an AutoText entry named [AB] and text dispersed in the document
"[AB]" blah blah "[AB]" ect.,

This code will find all instances and replace with the AutoText (as an
AUTOTEXT field)

Sub Test()
With Selection
With .Find
.ClearFormatting
.Text = "[AB]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
While .Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:=
_
"AUTOTEXT [AB] ", PreserveFormatting:=False
Selection.Collapse wdCollapseEnd
Wend
End With
End With
End Sub

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

jerem said:
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]|", "|") '[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 ("")
...
...
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. ?????????
 
S

Summer

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

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

Run macro it does this:

Mr AppleA saw Mr JonesC and went to LondonB.

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

Do you know how to run a macro?



Graham Mayor said:
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


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


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]|", "|") '[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 ("")
...
...
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. ?????????

 
G

Graham Mayor

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


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


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

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

Run macro it does this:

Mr AppleA saw Mr JonesC and went to LondonB.

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

Do you know how to run a macro?



Graham Mayor said:
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


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


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]|", "|") '[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 ("")
...
...
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. ?????????
 
G

Greg Maxey

Jerum,

Here is code that works to replace either with AutoText or the AutoText
field code. You decide which you want and then delete the message box and
extraneous code:

Sub ScrathMacroII()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
myArray = Split("[AB]|[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
If MsgBox("Do you want to replace with AutoText fields?", vbYesNo, "Choice")
= vbYes Then
While .Execute
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT [AB] ", PreserveFormatting:=False
oRng.Collapse wdCollapseEnd
Wend
Else
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End If

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:
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]|", "|") '[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 ("")
...
...
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. ?????????
 
S

Summer

Nice field codes - why preserve formatting false?

Graham Mayor said:
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


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


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

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

Run macro it does this:

Mr AppleA saw Mr JonesC and went to LondonB.

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

Do you know how to run a macro?



Graham Mayor said:
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

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



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]|", "|") '[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 ("")
...
...
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. ?????????

 
G

Graham Mayor

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


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

Nice field codes - why preserve formatting false?

Graham Mayor said:
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


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


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

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

Run macro it does this:

Mr AppleA saw Mr JonesC and went to LondonB.

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

Do you know how to run a macro?



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

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



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]|", "|") '[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 ("")
...
...
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.
?????????
 
R

Russ

Jerem,
Some more questions that may help us are: what version of Word are you
using?
Are these plain Word .doc(s) generated by Word to begin with?
Are these unprotected forms?
===============
Also, always use:

Option Explicit

In the (DECLARATIONS) section of your VBA code projects to help show when
there are undeclared variables or unknown subroutines in your VBA code when
it is 'compiled'.
===============
If you are getting any errors, we need to know exactly on which line the
error stops on and highlights, when you choose the DEBUG option; and exactly
what the error says.
===============

Most of these choices that we offered so far depend on the fact that you
already have the AutoText entries available for use and that they are named
exactly like the search terms we are looking for.
I.E. Look for [A] (three characters) and replace with the result of ***an
already existing*** AutoText entry or AutoText field ***named [A] (three
characters).***
Isn't that what you wanted?
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?
 
J

jerem

Summer,

Thank you, thank you, thank you. Someone understand me. I've been trying
to explain in every possible way I could that I want text - What text? [A],
, [C], etc. that is dispersed throughout a document to be replaced by its
similarly named autotext entry - autotext entry - autotext entry, not
autotext code. (These similarly named autotext entries have been set up
prior to running the macro). The code I've been using (which appears below)
works beautifully and fast (about 2 seconds flat replacing all [A], , [C],
etc. with their similarly named autotext entry) - the only problem was I
didn't know how to get it to loop so that if there are 4 [A]'s in the
document it finds all 4 and replaces them with their similarly named autotext
entry, then do a similar find and replace for the 's, [C]'s and so on and
so forth. So in order to combat that problem I've used a counter

Dim i As Integer
i = 4 (why because there are 4 [A]'s in this document that I need
replaced)
Do
i = i - 1
Selection.Find.ClearFormatting
With Selection.Find
..Text = "[A]"
..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
Loop Unitl i = 0
i = 6 (why because there are 6 's in this document that I need replaced)
Do
i = i - 1
Selection.Find.ClearFormatting
With Selection.Find
..Text = ""
..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 ", PreserveFormatting:=False (this sentence replaces the found
text with the autotext entry)
Loop Unitl i = 0

The problem (which was not a functional problem) was I didn't want to have
to tell it how many [A]'s, 's to replace (what if someone decided to add
another [A] in the document later on down the line, then it would become a
functional problem because it would miss replacing that text). So my initial
question that was posed was can I get this to somehow do a Replace All for
each find or can someone give me a way to loop each set of finds. This then
spawned all kinds of suggestions (which I do appreciate and have tried to
implement), however, they don't seem to do anything. Do I know how to write
a macro - yes, have written many. Am I fluent in VBA - no. I let my macro
recorder do my code-writing. I scavenge this site and others and grab pieces
here and there.

So, I never had a problem with my macro working - I just wasn't happy with
having to designate how many of each bracketed text [A], , etc. there were
and I only did designate it because I didn't know how to get each Find and
Replace to loop until it was done with the [A]'s, 's, [C]'s, etc.

If you see my previous inquiry (which was similarly named - Replace Found
Text wtih AutoText entries) I also went into detail why this was not being
handled by a merge (documents lock up - cannot breach the merge relationship
between the documents, which causes blacklining problems).

So, Greg, Graham, Ed, Russ - if you're out there and reading this:

I appreciate all of your help. I cannot get your code to work for my
problem and it very well may be that your code is good and my VBA savvy is
lacking. Again, I appreciate your effort in this.

Graham, here's the example you requested:

Autotext entries that I have already placed in AutoText [A] = Graham, =
Greg, [C] = Ed, [D] = Russ, [E] = Summer, [F] = jerem


Dear [A], , [C], [D],

Thank you [A], , [C], and [D] for reading my problem and trying to solve
it. I know you have put a lot of thought into my problem. I'd like to thank
[A] for responding so quickly and for being fundamental in his response
and [C] for being patient and [D] for his perserverance.

, I'm not familiar with arrays and so when that code didn't work, had no
idea why it didn't work. [C], I'm sure that code was brilliant and am sorry
that at this stage I cannot appreciate the genius of it. I will hold onto it
for the future so hopefully I can appreciate it when I do improve my VBA
language skills. [A], I detect your frustation in your last communication
and understand it. I've been trying to communicate EXACTLY what I needed and
thought I had in more than one of my communications.

Again, I appreciate all your help. Sorry to frustrate any or all of you.
[E], [A], , [C], [D], anyone have a loop statement for me?

Very truly yours,

[F]


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

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

Run macro it does this:

Mr AppleA saw Mr JonesC and went to LondonB.

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

Do you know how to run a macro?



Graham Mayor said:
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

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


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]|", "|") '[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 ("")
...
...
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. ?????????


 
J

jerem

Summer,

Thank you, thank you, thank you. Someone understands me. I've been trying
to explain in every possible way I could that I want text - What text? [A],
, [C], etc. that is dispersed throughout a document to be replaced by its
similarly named autotext entry - autotext entry - autotext entry, not
autotext code. (These similarly named autotext entries have been set up
prior to running the macro). The code I've been using (which appears below)
works beautifully and fast (about 2 seconds flat replacing all [A], , [C],
etc. with their similarly named autotext entry) - the only problem was I
didn't know how to get it to loop so that if there are 4 [A]'s in the
document it finds all 4 and replaces them with their similarly named autotext
entry, then do a similar find and replace for the 's, [C]'s and so on and
so forth. So in order to combat that problem I've used a counter

Dim i As Integer
i = 4 (why because there are 4 [A]'s in this document that I need
replaced)
Do
i = i - 1
Selection.Find.ClearFormatting
With Selection.Find
..Text = "[A]"
..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 (this puts in the autotext entry)
Loop Unitl i = 0
i = 6 (why because there are 6 's in this document that I need replaced)
Do
i = i - 1
Selection.Find.ClearFormatting
With Selection.Find
..Text = ""
..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 ", PreserveFormatting:=False (this sentence replaces the found
text with the autotext entry)
Loop Unitl i = 0

The problem (which was not a functional problem) was I didn't want to have
to tell it how many [A]'s, 's to replace (what if someone decided to add
another [A] in the document later on down the line, then it would become a
functional problem because it would miss replacing that text). So my initial
question that was posed was can I get this to somehow do a Replace All for
each find or can someone give me a way to loop each set of finds. This then
spawned all kinds of suggestions (which I do appreciate and have tried to
implement), however, they don't seem to do anything. Do I know how to write
a macro - yes, have written many. Am I fluent in VBA - no. I let my macro
recorder do my code-writing. I scavenge this site and others and grab pieces
here and there.

So, I never had a problem with my macro working - I just wasn't happy with
having to designate how many of each bracketed text [A], , etc. there were
and I only did designate it because I didn't know how to get each Find and
Replace to loop until it was done with the [A]'s, 's, [C]'s, etc.

If you see my previous inquiry (which was similarly named - Replace Found
Text wtih AutoText entries) I also went into detail why this was not being
handled by a merge (documents lock up - cannot breach the merge relationship
between the documents, which causes blacklining problems).

So, Greg, Graham, Ed, Russ - if you're out there and reading this:

I appreciate all of your help. I cannot get your code to work for my
problem and it very well may be that your code is good and my VBA savvy is
lacking. Again, I appreciate your effort in this.

Graham, here's the example you requested:

Autotext entries that I have already placed in AutoText [A] = Graham, =
Greg, [C] = Ed, [D] = Russ, [E] = Summer, [F] = jerem


Dear [A], , [C], [D], [E],

Thank you [A], , [C], and [D] for reading my problem and trying to solve
it. I know you have put a lot of thought into my problem. I'd like to thank
[A] for responding so quickly and for being fundamental in his response
and [C] for being patient and [D] for his perserverance.

, I'm not familiar with arrays and so when that code didn't work, had no
idea why it didn't work. [C], I'm sure that code was brilliant and am sorry
that at this stage I cannot appreciate the genius of it. I will hold onto it
for the future so hopefully I can appreciate it when I do improve my VBA
language skills. [A], I detect your frustation in your last communication
and understand it. I've been trying to communicate EXACTLY what I needed and
thought I had in more than one of my communications.

Again, I appreciate all your help. Sorry to frustrate any or all of you.
[E], [A], , [C], [D], anyone have a loop statement for me?

Very truly yours,

[F]

After running my macro, all of your names and mine would appear in this
document. I just don't want to have to tell it that your name Graham [A]
appears 5 times, Summer's [E] appears twice, etc., etc.

Graham Mayor said:
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


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


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]|", "|") '[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 ("")
...
...
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. ?????????

 
J

jerem

Tried it, Result: Run time Error '424', Object Required

Greg Maxey said:
Jerum,

Here is code that works to replace either with AutoText or the AutoText
field code. You decide which you want and then delete the message box and
extraneous code:

Sub ScrathMacroII()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
myArray = Split("[AB]|[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
If MsgBox("Do you want to replace with AutoText fields?", vbYesNo, "Choice")
= vbYes Then
While .Execute
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT [AB] ", PreserveFormatting:=False
oRng.Collapse wdCollapseEnd
Wend
Else
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End If

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:
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]|", "|") '[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 ("")
...
...
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. ?????????

 
J

jerem

Looks like the problem occurs at Replacement.Text = "" or at least that
statement is highlighted in Yellow when you go back into the Macro after
getting the Run Time Error '424', Object Required message

jerem said:
Tried it, Result: Run time Error '424', Object Required

Greg Maxey said:
Jerum,

Here is code that works to replace either with AutoText or the AutoText
field code. You decide which you want and then delete the message box and
extraneous code:

Sub ScrathMacroII()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
myArray = Split("[AB]|[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
If MsgBox("Do you want to replace with AutoText fields?", vbYesNo, "Choice")
= vbYes Then
While .Execute
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT [AB] ", PreserveFormatting:=False
oRng.Collapse wdCollapseEnd
Wend
Else
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End If

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:
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]|", "|") '[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 ("")
...
...
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. ?????????

 
G

Greg Maxey

Jerem,

That wasn't a 4 Star example of coding. It worked until I started trying to
give you the option of making a choice between replacing your text with
AutoText or AutoText fields. I didnt' test afterwards and admit the post
was dog's breakfast.

Here are two macros. The first replaces your [A], , [C] with autotext.
The second replaces with AutoText fields.

Sub ScrathMacroI()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
myArray = Split("[A]||[C]", "|")
For i = 0 To UBound(myArray)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = myArray(i)
.Forward = True
.Wrap = wdFindStop
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End With
Next i
End Sub
Sub ScrathMacroII()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
myArray = Split("[A]||[C]", "|")
For i = 0 To UBound(myArray)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = myArray(i)
.Forward = True
.Wrap = wdFindStop
While .Execute
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT " & myArray(i), PreserveFormatting:=False
oRng.Collapse wdCollapseEnd
Wend
End With
Next i
End Sub
End Sub

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

jerem said:
Looks like the problem occurs at Replacement.Text = "" or at least that
statement is highlighted in Yellow when you go back into the Macro after
getting the Run Time Error '424', Object Required message

jerem said:
Tried it, Result: Run time Error '424', Object Required

Greg Maxey said:
Jerum,

Here is code that works to replace either with AutoText or the AutoText
field code. You decide which you want and then delete the message box
and
extraneous code:

Sub ScrathMacroII()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
myArray = Split("[AB]|[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
If MsgBox("Do you want to replace with AutoText fields?", vbYesNo,
"Choice")
= vbYes Then
While .Execute
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT [AB] ", PreserveFormatting:=False
oRng.Collapse wdCollapseEnd
Wend
Else
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End If

End With
Next i
End Sub

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

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]|", "|") '[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 ("")
...
...
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. ?????????
 
J

jerem

I'm in love - will you marry me? Your code works beautifully, I don't have
to use mine and keep listing repetitive instructions. Hallelujah. I have to
tell you (and the other fellas out there) - you were starting to give me a
complex. I always thought I was relatively good at expressing myself and
that's why I decided to put up a new post so that maybe I could get a fresh
pair of eyes looking at my dilemma and someone would understand what I was
trying to do (32 threads is too much for a person to muddle through). When
Summer came along (and I'm going to assume Summer is a she) she picked up
immediately what I was trying to do - restored my faith in my communication
skills. Maybe it's true - woman are on one wavelength, men another. For a
time there I felt like I was speaking Greek to an all English audience.
Anyway, thanks again. It works spectacularly!!!

Greg Maxey said:
Jerem,

That wasn't a 4 Star example of coding. It worked until I started trying to
give you the option of making a choice between replacing your text with
AutoText or AutoText fields. I didnt' test afterwards and admit the post
was dog's breakfast.

Here are two macros. The first replaces your [A], , [C] with autotext.
The second replaces with AutoText fields.

Sub ScrathMacroI()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
myArray = Split("[A]||[C]", "|")
For i = 0 To UBound(myArray)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = myArray(i)
.Forward = True
.Wrap = wdFindStop
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End With
Next i
End Sub
Sub ScrathMacroII()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
myArray = Split("[A]||[C]", "|")
For i = 0 To UBound(myArray)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = myArray(i)
.Forward = True
.Wrap = wdFindStop
While .Execute
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT " & myArray(i), PreserveFormatting:=False
oRng.Collapse wdCollapseEnd
Wend
End With
Next i
End Sub
End Sub

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

jerem said:
Looks like the problem occurs at Replacement.Text = "" or at least that
statement is highlighted in Yellow when you go back into the Macro after
getting the Run Time Error '424', Object Required message

jerem said:
Tried it, Result: Run time Error '424', Object Required

:

Jerum,

Here is code that works to replace either with AutoText or the AutoText
field code. You decide which you want and then delete the message box
and
extraneous code:

Sub ScrathMacroII()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
myArray = Split("[AB]|[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
If MsgBox("Do you want to replace with AutoText fields?", vbYesNo,
"Choice")
= vbYes Then
While .Execute
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT [AB] ", PreserveFormatting:=False
oRng.Collapse wdCollapseEnd
Wend
Else
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End If

End With
Next i
End Sub

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

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]|", "|") '[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 ("")
...
...
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. ?????????

 
G

Graham Mayor

It has to be said that had it been clear that you wanted to replace[A] and
rather than [AB] and [BC] etc that the version I posted earlier wouold
have worked. All you had to do was put whatever you wanted to search for in
the array

vFindText = Array("[AB]", "[BC]", "etc")
thus
vFindText = Array("[A]", "", "[C]","[D]", "[E]", "[F]","ad infinitum" )

I also don't think Susan would appreciate Greg taking a new wife .......
though you never know ;)

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


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

I'm in love - will you marry me? Your code works beautifully, I
don't have to use mine and keep listing repetitive instructions.
Hallelujah. I have to tell you (and the other fellas out there) -
you were starting to give me a complex. I always thought I was
relatively good at expressing myself and that's why I decided to put
up a new post so that maybe I could get a fresh pair of eyes looking
at my dilemma and someone would understand what I was trying to do
(32 threads is too much for a person to muddle through). When Summer
came along (and I'm going to assume Summer is a she) she picked up
immediately what I was trying to do - restored my faith in my
communication skills. Maybe it's true - woman are on one wavelength,
men another. For a time there I felt like I was speaking Greek to
an all English audience. Anyway, thanks again. It works
spectacularly!!!

Greg Maxey said:
Jerem,

That wasn't a 4 Star example of coding. It worked until I started
trying to give you the option of making a choice between replacing
your text with AutoText or AutoText fields. I didnt' test
afterwards and admit the post was dog's breakfast.

Here are two macros. The first replaces your [A], , [C] with
autotext. The second replaces with AutoText fields.

Sub ScrathMacroI()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
myArray = Split("[A]||[C]", "|")
For i = 0 To UBound(myArray)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = myArray(i)
.Forward = True
.Wrap = wdFindStop
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End With
Next i
End Sub
Sub ScrathMacroII()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
myArray = Split("[A]||[C]", "|")
For i = 0 To UBound(myArray)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = myArray(i)
.Forward = True
.Wrap = wdFindStop
While .Execute
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT " & myArray(i), PreserveFormatting:=False
oRng.Collapse wdCollapseEnd
Wend
End With
Next i
End Sub
End Sub

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

jerem said:
Looks like the problem occurs at Replacement.Text = "" or at least
that statement is highlighted in Yellow when you go back into the
Macro after getting the Run Time Error '424', Object Required
message

:

Tried it, Result: Run time Error '424', Object Required

:

Jerum,

Here is code that works to replace either with AutoText or the
AutoText field code. You decide which you want and then delete
the message box and
extraneous code:

Sub ScrathMacroII()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
myArray = Split("[AB]|[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
If MsgBox("Do you want to replace with AutoText fields?", vbYesNo,
"Choice")
= vbYes Then
While .Execute
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT [AB] ", PreserveFormatting:=False
oRng.Collapse wdCollapseEnd
Wend
Else
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End If

End With
Next i
End Sub

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

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]|", "|") '[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 ("")
...
...
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. ?????????
 
S

Summer

It does not however do graphics in Hdr/Ftr's though - just in case you use
them.


Greg Maxey said:
Jerem,

That wasn't a 4 Star example of coding. It worked until I started trying
to give you the option of making a choice between replacing your text with
AutoText or AutoText fields. I didnt' test afterwards and admit the post
was dog's breakfast.

Here are two macros. The first replaces your [A], , [C] with autotext.
The second replaces with AutoText fields.

Sub ScrathMacroI()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
myArray = Split("[A]||[C]", "|")
For i = 0 To UBound(myArray)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = myArray(i)
.Forward = True
.Wrap = wdFindStop
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End With
Next i
End Sub
Sub ScrathMacroII()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
myArray = Split("[A]||[C]", "|")
For i = 0 To UBound(myArray)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = myArray(i)
.Forward = True
.Wrap = wdFindStop
While .Execute
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT " & myArray(i), PreserveFormatting:=False
oRng.Collapse wdCollapseEnd
Wend
End With
Next i
End Sub
End Sub

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

jerem said:
Looks like the problem occurs at Replacement.Text = "" or at least that
statement is highlighted in Yellow when you go back into the Macro after
getting the Run Time Error '424', Object Required message

jerem said:
Tried it, Result: Run time Error '424', Object Required

:

Jerum,

Here is code that works to replace either with AutoText or the
AutoText
field code. You decide which you want and then delete the message box
and
extraneous code:

Sub ScrathMacroII()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
myArray = Split("[AB]|[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
If MsgBox("Do you want to replace with AutoText fields?", vbYesNo,
"Choice")
= vbYes Then
While .Execute
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT [AB] ", PreserveFormatting:=False
oRng.Collapse wdCollapseEnd
Wend
Else
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End If

End With
Next i
End Sub

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

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]|", "|") '[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 ("")
...
...
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. ?????????

 
R

Russ

Jerem,
I think most of us were on the same wavelength, we just failed to test our
own and each other's code well enough and since you gave a general response
of the code wasn't doing what you wanted, but you didn't specify exactly how
the code wasn't working for you, we assumed that it had something to do with
the criteria you wanted and so we kept asking what exactly was it that you
wanted.
We were all trying to give you a solution that wouldn't involve knowing
beforehand how many replacements had to be made. But, when you said the code
did break at a specific line, then we were able to focus back on the code
and not your criteria and solve the problem.
Looks like the problem occurs at Replacement.Text = "" or at least that
statement is highlighted in Yellow when you go back into the Macro after
getting the Run Time Error '424', Object Required message

jerem said:
Tried it, Result: Run time Error '424', Object Required

Greg Maxey said:
Jerum,

Here is code that works to replace either with AutoText or the AutoText
field code. You decide which you want and then delete the message box and
extraneous code:

Sub ScrathMacroII()
Dim myArray As Variant
Dim i As Long
Dim oRng As Word.Range
myArray = Split("[AB]|[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
If MsgBox("Do you want to replace with AutoText fields?", vbYesNo, "Choice")
= vbYes Then
While .Execute
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
"AUTOTEXT [AB] ", PreserveFormatting:=False
oRng.Collapse wdCollapseEnd
Wend
Else
While .Execute
oRng.InsertAutoText
oRng.Collapse wdCollapseEnd
Wend
End If

End With
Next i
End Sub

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

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]|", "|") '[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 ("")
...
...
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. ?????????
 
I

Irene

jerem said:
Summer,

Thank you, thank you, thank you. Someone understand me. I've been trying
to explain in every possible way I could that I want text - What text? [A],
, [C], etc. that is dispersed throughout a document to be replaced by its
similarly named autotext entry - autotext entry - autotext entry, not
autotext code. (These similarly named autotext entries have been set up
prior to running the macro). The code I've been using (which appears below)
works beautifully and fast (about 2 seconds flat replacing all [A], , [C],
etc. with their similarly named autotext entry) - the only problem was I
didn't know how to get it to loop so that if there are 4 [A]'s in the
document it finds all 4 and replaces them with their similarly named autotext
entry, then do a similar find and replace for the 's, [C]'s and so on and
so forth. So in order to combat that problem I've used a counter

Dim i As Integer
i = 4 (why because there are 4 [A]'s in this document that I need
replaced)
Do
i = i - 1
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[A]"
.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
Loop Unitl i = 0
i = 6 (why because there are 6 's in this document that I need replaced)
Do
i = i - 1
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.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 ", PreserveFormatting:=False (this sentence replaces the found
text with the autotext entry)
Loop Unitl i = 0

The problem (which was not a functional problem) was I didn't want to have
to tell it how many [A]'s, 's to replace (what if someone decided to add
another [A] in the document later on down the line, then it would become a
functional problem because it would miss replacing that text). So my initial
question that was posed was can I get this to somehow do a Replace All for
each find or can someone give me a way to loop each set of finds. This then
spawned all kinds of suggestions (which I do appreciate and have tried to
implement), however, they don't seem to do anything. Do I know how to write
a macro - yes, have written many. Am I fluent in VBA - no. I let my macro
recorder do my code-writing. I scavenge this site and others and grab pieces
here and there.

So, I never had a problem with my macro working - I just wasn't happy with
having to designate how many of each bracketed text [A], , etc. there were
and I only did designate it because I didn't know how to get each Find and
Replace to loop until it was done with the [A]'s, 's, [C]'s, etc.

If you see my previous inquiry (which was similarly named - Replace Found
Text wtih AutoText entries) I also went into detail why this was not being
handled by a merge (documents lock up - cannot breach the merge relationship
between the documents, which causes blacklining problems).

So, Greg, Graham, Ed, Russ - if you're out there and reading this:

I appreciate all of your help. I cannot get your code to work for my
problem and it very well may be that your code is good and my VBA savvy is
lacking. Again, I appreciate your effort in this.

Graham, here's the example you requested:

Autotext entries that I have already placed in AutoText [A] = Graham, =
Greg, [C] = Ed, [D] = Russ, [E] = Summer, [F] = jerem


Dear [A], , [C], [D],

Thank you [A], , [C], and [D] for reading my problem and trying to solve
it. I know you have put a lot of thought into my problem. I'd like to thank
[A] for responding so quickly and for being fundamental in his response
and [C] for being patient and [D] for his perserverance.

, I'm not familiar with arrays and so when that code didn't work, had no
idea why it didn't work. [C], I'm sure that code was brilliant and am sorry
that at this stage I cannot appreciate the genius of it. I will hold onto it
for the future so hopefully I can appreciate it when I do improve my VBA
language skills. [A], I detect your frustation in your last communication
and understand it. I've been trying to communicate EXACTLY what I needed and
thought I had in more than one of my communications.

Again, I appreciate all your help. Sorry to frustrate any or all of you.
[E], [A], , [C], [D], anyone have a loop statement for me?

Very truly yours,

[F]


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

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

Run macro it does this:

Mr AppleA saw Mr JonesC and went to LondonB.

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

Do you know how to run a macro?



Graham Mayor said:
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

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



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]|", "|") '[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 ("")
...
...
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. ?????????

 
G

Greg Maxey

I am not sure what the question is now. ScratchMacroII that I posted
does perform the task of replacing [A], , [C] and [D] with
similiarly named AutoText entries.

jerem said:
Thank you, thank you, thank you.  Someone understand me.   I've been trying
to explain in every possible way I could that I want text - What text?  [A],
, [C], etc. that is dispersed throughout a document to be replaced by its
similarly named autotext entry - autotext entry - autotext entry, not
autotext code.  (These similarly named autotext entries have been setup
prior to running the macro).  The code I've been using (which appearsbelow)
works beautifully and fast (about 2 seconds flat replacing all [A], , [C],
etc. with their similarly named autotext entry) - the only problem was I
didn't know how to get it to loop so that if there are 4 [A]'s in the
document it finds all 4 and replaces them with their similarly named autotext
entry, then do a similar find and replace for the 's, [C]'s and so on and
so forth.  So in order to combat that problem I've used a counter

 Dim i As Integer
    i = 4 (why because there are 4 [A]'s in this document that I need
replaced)
    Do
    i = i - 1
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[A]"
.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
Loop Unitl i = 0
i = 6 (why because there are 6 's in this document that I need replaced)
    Do
    i = i - 1
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.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 ", PreserveFormatting:=False (this sentence replaces the found
text with the autotext entry)
Loop Unitl i = 0

The problem (which was not a functional problem) was I didn't want to have
to tell it how many [A]'s, 's to replace (what if someone decided toadd
another [A] in the document later on down the line, then it would become a
functional problem because it would miss replacing that text).  So myinitial
question that was posed was can I get this to somehow do a Replace All for
each find or can someone give me a way to loop each set of finds.  This then
spawned all kinds of suggestions (which I do appreciate and have tried to
implement), however, they don't seem to do anything.  Do I know how to write
a macro - yes, have written many.  Am I fluent in VBA - no.  I let my macro
recorder do my code-writing.  I scavenge this site and others and grab pieces
here and there.

So, I never had a problem with my macro working - I just wasn't happy with
having to designate how many of each bracketed text [A], , etc. there were
and I only did designate it because I didn't know how to get each Find and
Replace to loop until it was done with the [A]'s, 's, [C]'s, etc.

If you see my previous inquiry (which was similarly named - Replace Found
Text wtih AutoText entries)  I also went into detail why this was notbeing
handled by a merge (documents lock up - cannot breach the merge relationship
between the documents, which causes blacklining problems).
So, Greg, Graham, Ed, Russ - if you're out there and reading this:
I appreciate all of your help.  I cannot get your code to work for my
problem and it very well may be that your code is good and my VBA savvyis
lacking.  Again, I appreciate your effort in this.
Graham, here's the example you requested:
Autotext entries that I have already placed in AutoText [A] = Graham, =
Greg, [C] = Ed, [D] = Russ, [E] = Summer, [F] = jerem

Dear [A], , [C], [D],

Thank you [A],  , [C], and [D] for reading my problem and trying to solve
it.  I know you have put a lot of thought into my problem.  I'd like to thank
[A] for responding so quickly and for being fundamental in his response
and [C] for being patient and [D] for his perserverance.

, I'm not familiar with arrays and so when that code didn't work, had no
idea why it didn't work.  [C], I'm sure that code was brilliant and am sorry
that at this stage I cannot appreciate the genius of it.  I will holdonto it
for the future so hopefully I can appreciate it when I do improve my VBA
language skills.  [A], I detect your frustation in your last communication
and understand it.  I've been trying to communicate EXACTLY what I needed and
thought I had in more than one of my communications.

Again, I appreciate all your help.  Sorry to frustrate any or all of you.  
[E], [A], , [C], [D], anyone have a loop statement for me?

Very truly yours,

This code is fine - I have ATe code that does this:
Mr [A] saw Mr [C] and went to .
Run macro it does this:
Mr AppleA saw Mr JonesC and went to LondonB.
It is calling Autotext named [A] or or [C] from Normal - you may need to
specify location of ATe.
Do you know how to run a macro?
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 sitewww.gmayor.com
Word MVP web sitehttp://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]|", "|") '[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 ("")
...
...
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.  ?????????- Hide quoted text -


- Show quoted text -
 

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