Moha the almighty camel
Moha the almighty camel

Reputation: 4453

MS Word macro - use system variable for template path

I have a template that contains several macros, this template will be distributed on several devices.

I have the following macros to insert a text using quick parts of ms word.

Application.Templates( _


"C:\Users\user_name\AppData\Roaming\Microsoft\Word\STARTUP\template_name.dotm" _
    ).BuildingBlockEntries("alphabet").Insert Where:=Selection.Range, _
    RichText:=True
ActiveDocument.TrackRevisions = Not ActiveDocument.TrackRevisions

The issue is, the code contains the absolute path to the template, which won't be the same on different machines.

I have tried using %Appdata% instead, but there macro did nothing, with no error messages.

is there any way around this ?

Thank you

Upvotes: 1

Views: 1182

Answers (1)

Wayne G. Dunn
Wayne G. Dunn

Reputation: 4312

You can use Environmental Variables in your VBA code to get the 'UserName' i.e.

"C:\Users\" & LCase(Environ("UserName")) & "\AppData\Roaming\.."

Here is one link to show other variables (or just search for 'VBA using environmental variables' (without quotes: https://www.wiseowl.co.uk/blog/s387/environment-variable-vba.htm

Upvotes: 1

Related Questions