Replacing Found Text with AutoText Entry

J

jerem

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

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

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

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

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

Any suggestions......
 
J

jerem

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

jerem

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

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

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

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

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

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

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

Any suggestions......
 
G

Greg Maxey

Disrcard last post. I am having trouble saving files tonight. I will post
a fix on my site in a few minutes.

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

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

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

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

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

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

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

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

Any suggestions......
 
G

Greg Maxey

OK, try downloading the addin now.

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

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

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

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

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

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

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

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

Any suggestions......
 
G

Graham Mayor

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


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


for
Dialogs(wdDialogEditAutoText).Show


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


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

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

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

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

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

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

Any suggestions......
 
G

Greg Maxey

Graham,

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

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

Then after the End Sub add:

Function GetDialog () As String
GetDialog - wdDialogBuildingBlockOrganizer
End Function
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


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


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


for
Dialogs(wdDialogEditAutoText).Show


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

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

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

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

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

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

Any suggestions......
 
G

Graham Mayor

I see your point - but I wonder if this would confuse beginners even more.
Experienced users probably wouldn't need the web page? There were however a
couple of insignificant bugs in my code which I have now finished. I'll
think further about including your suggested workaround :)

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


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

Greg said:
Graham,

I found what was causing the error in Word2003. I use the "Option
Explicit" statement at the top of all of my VBA projects. That is
what causes the complilation error to be thrown on
wdDialogsBuildingBlocks. I suppose one could just leave that out in
this case or use:
If Application.Version = 12 Then
'Word 2007 - Use the Building Blocks Organizer
Dialogs(GetDialog).Show
sType = "Building Block"
Else
'Not Word 2007 - Use the Autotext dialog.
Dialogs(wdDialogEditAutoText).Show
sType = "Autotext"
End If

Then after the End Sub add:

Function GetDialog () As String
GetDialog - wdDialogBuildingBlockOrganizer
End Function

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


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


for
Dialogs(wdDialogEditAutoText).Show


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

:

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

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

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

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

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

Any suggestions......
 
G

Graham Mayor

OK I've thought - and updated the web page ;)

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


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

Graham said:
I see your point - but I wonder if this would confuse beginners even
more. Experienced users probably wouldn't need the web page? There
were however a couple of insignificant bugs in my code which I have
now finished. I'll think further about including your suggested
workaround :)

Greg said:
Graham,

I found what was causing the error in Word2003. I use the "Option
Explicit" statement at the top of all of my VBA projects. That is
what causes the complilation error to be thrown on
wdDialogsBuildingBlocks. I suppose one could just leave that out in
this case or use:
If Application.Version = 12 Then
'Word 2007 - Use the Building Blocks Organizer
Dialogs(GetDialog).Show
sType = "Building Block"
Else
'Not Word 2007 - Use the Autotext dialog.
Dialogs(wdDialogEditAutoText).Show
sType = "Autotext"
End If

Then after the End Sub add:

Function GetDialog () As String
GetDialog - wdDialogBuildingBlockOrganizer
End Function

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


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


for
Dialogs(wdDialogEditAutoText).Show



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

:

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

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

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

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

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

Any suggestions......
 
J

jerem

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

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

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

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

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

Thanks for your help.



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

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

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

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

:

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

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

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

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

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

Any suggestions......
 
J

jerem

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

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

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

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

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

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

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

Thanks for your help.


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


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


for
Dialogs(wdDialogEditAutoText).Show


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


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

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

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

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

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

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

Any suggestions......
 
J

jerem

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

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

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

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

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

:

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

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

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

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

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

Any suggestions......
 
G

Greg Maxey

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

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

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

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


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

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

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

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

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

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

Thanks for your help.



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

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

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

:

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

:

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

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

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

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

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

Any suggestions......
 
E

Ed

Hello All,

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

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


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

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

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

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

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

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

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

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

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

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

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

Thanks for your help.


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


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


for
Dialogs(wdDialogEditAutoText).Show


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

My web site www.gmayor.com

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

:

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

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

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

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

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

Any suggestions......
 
E

Ed

Hello All,

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

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


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

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

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

Regards.

Ed

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

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

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

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

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

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

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

Thanks for your help.


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


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


for
Dialogs(wdDialogEditAutoText).Show


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

My web site www.gmayor.com

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

:

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

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

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

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

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

Any suggestions......
 
R

Russ

Ed,
It looks brilliant to me!
Hello All,

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

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


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

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

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

Regards.

Ed

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

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

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

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

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

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

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

Thanks for your help.


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


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


for
Dialogs(wdDialogEditAutoText).Show


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

My web site www.gmayor.com

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

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

:

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

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

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

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

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

Any suggestions......
 
G

Greg Maxey

Ed,

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

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

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

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

Hello All,

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

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


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

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

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

Regards.

Ed

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

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

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

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

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

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

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

Thanks for your help.


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


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


for
Dialogs(wdDialogEditAutoText).Show


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

My web site www.gmayor.com

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

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

:

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

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

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

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

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

Any suggestions......
 
R

Russ

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

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

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

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

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

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

Russ

A small tweak:
Also it might be simpler and faster to use the Array() function:
myArray = Array("[A]",""...)

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

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

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

Ed,

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

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

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

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