我正在编写一些VBA代码(在Excel中),以便在工作中自动生成报表。这些报告需要有一个特定的格式,这是很容易通过我们的公司模板。所有这些都在MS Office 2010中。
然而,所有这些模板都有Document_New()子程序,这会引发一些我不想启动的VBA代码,遗憾的是,它们无法直接访问(公司规则,当然可以!)
是否有一种方法来覆盖Document_New()宏?现在,我打开文档的VBA代码(触发Document_New()宏)如下所示:
Sub GenerateReport()
Dim WordApp As Object
Set WordApp = CreateObject("Word.Application")
WordApp.Documents.Add Template:="Letter.dot" ' the "error" is produced at this lie
Dim SaveStr As String
SaveStr = "KommRapp-" & Date & ".docx"
WordApp.ActiveDocument.SaveAs SaveStr
End Sub发布于 2016-01-19 14:28:43
试一试如下:
Sub OpenWordDocAndBlockMacros()
Dim WordApp As Object
Set WordApp = CreateObject("Word.Application")
'Just to see whats happening
WordApp.Visible = True
Dim InitialSecurityAutoSetting As MsoAutomationSecurity
'Save Automations Security Setting
SecurityAutoSetting = WordApp.Application.AutomationSecurity
WordApp.Application.AutomationSecurity = msoAutomationSecurityForceDisable
WordApp.Documents.Add Template:=" Place Template file path here"
'Do some stuff with the template
End Sub我对它进行了测试,使用Excel打开了一个Word.dotm,它在ThisDocument模块中有一个名为Document_New的Sub,并且工作正常。
https://stackoverflow.com/questions/34877887
复制相似问题