How to Use AutoText on an Existing Document

V

vbawannabee

I saw the following macro:

"How to Use AutoCorrect on an Existing Document"
on microsoft's website (http://support.microsoft.com/kb/210869)
I'm looking for a similar macro, but for AUTOTEXT entries.

REASON: Over the years I have amassed tons of autotext entries and I
often dream of a macro that "automatically scan a previously-created
document and make AutoText changes".

Is such a macro possible?
Thanks in advance for your reply.
 
G

Greg Maxey

Perhaps something like this will get you started:

Sub ScratchMacro()
Dim oAT As AutoTextEntry
Dim pStr As String
Dim oRng As Word.Range
For Each oAT In ActiveDocument.AttachedTemplate.AutoTextEntries
Set oRng = ActiveDocument.Range
pStr = oAT.Name
With oRng.Find
.Text = pStr
While .Execute
ActiveDocument.AttachedTemplate.AutoTextEntries(pStr).Insert
oRng
oRng.Collapse wdCollapseEnd
Wend
End With
Next
End Sub
 
H

Helmut Weber

Hi,

OK, i'm taking the risk of making a fool out of myself.

Possible, if you got a list of the values
of all your autotexts in the past, whatever that means.

Then you need a list of all your autotext values for now.

Then search all of the docs for the old autotext values.
Replace them with the corresponding new autotext values.

IMHO, Word does not remember the way some text was inserted.
Be it as autocorrect text or as autotext text.

Would be a nice feature to have.


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
V

vbawannabee

Helmut Weber said:
Hi,

OK, i'm taking the risk of making a fool out of myself.

Possible, if you got a list of the values
of all your autotexts in the past, whatever that means.

Then you need a list of all your autotext values for now.

Then search all of the docs for the old autotext values.
Replace them with the corresponding new autotext values.

IMHO, Word does not remember the way some text was inserted.
Be it as autocorrect text or as autotext text.

Would be a nice feature to have.


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
Thanks for your prompt reply.

I'm not sure whether I was clear in my post.

My autotext entries and values are the same and have not changed.

For instance, for the entry "i.e.", the corresponding autotext value is
"that is",
for the entry "turnover", the corresponding autotext value is "sales",
"debentures" is replaced by "bonds".
"center" by "centre",
etc.

I would like to have a macro that runs through an existing document and
automatically replaces each occurrence of an AutoText entry found in document
by the corresponding AutoText value. That would save me time as I would not
have to manually press the F3 button each time I come across such a word in
the existing document. Such a macro exists for AutoCorrect entries
(http://support.microsoft.com/kb/210869/en-us).
 
V

vbawannabee

Greg Maxey said:
Perhaps something like this will get you started:

Sub ScratchMacro()
Dim oAT As AutoTextEntry
Dim pStr As String
Dim oRng As Word.Range
For Each oAT In ActiveDocument.AttachedTemplate.AutoTextEntries
Set oRng = ActiveDocument.Range
pStr = oAT.Name
With oRng.Find
.Text = pStr
While .Execute
ActiveDocument.AttachedTemplate.AutoTextEntries(pStr).Insert
oRng
oRng.Collapse wdCollapseEnd
Wend
End With
Next
End Sub
Thanks Greg for your prompt reply.

Your macro certainly got me excited! I tried it last night but kept getting
(compilation) errors, which I was unable to resolve, given that I'm quite new
to VBA. I haven't given up yet, though! Do let me know if you it works for
you. Again, many thanks.
 
G

Greg Maxey

It works for me as I understand how you want it to work for you..Just from
what I see in your reply you should move the lone oRng up to the end of the
preceeding range.
 
F

Fred

Greg
Beginner here, how would I specify a specific Global template for
ActiveDocument.???Template.AutoTextEntries?
 
G

Greg Maxey

Fred,

Not tested as I don't have such a template wiht AT entries, but something
like this I think:

Sub ScratchMacro()
Dim oAT As AutoTextEntry
Dim pStr As String
Dim oTmp As Template
Dim oRng As Word.Range
Set oTmp = Templates("E:\My Documents\Word\Templates\Standard.dot")
'Template must be loaded.
For Each oAT In oTmp.AutoTextEntries
Set oRng = ActiveDocument.Range
pStr = oAT.Name
With oRng.Find
.Text = pStr
While .Execute
ActiveDocument.AttachedTemplate.AutoTextEntries(pStr).Insert oRng
oRng.Collapse wdCollapseEnd
Wend
End With
Next
End Sub
 
F

Fred

Great Greg,

But DEBUG on

ActiveDocument.AttachedTemplate.AutoTextEntries(pStr).Insert oRng

I tried saying oTmp but AttachedTemplate is not the right object is it?

Many thanks.
 
G

Greg Maxey

Oh,

I think:
ActiveDocument.AttachedTemplate.AutoTextEntries(pStr).Insert oRng

Would be:
oTmp.AutoTextEntries(pStr).Insert oRng
 
R

Russ

Fred,
I think your problem is with using templates other than normal.dot.
Type "template" or "autotext" or "organizer" into the VBA help search box to
learn about attaching templates or using add-ins or global templates and see
code snippets for templates and autotext and transferring info into
templates with the organizer.

If you just want to use the normal.dot autotext entries, you can amend
Greg's code to cycle through them with:
....
For Each oAT In NormalTemplate.AutoTextEntries
....
NormalTemplate.AutoTextEntries(pStr).Insert _
Where:=oRng
'The above two lines is actually one line broken with a space and
'a underscore character to fit on two lines for compactness.
....
 

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