Results 1 to 5 of 5

Thread: vb. Net how to capture the save () event of word?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2021
    Posts
    19

    vb. Net how to capture the save () event of word?

    I use VB.Net calls word2010 in com mode, but how to capture the save () event of word

  2. #2
    Fanatic Member
    Join Date
    Jun 2019
    Posts
    579

    Re: vb. Net how to capture the save () event of word?

    VB.NET Code:
    1. Private _word As Word.Application
    2.  
    3. ...
    4. ' Get or create Word application object
    5. _word = ...
    6. AddHandler _word.DocumentBeforeSave, AddressOf DocumentBeforeSave
    7. ...
    8.  
    9. Private Sub DocumentBeforeSave(Doc As Word.Document, ByRef SaveAsUI As Boolean, ByRef Cancel As Boolean)
    10.     Debug.Print(Doc.Name)
    11.     Cancel = False
    12. End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2021
    Posts
    19

    Re: vb. Net how to capture the save () event of word?

    Quote Originally Posted by peterst View Post
    VB.NET Code:
    1. Private _word As Word.Application
    2.  
    3. ...
    4. ' Get or create Word application object
    5. _word = ...
    6. AddHandler _word.DocumentBeforeSave, AddressOf DocumentBeforeSave
    7. ...
    8.  
    9. Private Sub DocumentBeforeSave(Doc As Word.Document, ByRef SaveAsUI As Boolean, ByRef Cancel As Boolean)
    10.     Debug.Print(Doc.Name)
    11.     Cancel = False
    12. End Sub

    Thank you. It works.

    Unfortunately, word has no documentaftersave event.

    Well, I want to use VB immediately after the document is saved Net code saves the document to the BLOB field of the data table.

  4. #4
    Fanatic Member
    Join Date
    Jun 2019
    Posts
    579

    Re: vb. Net how to capture the save () event of word?

    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:
    1. Private _saveActive As Boolean = False
    2.  
    3. Private Sub DocumentBeforeSave(Doc As Word.Document, ByRef SaveAsUI As Boolean, ByRef Cancel As Boolean)
    4.     If _saveActive Then
    5.         Return      ' If save is active then just return
    6.     End If
    7.  
    8.     _saveActive = True
    9.     If SaveAsUI Then
    10.         ' Show UI when SaveAs or new file is saved - needs user interaction to select file
    11.         Dim saveDialog = _word.Dialogs.Item(Word.WdWordDialog.wdDialogFileSaveAs)
    12.         saveDialog.Show()
    13.     Else
    14.         Doc.Save()
    15.     End If
    16.  
    17.     Dim filename = Doc.FullName     ' Grab the file name of the document
    18.  
    19.     '
    20.     ' Do whatever you want with the file - upload to network storage, save in db BLOB, ...
    21.     '
    22.  
    23.     _saveActive = False
    24.     Cancel = True                   ' Cancel the save operation as document was just saved
    25. 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.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2021
    Posts
    19

    Re: vb. Net how to capture the save () event of word?

    Thank you. I see. It is to execute the save method in the documentbeforesave event

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width