我有一些代码,其中添加了使用Access所需的VBA引用。(它检查用户是否已经包含了它们)。
这一切都很好。我的问题是,当我去关闭word时,我被提示保存缩略词工具模板(其中存储此代码的宏)。是否有可能以编程的方式做到这一点,以便在添加引用时尽快完成,并且用户看不到它发生?
我使用的代码:
If Not isReferenceLoaded("Access") Then
MsgBox ("Access Object library not found, the script will now attempt to find the library for you.")
'Ensure access library is included so database actions can be done
ID.AddFromFile "C:Program Files (x86)Microsoft OfficeOffice14MSACC.OLB"
MsgBox ("Access Object library added")
End If
If Not isReferenceLoaded("DAO") Then
MsgBox ("DAO library not found, the script will now attempt to find the library for you.")
'Ensure access library is included so database actions can be done
ID.AddFromFile "C:Program Files (x86)Common Filesmicrosoft sharedOFFICE14ACEDAO.DLL"
MsgBox ("DAO library added")
End If
查看引用是否被加载的函数:
Private Function isReferenceLoaded(referenceName As String) As Boolean
Dim xRef As Variant
For Each xRef In ThisDocument.VBProject.References
isReferenceLoaded = (xRef.Name = referenceName) Or isReferenceLoaded
Next xRef
End Function
如果您不想用更新的引用保存文档(我猜它们将在下次打开文档时重新制作),请将以下子例程添加到document对象(假定命名为
ThisDocument
):
Private Sub Document_Close()
ThisDocument.Saved = True
End Sub
这将只禁止对ThisDocument进行保存检查。如果确实想要保存,则在开头添加Call ThisDocument.Save
语句。但是,我希望用户能够保存他们的更改……但这是一个策略问题,请按照您的意愿去做。
注释:添加函数后不要忘记保存,否则会丢失更改;Word将忽略您的更改,因为它只是由函数指示这样做。: -)