Question

I'm writing a custom Word template which does some processing upon saving the current document.

In Office2007, to save a document you can use the Save and the Save As functions, and handle the events with the FileSave and FileSaveAs macros.

But when the user hovers over the SaveAs option, other sub-options are displayed: Save as a document, as a Word template, as Word 97-2003 document, etc. These sub-options don't seem to have their own events, but I'd like to know when the user uses them.

So I came up with the idea to use the DocumentBeforeSave event, but then I still have to figure out if the save occured with the standard Save/SaveAs options or with the sub-options.

I thought about setting a variable to True in the Save/SaveAs functions, which the DocumentBeforeSave event would check to see if one of the normal save methods occured, then it would set the variable back to False.

But after experimenting with different methods, I can't figure out how I can pass the value of a variable between ThisDocument and the Class Module which has the BeforeSave event.

Any ideas? Thanks!


Edit: Example code that doesn't work:

ThisDocument:

Public pSSave As Boolean

Public Property Get SSave() As Boolean
    SSave = pSSave
End Property

Public Property Let SSave(Value As Boolean)
    pSSave = Value
End Property

Sub FileSave()

Me.SSave = True

If SSave = True Then
    MsgBox "True"
End If

Application.ActiveDocument.Save

If SSave = True Then
    MsgBox "True"
End If

End Sub

Class Module:

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

    If Application.ActiveDocument.SSave = False Then
        MsgBox "False"
    End If
End Sub

The class module registration is done properly, I won't paste the code her.

The result displayed is True, False, True while theoretically, it should be True, True.

Was it helpful?

Solution

I still miss something in your logic. In comments I thought about different-reverse logic which would go this way. This code below is a mix of my way and code you presented.

Class Module

Public WithEvents App As Word.Application

Public pSSave As Boolean   'your class variable/property

Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
If pSSave = False Then
    MsgBox pSSave
Else
    MsgBox pSSave
End If
End Sub

Module1

'class initialization
Public wrdAPP As New myClass
Sub set_References()
    Set wrdAPP.App = Application
End Sub

ThisDocument Module

Private Sub Document_Open()
'to initialize public variable when open
    Call Module1.set_References
End Sub

Sub FileSave()

wrdAPP.pSSave = True

Application.ActiveDocument.Save

If wrdAPP.pSSave = True Then
    MsgBox "True"
End If

End Sub

I don't know which way you are going to run FileSave sub. But after it is run it pass the value to class property which you could check in your event. Hope it would help you anyway.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top