Reputation: 134
I have tried many different ways of doing this but I can't find one that works for the application.
I have a MS Word Template that will be used by another team.
This is the criteria I have been given.
The closest I can get to this working is this: -
In the top section I have this: -
Private WithEvents App As Word.Application
and Dim n as long
Then in the Document New I have: -
Private Sub Document_New()
Set App = Word.Application
n = 0
End Sub
Then for the execution I have done this: -
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
ActiveDocument.SaveAs2 "PL" & Right(ActiveDocument.SelectContentControlsByTitle("Works Order Number").Item(1).Range.Text, 5) & " Issue " & Format(n, "00") & ".docx", wdFormatDocumentDefault
End Sub
However, this saves before the user has chosen a location. It works but the user needs to chose the location. So I tried this, this just does the same thing.
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
If Cancel = False Then
n = n + 1
ActiveDocument.SaveAs2 "PL" & Right(ActiveDocument.SelectContentControlsByTitle("Works Order Number").Item(1).Range.Text, 5) & " Issue " & Format(n, "00") & ".docx", wdFormatDocumentDefault
ElseIf Cancel = True Then
Exit Sub
End If
End Sub
Any suggestions or help would very much be welcomed. I basically just want to suggest the filename which up issues with every save as. But I cant find a way to influence that without physically saving.
Thank you in advance for your time and support.
Upvotes: 1
Views: 203
Reputation: 49455
You can repurpose ribbon controls (Save) to call your event handler first. Moreover, if required, you may cancel the default action in the event handler. See Temporarily Repurpose Commands on the Office Fluent Ribbon for more information. Repurposing ribbon controls give you a big plus - corresponding keyboard shortcuts are handled by your code as well. So, you will be able to intercept keyboard shortcuts by the same code.
In case of Backstage UI you can hide the SaveAs
button and add your own. Read more about the Backstage UI in the following articles:
Upvotes: 1
Reputation: 7860
You really need to learn how to use the online documentation for VBA. If you had looked up DocumentBeforeSave
you would have learned that Cancel
is always False
when the event is triggered. If you set it to True
in the event handler it cancels the save.
You need to intercept the save before the dialog has been displayed, but the event is only triggered after the dialog. Because the criteria for Save
is to use the standard functionality it is only the FileSaveAs
you need to intercept.
Prior to the implementation of the Backstage view (the File tab) this could be solved simply by creating a routine named FileSaveAs
. You can still do this and it will intercept the keyboard shortcut or clicking the QAT button. But it will not intercept the backstage commands. Only the event can do that without rebuilding the Backstage view, and the event won't work for you...
As far as intercepting the dialog to set the initial file name, that has been covered on SO before, here for example.
Upvotes: 0