簡體   English   中英

如何在 VB.Net 中的 Application.word 上使用 DocumentBeforeClose 事件

[英]How to use DocumentBeforeClose event on Application.word in VB.Net

我通過以下代碼在 VB.Net 中打開 Word 應用程序:

Dim appWord As New Microsoft.Office.Interop.Word.Application
appWord.Documents.Open("path")
appWord.Visible = True

我想訂閱 msword 的關閉事件並在關閉之前運行一些東西。 我讀了這個問題和這篇文章,但我真的不知道如何在 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM