I use VB.Net calls word2010 in com mode, but how to capture the save () event of word
Printable View
I use VB.Net calls word2010 in com mode, but how to capture the save () event of word
VB.NET Code:
Private _word As Word.Application ... ' Get or create Word application object _word = ... AddHandler _word.DocumentBeforeSave, AddressOf DocumentBeforeSave ... Private Sub DocumentBeforeSave(Doc As Word.Document, ByRef SaveAsUI As Boolean, ByRef Cancel As Boolean) Debug.Print(Doc.Name) Cancel = False End Sub
Inside DocumentBeforeSave event management you have to simulate what Word do and you will have to do the save yourself so you can catch it. Something like this:
VB.NET Code:
Private _saveActive As Boolean = False Private Sub DocumentBeforeSave(Doc As Word.Document, ByRef SaveAsUI As Boolean, ByRef Cancel As Boolean) If _saveActive Then Return ' If save is active then just return End If _saveActive = True If SaveAsUI Then ' Show UI when SaveAs or new file is saved - needs user interaction to select file Dim saveDialog = _word.Dialogs.Item(Word.WdWordDialog.wdDialogFileSaveAs) saveDialog.Show() Else Doc.Save() End If Dim filename = Doc.FullName ' Grab the file name of the document ' ' Do whatever you want with the file - upload to network storage, save in db BLOB, ... ' _saveActive = False Cancel = True ' Cancel the save operation as document was just saved End Sub
If file is new or SaveAs is performed, it is required some user interaction to select destination file name so save dialog is shown. In the other case when no UI is required, the save is just performed. Then you can get from Word.Document object the file name and do whatever you want.
Thank you. I see. It is to execute the save method in the documentbeforesave event