当用户点击MS Word中的“保存”按钮时,如何在VBA中调用userform?
答案 0 :(得分:2)
您有两个选项可以执行此操作:您可以覆盖内置的FileSave
和FileSaveAs
命令,也可以为应用程序的DocumentBeforeSave
事件创建事件处理程序(还有一点工作要做。)
可以通过将以下代码添加到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 Objects - >下来实现。 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
答案 1 :(得分:0)
请参阅Intercepting events like Save and Print以获取有用的示例。