我已经构建了一个Word宏启用模板(.dotm),当我将它保存到我的Word Startup文件夹时,它在我的机器上运行良好。但是,当我将.dotm
文件发送给我的同事时(有些人和我一样,有些人没有),按下自定义ui功能区按钮时会产生错误:
由于您的安全设置,无法找到或已禁用宏
宏设置设置为运行所有宏,包含模板>的文件夹是受信任位置
....更多帮助选项跟随
我的同事还将文件保存到他们的启动文件夹并加载Word。功能区显示完美,但宏不运行。我和我的同事已经完成了大量的在线搜索,无法弄清楚问题,因为据我所知,我的所有配置都是正确的,我将在下面列出:
自定义用户界面
<?xml version="1.0" encoding="utf-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<ribbon>
<tabs>
<tab id="customTab" label="WebMerge">
<group id="CustomHelp" label="Map Tags">
<button id="mapper"
visible="true"
size="large"
label="Open Mapper"
screentip="Opens Mapping Engine"
onAction="LoadMappingGuide"
imageMso="FindDialog"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
标准模块中的UI功能区连接
Option Explicit
Sub LoadMappingGuide(ByVal Control As IRibbonControl) 'also tried without ByVal
'select business unit or automatically detect based on username against our database ...
Dim mappingForm As New mappingGuide
mappingForm.Show vbModal
End Sub
mappingForm(UserForm)代码
Option Explicit
Private Sub cancelButton_Click()
Unload Me
End Sub
Private Sub searchBox_Change()
generateList
Dim n As Long, index As Long
index = 0
For n = 0 To smartTagList.ListCount - 1
If InStr(1, smartTagList.List(index, 0), searchBox.Value, vbTextCompare) > 0 Then
index = index + 1
ElseIf InStr(1, smartTagList.List(index, 1), searchBox.Value, vbTextCompare) > 0 Then
index = index + 1
ElseIf InStr(1, smartTagList.List(index, 2), searchBox.Value, vbTextCompare) > 0 Then
index = index + 1
Else
smartTagList.RemoveItem (index)
End If
Next n
End Sub
Private Sub smartTagList_Click()
If smartTagList.ListIndex > -1 And smartTagList.Selected(smartTagList.ListIndex) Then
Dim smartyTag As String
smartyTag = smartTagList.List(smartTagList.ListIndex, 2)
Selection.Range.Text = smartyTag
End If
Unload Me
End Sub
Private Sub UserForm_Initialize()
generateList
End Sub
Private Sub generateList()
'replace with code to get values from database
Dim fields() As String
Dim descriptions() As String
Dim smartyTags() As String
fields = Split("Producer Name,Producer Address,Producer City,Producer State,Producer Zip,Insured Name,Insured Address,Insured City,Insured State,Insured ZIp,Risk Premium,TRIA Premium,Final Premium", ",")
descriptions = Split("Name of Producer,Address Line of Producer,City of Producer,State of Producer,Zip Code of Producer,Name of Insured,Address of Insured,City of Insured,State of Insured,ZIp of Insured,Total Premium for all risks excluding terrorism taxes and surcharges.,Premium resulting from acceptance of terrorism protection,Total Premium of quote / policy", ",")
smartyTags = Split("{$Producer Name},{$ProducerAddress},{$ProducerCity},{$ProducerState},{$ProducerZip},{$InsuredName},{$InsuredAddress},{$InsuredCity},{$InsuredState},{$InsuredZIp},{$RiskPremium},{$TRIAPremium},{$FinalPremium}", ",")
Dim i As Long
For i = LBound(fields) To UBound(fields)
With smartTagList
.AddItem
.List(i, 0) = fields(i)
.List(i, 1) = descriptions(i)
.List(i, 2) = smartyTags(i)
End With
Next
End Sub
这是我的VBE在.dotm
打开时的样子。
答案 0 :(得分:2)
出现错误消息的最可能原因是宏在包含功能区自定义的模板中不可用。通常,它们保留在开发机器上的Normal.dotm
模板中。
背景:
VBA开发人员在开始项目时始终需要记住的一点,并且在Word中进行测试是Word使用多个“上下文”。首先,在私有用户环境中记录宏和创建功能时,最重要的是默认值是Normal.dotm
模板。这是“通用的”:无论您使用哪种文档,即使该文档附加到不同的模板,它也会发挥作用。
这样做的最大危险是您可能忘记将针对特定模板的代码从Normal.dotm
移动到该模板。或者,即使您复制了代码,如果两个模板中都存在具有相同过程名称的代码,Normal.dotm
中的代码也可以保持优先权。
在为Ribbon和QAT按钮分配宏时,这尤其是一个问题。即使开发人员计算机中的Normal.dotm
不再存在,功能区或QAT分配仍然会查找,并且不会检查备用源。
因此,如果您决定将代码保留在Normal.dotm
以及特定模板中,则最好重命名Normal.dotm
中的过程以避免冲突。