保存时调用 UserForm

发布于 2024-07-30 00:47:29 字数 47 浏览 3 评论 0原文

当用户单击 MS Word 中的“保存”按钮时,如何在 VBA 中调用用户窗体?

How do I go about calling a userform in VBA when user clicks on the Save button in MS Word?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

能否归途做我良人 2024-08-06 00:47:29

您有两种选择来执行此操作:您可以覆盖内置的 FileSaveFileSaveAs 命令,也可以为应用程序的 DocumentBeforeSave< 创建事件处理程序/code> 事件(还有更多工作要做)。

可以通过将以下代码添加到 VBA 模块来实现覆盖内置命令(相应地调整要显示的用户窗体的类型):

' override File -> Save
Public Sub FileSave()

    CustomSave
    ' call ActiveDocument.Save to actually save the document

End Sub

' override File -> Save As...
Public Sub FileSaveAs()

    CustomSave
    ' call ActiveDocument.SaveAs to actually save the document

End Sub

Sub CustomSave()

    Dim frm As New frmCustomSave
    frm.Show

End Sub

第二个选项可以通过将以下代码放置在 Microsoft Word 对象下来实现 -> VBA 编辑器中的ThisDocument

Option Explicit

Private WithEvents wdApp As Word.Application

Private Sub Document_New()
    Set wdApp = Word.Application
End Sub

Private Sub Document_Open()
    Set wdApp = Word.Application
End Sub

Private Sub wdApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)

    Dim frm As New frmCustomSave
    frm.Show

End Sub

You have two options to do that: You can either override the built-in FileSave and FileSaveAs commands, or you can create an event handler for the application's DocumentBeforeSave event (which is a little more work to do).

Overriding the built-in commands can be accomplished by adding the following code to a VBA module (adjust the type of the user form to be displayed accordingly):

' override File -> Save
Public Sub FileSave()

    CustomSave
    ' call ActiveDocument.Save to actually save the document

End Sub

' override File -> Save As...
Public Sub FileSaveAs()

    CustomSave
    ' call ActiveDocument.SaveAs to actually save the document

End Sub

Sub CustomSave()

    Dim frm As New frmCustomSave
    frm.Show

End Sub

The second option can be implemented by placing the following code under Microsoft Word Objects -> ThisDocument in the VBA editor:

Option Explicit

Private WithEvents wdApp As Word.Application

Private Sub Document_New()
    Set wdApp = Word.Application
End Sub

Private Sub Document_Open()
    Set wdApp = Word.Application
End Sub

Private Sub wdApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)

    Dim frm As New frmCustomSave
    frm.Show

End Sub
初雪 2024-08-06 00:47:29

请参阅拦截“保存”和“打印”等事件,了解应该有所帮助的示例。

See Intercepting events like Save and Print for an example that should help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文