Document_New事件启动Word [英] Document_New event on launching Word

查看:103
本文介绍了Document_New事件启动Word的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VBA插件,我想在有人打开文档时运行它.对于打开现有文档(AutoOpen)和从文件">新建"菜单(AutoNew)创建新文档,它工作正常,但是当我第一次打开Word时,这两个事件均未触发.我似乎可以想到的唯一事件是AutoExec事件,但这并不好,因为该文档尚不存在,因此ActiveWindow为null.

I have a VBA addin which I want to run every time someone opens a document. It's working fine for opening existing documents (AutoOpen) and creating new documents from the File > New menu (AutoNew) but when I just open Word up for the first time, neither of these events are firing. The only event I can seem to hook into is the AutoExec event and this is not great as the document doesn't exist yet so ActiveWindow is null.

任何人都可以帮忙吗?

Sub AutoNew
    MsgBox "New"
End Sub

Sub AutoOpen
    MsgBox "Open"
End Sub

Sub AutoExec
    MsgBox "Exec"
End Sub

推荐答案

我将从DocumentOpen和NewDocument开始.如果需要支持ProtectedView文档,则存在更高的复杂性. Word触发另一个事件.我发现如果我尝试检查该事件(并且不会发生),则会引发错误.我运气不太好,最终我不值得花时间.我在下面发布了一个示例代码,该示例在打开文档或创建新文档时(假定加载了加载项)打开样式窗格,并在草稿视图中扩展样式边距(如果尚未扩展).

I would start with DocumentOpen and NewDocument. An additional level of complexity exists if you need to support ProtectedView documents; Word triggers a different event. I've found that if I try to check for that event (and it doesn't occur) it raises an error. I didn't had much luck and eventually it wasn't worth the time I was spending. I've posted an example below of some code that opens the Style Pane when a document is opened or a new one created (assuming the add-in is loading) and expands the style margin in draft view if not already expanded.

在我的UI模块中:

Dim X As New clsAppEvent 'This is in the declarations

Public Sub OnRibbonLoad(objRibbon As IRibbonUI)
    Do While Documents.Count = 0
      DoEvents
    Loop ' I find this useful as sometimes it seems my ribbon loads before the document. 
    Call Register_Event_Handler
    ' Other stuff
End Sub

Private Sub Register_Event_Handler()
    Set X.App = Word.Application
End Sub

然后,在类模块中,我将其称为clsAppEvent:

Then, in a class module I call clsAppEvent:

Option Explicit

Public WithEvents App As Word.Application

Private Sub App_DocumentOpen(ByVal Doc As Document)
    App.TaskPanes(wdTaskPaneFormatting).visible = True
End Sub

Private Sub App_NewDocument(ByVal Doc As Document)
    App.TaskPanes(wdTaskPaneFormatting).visible = True
End Sub

Private Sub App_WindowActivate(ByVal Doc As Document, ByVal Wn As Window)
    If Wn.StyleAreaWidth <= 0 Then
      Wn.StyleAreaWidth = 60
    End If
End Sub

除了我上面指出的警告以外,我遇到的一个问题是,如果用户在其Normal模板中也具有自动"代码.这只出现过一次,所以我没有对其进行调查.

Other than the caveats I've noted above, the one problem I've had is if a user has Auto code in their Normal template as well. This has only come up once so I haven't investigated it.

我希望我能找到一个了解此信息的站点(并从该站点派生Register_Event_Handler.如果找到它,我会添加评论.

I wish I could find the site where I learned about this (and from which the Register_Event_Handler was derived. If I find it I'll add a comment.

这篇关于Document_New事件启动Word的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆