Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

gpf occurrence in VBA with Word 2002

Status
Not open for further replies.

kcalder

Programmer
Joined
Apr 13, 2005
Messages
2
Location
GB
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

 
Apols to any who looked at the initial posting and sighed. Code is properly embedded as below. The problem appears to be with the SaveAsUI parameter which, when set to False in order to suppress the appearance of the default SaveAs dialog, crashes the routine and Word with it. If I allow SaveAsUI by default (i.e., True) by commenting out the line SaveAsUI = False, the app doesn't crash but the default SaveAs dialog box appears. I don't want it to, UNLESS I can populate the InitialFileName parameter with my own filename string, but I cannot find a method for doing that. Of course, I may be mistaken putting all of this code into the Application level but I can't see a way of embedding it into the Document.

Code:
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     
        .InitialFileName = "C:\" & szFileName
        If .Show = -1 Then
             'Save if user presses OK
            .Execute
            'Show the correct filename in the titlebar
            ActiveDocument.ActiveWindow.Caption = szFileName
        End If
    End With
    
End If

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top