|
Prev: Constants corresponding to DocumentFormat for SaveAs
Next: Generate doc in MHT format or in simple htmlformat
From: Stuart Troy on 29 Jun 2008 09:19 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?
From: Jay Freedman on 29 Jun 2008 11:20 On Sun, 29 Jun 2008 06:19:00 -0700, Stuart Troy <StuartTroy(a)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.
From: Lene Fredborg on 29 Jun 2008 12:32 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(a)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. >
From: Jay Freedman on 29 Jun 2008 16:41 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(a)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(a)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. >>
From: Lene Fredborg on 29 Jun 2008 17:26 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(a)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(a)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. > >> >
|
Next
|
Last
Pages: 1 2 Prev: Constants corresponding to DocumentFormat for SaveAs Next: Generate doc in MHT format or in simple htmlformat |