DocumentBeforeSave event
DocumentBeforeSave event
DocumentBeforeSave event
DocumentBeforeSave event
DocumentBeforeSave event
DocumentBeforeSave event DocumentBeforeSave event DocumentBeforeSave event DocumentBeforeSave event DocumentBeforeSave event DocumentBeforeSave event DocumentBeforeSave event DocumentBeforeSave event
DocumentBeforeSave event DocumentBeforeSave event
DocumentBeforeSave event
Go Back  Xtreme Visual Basic Talk > > > DocumentBeforeSave event


Reply
 
Thread Tools Display Modes
  #1  
Old 01-08-2014, 03:24 AM
george_m george_m is offline
Newcomer
 
Join Date: Mar 2012
Posts: 20
Default DocumentBeforeSave event


Hi,

I'm trying to use DocumentBeforeSave event on opened Word document. My goal is to save backup copy of document whenever user save's it. I've made a new class like this:

Code:
HTML Code:
Imports Word = Microsoft.Office.Interop.Word

Public Class Class1

    Public WithEvents letter As Word.Application

    Private Sub letter_DocumentBeforeSave(Doc As Global.Microsoft.Office.Interop.Word.Document, ByRef SaveAsUI As Boolean, ByRef Cancel As Boolean) Handles letter.DocumentBeforeSave
        Me.Application.ActiveDocument.SaveAs("C:\Test\NewDocument.docx")
    End Sub
   
End Class
In the main class I used connection with Class1:

Code:
  Dim X As New Class1

    Sub Register_Event_Handler()
        X.App = Word.Application
    End Sub
but I'm doing something wrong
Reply With Quote
  #2  
Old 01-09-2014, 06:37 AM
george_m george_m is offline
Newcomer
 
Join Date: Mar 2012
Posts: 20
Default

Found mistake, but still doesn't work. In main class I'm opening Word document like this:

Code:
Dim predloga As New Word.Application
Dim dopis As Word.Document
dopis = predloga.Documents.Open("P:\Dopis_predloga.docx")
New class:

Code:
Imports Word = Microsoft.Office.Interop.Word

Public Class Class1

    Public WithEvents letter As Word.Application

    Private Sub letter_DocumentBeforeSave(Doc As Global.Microsoft.Office.Interop.Word.Document, ByRef SaveAsUI As Boolean, ByRef Cancel As Boolean) Handles letter.DocumentBeforeSave
        Me.Application.ActiveDocument.SaveAs("C:\Test\NewDocument.docx")
    End Sub
   
End Class
Main application class:

Code:
 Dim X As New Class1

    Sub Register_Event_Handler()
        set X.letter= Word.Application
    End Sub
Reply With Quote
  #3  
Old 01-09-2014, 01:23 PM
Cerian Knight's Avatar
Cerian KnightDocumentBeforeSave event Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,191
Default

It looks like you are performing automation from VB.NET, so I've moved this thread to .NET Office Automation.
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
  #4  
Old 01-09-2014, 04:17 PM
Gruff's Avatar
GruffDocumentBeforeSave event Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

George,

I do not think you can do this sort of thing reliably through automation from VB.NET.
The problem will be that your VB.NET program has no idea which Word Application to attach to.
(There could be several in memory.)

You would probably be better served by creating your utility in Word VBA.
Save it as a Word Template file. (*.Dot)
When your users save the doc started with your template
your code will always run for that Word session and document.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #5  
Old 01-16-2014, 06:06 AM
george_m george_m is offline
Newcomer
 
Join Date: Mar 2012
Posts: 20
Default

I thought it was possible to use DocumentBeforeSave event also through VisualBasic. Thanks for the advice. Now I saved my document as template and wrote this code under the template Class Modules (when opening a template also a new document is created with name Document1):

Code:
Public WithEvents App As Word.Application

Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)

Application.DisplayAlerts = False
ActiveWorkbook.SaveAs FileName:="C:\Users\GregorP\Desktop\Dopis_" + Now

End Sub

    Dim X As New EventClassModule
Sub Register_Event_Handler()
    Set X.App = Word.Application
End Sub
But when I save Document1 with different name nothing happens
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
DocumentBeforeSave event
DocumentBeforeSave event
DocumentBeforeSave event DocumentBeforeSave event
DocumentBeforeSave event
DocumentBeforeSave event
DocumentBeforeSave event DocumentBeforeSave event DocumentBeforeSave event DocumentBeforeSave event DocumentBeforeSave event DocumentBeforeSave event DocumentBeforeSave event
DocumentBeforeSave event
DocumentBeforeSave event
 
DocumentBeforeSave event
DocumentBeforeSave event
 
-->