【问题标题】:Override Document_New() in Office 2010 from VBA从 VBA 覆盖 Office 2010 中的 Document_New()
【发布时间】:2016-01-19 13:19:02
【问题描述】:

我正在编写一些 VBA 代码(在 Excel 中)以在工作中自动生成报告。这些报告需要具有特定格式,可通过我们的公司模板轻松获得。这一切都在 MS Office 2010 中。

但是,所有这些模板都有Document_New() subs,这会引发一些我不想启动的 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

【问题讨论】:

    标签: vba excel ms-office


    【解决方案1】:

    试试类似的东西:

    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 VBA 打开了一个 Word.dotm,它在 ThisDocument 模块中有一个名为 Document_NewSub,它可以正常工作。

    【讨论】:

    • 我在WordApp.EnableEvents = False 线上收到错误,说该对象不支持此属性或方法...
    • 是的!这解决了它。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    相关资源
    最近更新 更多