0
votes

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.

1
I'm not sure if I get you right... what is your real goal? allow or disallow saving? take control of the process? or simply passing value from object to Class... It seems that you have some idea but question is unclear. You could add some code to better understand your needs...Kazimierz Jawor
I'd like to detect it and run a snippet of code when the user saves the document with the suboptions (not using the standard Save and SaveAs menu items).Steve
you could create additional property let\get in your class and using that property pass any value to your Class. Or just add additional public variable in your class which could take that value you passKazimierz Jawor
This is exactly what I tried to do. The problem is that for some reason, the class module doesn't "see" the proper value of the variable that is defined in ThisDocument, even if I try to reach it through the property let/get. If you think this should work and have some example code, please share it. Thanks!Steve
it would be much easier if you show that part of code which creates Class variables/property and the way you set Class reference in module. All to review...Kazimierz Jawor

1 Answers

1
votes

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.