I am new to VBA, having been developing in Visual C++ for a few years, and it takes a bit of getting used to! I have written a small project that takes a userform to layout text on a page and then intercepts the SaveAs command so that the filename format is controlled. I keep getting a gpf message from Word but cannot work out why.
I won't bother posting the form code because I know that works, but I am trapping the SaveAs at App level through running an AutoExec routine and the code for the ClassModule "ThisApplication" is below. Perhaps there is something wrong with the code that I cannot see. All help gratefully received!
Option Explicit
Public WithEvents oApp As Word.Application
Private Sub oApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
Dim szFileName As String, szSeparator As String, szTheDate As String, szProjectTitle As String
Dim dlgSaveAs As FileDialog
'Define the filename component separator
szSeparator = " - "
'Format the date
szTheDate = Format(Date, "yy.mm.dd")
'Pass the filename string to the save dialog
If ActiveDocument.Saved = False Then
'If currently protected, unprotect the document before save
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:="sustainable"
End If
'The filename is formatted as follows:
'yymmdd - qms - Project registration - Project Title.doc
'Note that g_szProjectTitle is a global string whose value is set when the string is
'entered on the form by the user
szFileName = szTheDate & szSeparator & "qms" & szSeparator & "Project registration" & _
szSeparator & g_szProjectTitle
'Do not show the standard dialog afterwards
SaveAsUI = False
'Save the file when OK is clicked
Cancel = False
'Call the SaveAs dialog box with the correct filename
Set dlgSaveAs = Application.FileDialog(FileDialogType:=msoFileDialogSaveAs)
With dlgSaveAs
.InitialView = msoFileDialogViewDetails 'see the details view of the dialog
.InitialFileName = "C:\" & szFileName
If .Show = -1 Then
'Save if user presses OK
.Execute
'Show the correct filename in the document titlebar
ActiveDocument.ActiveWindow.Caption = szFileName
End If
End With
End If
End Sub
I won't bother posting the form code because I know that works, but I am trapping the SaveAs at App level through running an AutoExec routine and the code for the ClassModule "ThisApplication" is below. Perhaps there is something wrong with the code that I cannot see. All help gratefully received!
Option Explicit
Public WithEvents oApp As Word.Application
Private Sub oApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
Dim szFileName As String, szSeparator As String, szTheDate As String, szProjectTitle As String
Dim dlgSaveAs As FileDialog
'Define the filename component separator
szSeparator = " - "
'Format the date
szTheDate = Format(Date, "yy.mm.dd")
'Pass the filename string to the save dialog
If ActiveDocument.Saved = False Then
'If currently protected, unprotect the document before save
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:="sustainable"
End If
'The filename is formatted as follows:
'yymmdd - qms - Project registration - Project Title.doc
'Note that g_szProjectTitle is a global string whose value is set when the string is
'entered on the form by the user
szFileName = szTheDate & szSeparator & "qms" & szSeparator & "Project registration" & _
szSeparator & g_szProjectTitle
'Do not show the standard dialog afterwards
SaveAsUI = False
'Save the file when OK is clicked
Cancel = False
'Call the SaveAs dialog box with the correct filename
Set dlgSaveAs = Application.FileDialog(FileDialogType:=msoFileDialogSaveAs)
With dlgSaveAs
.InitialView = msoFileDialogViewDetails 'see the details view of the dialog
.InitialFileName = "C:\" & szFileName
If .Show = -1 Then
'Save if user presses OK
.Execute
'Show the correct filename in the document titlebar
ActiveDocument.ActiveWindow.Caption = szFileName
End If
End With
End If
End Sub