Friday, 8 April 2011

Open a ‘Save As’ file dialogue box for Word using VBA

It may be necessary to complete an action in Word or Excel and then automatically pop up a ‘Save As’ dialogue . VBA_logo

In my case not only did I need to pop up a Save window but I also needed to give it a pre-determined name. Once the user had entered some details and submitted the information for the VBA code to act on, I didn’t want them to alter the document any further. In addition, I also wanted to standardise the word document and cut down on the work the user had to do.

Here is the code that will produce a Save window with the pre-populated name of ‘MyFileName’.

Dim fNameText As String
fNameText = “MyFileName”
With Dialogs(wdDialogFileSaveAs)
.Name = fNameText
.Show
End With


In the above example I have declared a variable, assigned it some text and then used the variable to populate the filename in the Save As dialogue.


Therefore, when the user saves the document it will be called “MyFileName.doc”.

NB. You could also just add the filename directly without a variable :

With Dialogs(wdDialogFileSaveAs)
.Name = “MyFileName”
.Show
End With

No comments:

Post a Comment