Vb.net VB文字管理

Vb.net VB文字管理,vb.net,ms-word,Vb.net,Ms Word,我一直在尝试创建一个程序来处理使用VB.net在Microsoft Word中触发的事件。我的最终目标是使用此程序来响应用户关闭的Word,但当我尝试使用DocumentBeforeClose或DocumentBeforeSave时,它们似乎不会触发,但我有一个事件要处理NewDocument,并且工作正常 这是我的密码: Imports Microsoft.Office.Interop Public Class Form1 Private WithEvents oWord As Word.

我一直在尝试创建一个程序来处理使用VB.net在Microsoft Word中触发的事件。我的最终目标是使用此程序来响应用户关闭的Word,但当我尝试使用
DocumentBeforeClose
DocumentBeforeSave
时,它们似乎不会触发,但我有一个事件要处理
NewDocument
,并且工作正常

这是我的密码:

Imports Microsoft.Office.Interop

Public Class Form1

Private WithEvents oWord As Word.ApplicationClass
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Create a new instance of Word, make it visible, and activate it.
    oWord = CreateObject("Word.Application")
    oWord.Visible = True
    oWord.Activate()

    'Create a new document.
    oWord.Documents.Add()

    'Release the instance of Word and leave it running.
    oWord = Nothing

End Sub

Private Sub oWord_ApplicationEvents2_Event_DocumentBeforeClose(Doc As Word.Document, ByRef Cancel As Boolean) Handles oWord.ApplicationEvents2_Event_DocumentBeforeClose
    System.Windows.Forms.MessageBox.Show("The document is closing.")
End Sub


Private Sub oWord_ApplicationEvents2_Event_DocumentBeforeSave(Doc As Word.Document, ByRef SaveAsUI As Boolean, ByRef Cancel As Boolean) Handles oWord.ApplicationEvents2_Event_DocumentBeforeSave
    System.Windows.Forms.MessageBox.Show("The document is saving.")
End Sub
'Handler for the Microsoft Word NewDocument event.
'The NewDocument event is fired when a new document is created.

Private Sub oWord_ApplicationEvents2_Event_NewDocument(Doc As Word.Document) Handles oWord.ApplicationEvents2_Event_NewDocument
    'Add some text to the new document.
    With oWord.Selection
        .TypeText("The ")
        With .Font
            .Bold = Word.WdConstants.wdToggle
            .Italic = Word.WdConstants.wdToggle
        End With
        .TypeText("NewDocument ")
        With .Font
            .Bold = Word.WdConstants.wdToggle
            .Italic = Word.WdConstants.wdToggle
        End With
        .TypeText("event handler inserted this text.")
    End With
End Sub
End Class
我有对Microsoft Office Interop和Microsoft Word对象库的引用


我在.ApplicationEvents2\u Event\u DocumentBeforeClose事件上找到了文档,该文档声明它不打算从代码中使用,但提供了一种声明和使用它的方法,因此我不确定我是否使用了错误的语法或一起使用了错误的方法。

VB.NET

您可以通过计时器计算字进程。这只能在计时器比用户打开和关闭另一个文档快的情况下正常工作

Private m_wordProcessCount As Integer = 0
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim processCount As Integer = Process.GetProcessesByName("WinWord").Count()
    If Not processCount < m_wordProcessCount Then
        'a word process has been closed..
        doSomething()
        'update our wordcount
        m_wordProcessCount = processCount
    End If
End Sub

Private Sub Document_Close()   
  Dim strPath As String
  strPath = Dir("C:\Program Files (x86)\SomeProgram\Program.exe")
  'run the external exe
  Shell strPath
End Sub