none
How to react to Microsoft Word events? RRS feed

  • Question

  • My program runs Microsoft Word (setting "ws" = Word). I can issue commands to Word, but can I also react to Word events? I set ws to be "withevents" but that didn't seem to make wd's events available to my program.

    Robert Homes

    Monday, March 16, 2015 12:41 PM

Answers

  • Hi Robert,

    Based on the descrption, you want to subscribe the event for Word using VB.Net. Are you writing an Word automation application?

    If I understood correctly, it is similar as use other event in VB.Net and here is an sample that add an document_Open event for the Word application for your reference:

    Imports Word = Microsoft.Office.Interop.Word
    
    Class TestWordEvent
    
        Private WithEvents wordAppEvent As Word.Application
    
    
        Public Sub NewDocument()
    
            Dim wordApp As New Word.Application
            wordAppEvent = wordApp
            wordApp.Visible = True
            wordApp.Documents.Open("C:\users\UserName\desktop\doc1.docx")
            
        End Sub
    
        Private Sub Document_Open(ByVal Doc As Word.Document) Handles wordAppEvent.DocumentOpen
            MsgBox("")
        End Sub
    
    End Class
    

    Also you can get more events about Word applicaiton from link below:
    Application Events (Word)

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, March 18, 2015 2:59 AM
    Moderator

All replies

  • Hello,

    The best place to ask this question is in the dedicated forum for Word. I can move you question there if you want me too~


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Monday, March 16, 2015 12:52 PM
  • See the below link. It's for Office XP but it should work with later versions as well.

    How To Handle Events for Microsoft Word Using Microsoft Visual Basic .NET


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Monday, March 16, 2015 2:09 PM
  • The best place to ask this question is in the dedicated forum for Word. I can move you question there if you want me to

    Yes, please move me there. Thanks.


    Robert Homes

    • Proposed as answer by Cor Ligthert Monday, March 16, 2015 2:29 PM
    • Unproposed as answer by Cor Ligthert Monday, March 16, 2015 7:19 PM
    Monday, March 16, 2015 2:17 PM
  • Seems more like a VB.NET/Word automation question to me. Not sure why your response is proposed as an answer.

    Did you check out the MS KB article I posted? Is your code the same as that in the article?


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Monday, March 16, 2015 3:52 PM
  • Robert,

    I see Kevin has still not moved you, I'm not able to do that but maybe you can ask it yourself in the visual studio to office forum.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vsto


    Success
    Cor

    Monday, March 16, 2015 7:19 PM
  • Hi Robert,

    Based on the descrption, you want to subscribe the event for Word using VB.Net. Are you writing an Word automation application?

    If I understood correctly, it is similar as use other event in VB.Net and here is an sample that add an document_Open event for the Word application for your reference:

    Imports Word = Microsoft.Office.Interop.Word
    
    Class TestWordEvent
    
        Private WithEvents wordAppEvent As Word.Application
    
    
        Public Sub NewDocument()
    
            Dim wordApp As New Word.Application
            wordAppEvent = wordApp
            wordApp.Visible = True
            wordApp.Documents.Open("C:\users\UserName\desktop\doc1.docx")
            
        End Sub
    
        Private Sub Document_Open(ByVal Doc As Word.Document) Handles wordAppEvent.DocumentOpen
            MsgBox("")
        End Sub
    
    End Class
    

    Also you can get more events about Word applicaiton from link below:
    Application Events (Word)

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, March 18, 2015 2:59 AM
    Moderator