首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从VBA重写Office2010中的Document_New()

从VBA重写Office2010中的Document_New()
EN

Stack Overflow用户
提问于 2016-01-19 13:19:02
回答 1查看 93关注 0票数 0

我正在编写一些VBA代码(在Excel中),以便在工作中自动生成报表。这些报告需要有一个特定的格式,这是很容易通过我们的公司模板。所有这些都在MS Office 2010中。

然而,所有这些模板都有Document_New()子程序,这会引发一些我不想启动的VBA代码,遗憾的是,它们无法直接访问(公司规则,当然可以!)

是否有一种方法来覆盖Document_New()宏?现在,我打开文档的VBA代码(触发Document_New()宏)如下所示:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-19 14:28:43

试一试如下:

代码语言:javascript
复制
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_NewSub,并且工作正常。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34877887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档