简体   繁体   English

MS Word宏-使用系统变量作为模板路径

[英]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. 我具有以下宏,可使用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. 我尝试使用%Appdata%代替,但是宏没有任何作用,没有错误消息。

is there any way around this ? 有没有办法解决 ?

Thank you 谢谢

You can use Environmental Variables in your VBA code to get the 'UserName' ie 您可以在VBA代码中使用环境变量来获取“ UserName”,即

"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 这是一个显示其他变量的链接(或仅搜索“使用环境变量的VBA”(不带引号: https//www.wiseowl.co.uk/blog/s387/environment-variable-vba.htm)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM