[英]How to use DocumentBeforeClose event on Application.word in VB.Net
添加對 Microsoft Word XX.0 Object 庫的引用(項目--引用...)。
如果您的 MS Office 是 2016,XX 取決於您的 MS Office 版本,即 16.0。
在窗體中添加一個名為 Command1 的命令按鈕。 將此代碼添加到表單中:
Option Explicit
Public WithEvents moWord As Word.Application
Private Sub Command1_Click()
' open test document
With moWord
.Documents.Open "J:\Test.docx" ' change document path according to actual file
'.WindowState = wdWindowStateNormal
.Visible = True
End With
End Sub
Private Sub Form_Load()
Set moWord = New Word.Application
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If Not (moWord Is Nothing) Then Set moWord = Nothing
End Sub
Private Sub moWord_DocumentBeforeClose(ByVal Doc As Word.Document, Cancel As Boolean) Handles moWord.DocumentBeforeClose
If MsgBox("Do you really want to close the document?", vbQuestion + vbYesNo + vbDefaultButton2) = vbNo Then Cancel = True
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.