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?

Re: Create AutoText Entry from a Variable by Jay

Jay
Sun Jun 29 08:20:10 PDT 2008

On Sun, 29 Jun 2008 06:19:00 -0700, Stuart Troy
<StuartTroy@discussions.microsoft.com> wrote:

>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

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.

Re: Create AutoText Entry from a Variable by lf

lf
Sun Jun 29 09:32:01 PDT 2008

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


"Jay Freedman" wrote:

> On Sun, 29 Jun 2008 06:19:00 -0700, Stuart Troy
> <StuartTroy@discussions.microsoft.com> wrote:
>
> >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
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
>

Re: Create AutoText Entry from a Variable by Jay

Jay
Sun Jun 29 13:41:51 PDT 2008

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

Jay

On Sun, 29 Jun 2008 09:32:01 -0700, Lene Fredborg <lf@REMOVETHISthedoctools.com>
wrote:

>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
>
>
>"Jay Freedman" wrote:
>
>> On Sun, 29 Jun 2008 06:19:00 -0700, Stuart Troy
>> <StuartTroy@discussions.microsoft.com> wrote:
>>
>> >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
>>
>> --
>> Regards,
>> Jay Freedman
>> Microsoft Word MVP FAQ: http://word.mvps.org
>> Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
>>

Re: Create AutoText Entry from a Variable by lf

lf
Sun Jun 29 14:26:04 PDT 2008

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


"Jay Freedman" wrote:

> Thanks! That's some interesting "sideways" thinking. One more for the toolbox!
>
> Jay
>
> On Sun, 29 Jun 2008 09:32:01 -0700, Lene Fredborg <lf@REMOVETHISthedoctools.com>
> wrote:
>
> >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
> >
> >
> >"Jay Freedman" wrote:
> >
> >> On Sun, 29 Jun 2008 06:19:00 -0700, Stuart Troy
> >> <StuartTroy@discussions.microsoft.com> wrote:
> >>
> >> >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
> >>
> >> --
> >> Regards,
> >> Jay Freedman
> >> Microsoft Word MVP FAQ: http://word.mvps.org
> >> Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
> >>
>

Re: Create AutoText Entry from a Variable by StuartTroy

StuartTroy
Tue Jul 01 16:41:04 PDT 2008

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

Kind regards
Stuart
Sydney, AUSTRALIA