Pregunta

I need to open an existing Word document from within my VB 2010 application and detect when the user has closed the document in order to copy that document to a database, whether the user modified it or not. How do I accomplish this? Thanks.

Example:

'Code here
Dim word as New Word.Application()
Dim doc as New Word.Document()

'open the document here and let the user work on the word document
'detect when user has closed Word
'copy the word document into database
¿Fue útil?

Solución

I am editing this to show the complete code solution, this also terminates the application.

Imports Microsoft.Office.Interop

Class MainWindow
    Private WithEvents doc As Word.Document
    Private app As New Word.Application
    Public Sub New()

        InitializeComponent()

        app.Visible = True
        app.Documents.Add()
        doc = app.Documents(1)
        doc.Activate()
    End Sub

    Private Sub doc_Close() Handles doc.Close
        app.Quit()
    End Sub
End Class

Otros consejos

Here is an example on how to create a new document. Maybe this will get you started?

http://support.microsoft.com/kb/316383

Another good example:

http://www.codeproject.com/Articles/55685/Word-Automation-using-VB-NET-Part-I

this will work although it may not be the best choice:

  1. before open the doc file, get all processes' pid (like the pid in taskmgr). each opened word file has a pid.

  2. now, have a thread, check this at certain interval, if the pid does not exist anymore, the doc file is closed.

not a elegant solution, but it works in our project

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top