0
votes

I have a Word template with a class which is being registered in both new document and open document. The aim is to intercept a document being saved, so I'm using the DocumentBeforeSave event handler in the class:

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

The VBA is within a template, and I'm selecting the template from those offered by Word when doing 'new'. I'm using Word 365.

It works fine if I close a document using the x in the top right of the window and click 'save', and also when I actually click the save button in the 'save as', as shown in the image

enter image description here

This is too late, however, I would like it to trigger before the browsing option appears. Is this possible - in other words, intercept 'save as' as well as the actual save?

More importantly, the event doesn't trigger at all if I use the disk icon to save. I recognise that the template is different to the document itself, does that mean that the events which do trigger are actually still specific to the template and not the document? What am I missing to get the event to trigger on clicking the disk icon?

For completeness, I have also used a KeyBinding to call my function for Ctrl S and this works fine.

1

1 Answers

0
votes

I would not say that really "This is too late"...

You can intercept the event, check if SaveAsUI is True and in such a case, send a warning message to the user saying that SaveAs is not allowed, then make Cancel = True and the workbook will not be saved, any more. Wouldn't it be enough?

 If SaveAsUI Then
    MsgBox "SaveAs is Not allowed!"
    Cancel = True
 End If

I do not have now time to check that in Word, but in Excel the event is triggered before the browse for SaveAs is open...