Word 2007 宏:是否有可以附加宏的 OnPrint 事件? [英] Word 2007 macros: is there an OnPrint event I can attach a macro to?

查看:55
本文介绍了Word 2007 宏:是否有可以附加宏的 OnPrint 事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在 word 文件发送到打印机之前对其进行格式检查,并且它需要对用户完全透明(没有额外的控件,只需使用通过 UI 提供的标准打印选项).是否有 OnPrint 或 BeforePrint 事件,或者可以在我可以附加宏的方面使用的东西,就像我可以使用打开、关闭或保存一样?这感觉应该很简单……但那是著名的遗言.

I'm looking to do a formatting check on word files before they get sent to the printer and it needs to be completely transparent to the user (no extra controls, just using the standard print options available through the UI). Is there an OnPrint or BeforePrint event, or something that can be used in that respect which I could attach a macro to, the same way I can with Open, Close, or Save? This feels like it should be simple... but those are famous last words.

提前致谢,罗布

推荐答案

您可以设置包装类来启用 Word 的应用程序事件.

You can setup a wrapper class to enable Word's application events.

在您的文档中,您需要创建一个类模块.这个类模块将被称为clsEvents".将此代码粘贴到您的新类模块中:

In your document, you will need to create a class module. This Class module will be called "clsEvents". Paste this code into your new class module:

Public WithEvents myApp As Word.Application

Public Sub myApp_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)

    'add your code here
    MsgBox "Blah"
End Sub

接下来,创建一个标准模块.这将是将应用程序实例加载到您的类中的子程序.将此标准模块命名为事件".然后粘贴此代码:

Next, create a standard module. This will be the sub that will load the application instance into your class. Name this standard module "Events". Then Paste this code:

Public e As clsEvents

Public Sub SetupEvents(theApp As Application)
    Set e = New clsEvents
    Set e.myApp = theApp
End Sub

最后,我们需要调用您刚刚创建的子程序.最简单的方法是在ThisDocument"模块的 document_open 事件上调用它.粘贴此代码:

Lastly, we need to call that subroutine that you just created. The easiest way to do it is to call it on the document_open event from "ThisDocument" module. Paste this code:

Private Sub Document_Open()
    SetupEvents Me.Application
End Sub

这也将允许您使用通常在没有文档包装器的情况下隐藏的所有其他 Word 应用程序事件.

This will also allow you to use all the other Word Application events that are generally hidden without the document wrapper.

关闭应用程序,下次打开文档,用户尝试打印时,您的代码将执行.

Close the application, and next time the document is opened, and the user tried to print, your code will execute.

希望有帮助!

这篇关于Word 2007 宏:是否有可以附加宏的 OnPrint 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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