PDA

View Full Version : [SOLVED:] File SaveAs executing twice



El_Diabolo
12-31-2013, 06:20 PM
*** Sorry just noticed the typo in the title. Should say: "twice", not "twicen". (No drink taken).

Hi All,

Happy New Year. (At least it is New Year in Scotland).

I normally make a nuisance of myself in the Excel forum, but tonight it's the Word forum's turn to be unlucky.

I know very little about Word, so my question may well be entirely stupid, but here goes.

I am using Office 2010, 32-bit on a stand-alone laptop.

I have the following code in my document:



' This procedure sets the file extension as "docx" before save prompt


Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
With Application.Dialogs(wdDialogFileSaveAs)
.Format = wdFormatXMLDocument
.Show
End With


End Sub


The active document at this point is a "docm" file as I have some VBA code in it. However, when the user saves the document I want it to save as a "docx" file. Therefore, I am trying to set the extension to save the user doing it. (All the Application code is already in place). The "docx" extension does show in the dialog box and the document saves as a "docx" file, without the VBA from the original file. However, the dialog box shows a second time, with a "docm" file type specified (as per the original file, which is still open). My question is how do I stop the dialog box showing a second time? Or, is there a better way of doing what I am trying to do?

Many thanks and best regards.

macropod
12-31-2013, 10:34 PM
Since you're calling the SaveAs dialogue within the before_save event, you'll get two save prompts for an unsaved document. is there a reason for not using 'SaveAsUI = True' to call the SaveAs dialogue? See the 'Application.DocumentBeforeSave Event' in Word's VBA help file.

El_Diabolo
01-01-2014, 05:01 AM
Hi Paul and thank you for your reply.

I looked at your suggested solution, but I don't understand how it works.i.e. why/how does setting the SaveAsUI = True call the SaveAs dialogue? Also, if using this approach how can I set the document type so that the user doesn't have to do it? If you could provide a code example that would be perfect, please. Many thanks.

El_Diabolo
01-01-2014, 10:04 AM
I managed to solve it. All that was required was to insert "Cancel = True" after the "End With" in the code I posted in post #1. I guess that makes me a VBAX master.