Create AutoText Entry from a Variable

S

Stuart Troy

I want to create a AutoText entry named; "UserProfile"

And it's value to be the result of; VBA.Environ$("USERPROFILE")

How can I accomplish this using VBA?
 
J

Jay Freedman

I want to create a AutoText entry named; "UserProfile"

And it's value to be the result of; VBA.Environ$("USERPROFILE")

How can I accomplish this using VBA?

An AutoText entry can only be created from a range in a document, not directly
from a string variable. (I think that's a design deficiency in VBA, but unlikely
ever to be changed.) So you have to put the environment string temporarily into
some document (most easily the active document), create the entry, and then
remove the string from the document.

You can put the entry into any active (loaded or attached) template; I'll assume
you want it in Normal.dot. I also must assume you have Word 2003 or earlier,
because AutoText in Word 2007 is part of the Building Blocks collection, which
makes its creation in VBA considerably more complicated. If you need that, post
back.

So...

Sub demo()
Dim myRg As Range

Set myRg = ActiveDocument.Range
With myRg
.Collapse wdCollapseEnd
.Text = Environ$("USERPROFILE")
End With

On Error Resume Next
NormalTemplate.AutoTextEntries.Add _
Name:="UserProfile", Range:=myRg
If Err.Number <> 0 Then
MsgBox "Could not save AutoText UserProfile = " & myRg.Text
ActiveDocument.Undo
Exit Sub
End If

NormalTemplate.Save
ActiveDocument.Undo
End Sub
 
L

Lene Fredborg

I have experienced that you _can_ actually create an AutoText via VBA and set
the value to whatever string you want without first inserting the string in
the document. The idea is to first create the AutoText containing whatever is
selected in the document (or you could define another range of your wish).
Then you can use the Value property of the AutoText to replace the content
with the desired string.

An example of such macro is found below. Replace the name, template and new
value as desired.

Sub CreateAutoTextViaVBA()

Dim oAutoText As AutoTextEntry
'Create an AutoText with the current selection as the content
'You could instead use any range from the document
Set oAutoText =
Templates(ActiveDocument.AttachedTemplate).AutoTextEntries _
.Add(Name:="MyName", Range:=Selection.Range)
'Now replace the content of the AutoText with the desired value
With oAutoText
.Value = "ThisIsMyNewValue"
End With

'Clean up
Set oAutoText = Nothing

End Sub

Correspondingly, you can change the contents of any existing AutoText – or
you can replace a certain string in any AutoText.

Example: you want to replace the string “abc†in all AutoText entries in a
specific template (here MyTemplate) with “12345â€. To do this:

Dim oAutoText As AutoTextEntry

For Each oAutoText In MyTemplate.AutoTextEntries
oAutoText.Value = Replace(oAutoText.Value, "abc", "12345")
Next oAutoText

(I have not included any error handling in the examples above).


--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
J

Jay Freedman

Thanks! That's some interesting "sideways" thinking. One more for the toolbox!

Jay
 
L

Lene Fredborg

Yes, this kind of thinking is often needed when Word does not seem to let you
do what you want. I think I found the solution some time ago when I needed to
make corrections to a huge number of AutoTexts.

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
S

Stuart Troy

Thank you Jay.
Thank you Lene.
Very helpful responses!

Kind regards
Stuart
Sydney, AUSTRALIA
 

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