PDA

View Full Version : SaveAs Dialog box in Word 2016



max76
03-21-2018, 02:57 PM
Hi everyone,

I need your help. I'm trying to get the VBA code to call the SaveAs dialog box so that when a user saves the document for the first time the name (based on the information of the Content Control in the file) and file format (.docm) are preloaded and the user simply needs to manually choose the folder where to save the file (I used an event to intercept printing which works wonderfully - I didn't include it in the attached because I want to keep the focus on the SaveAs dialog box). I didn't have any issues at all using the code below with Word 2010 (see the attached file): In fact, I can call the SaveAs dialog with preset File Name and Format:

-------------------------------------------------------------------------------------------------------------------
Sub SaveAs_Dialog()
Dim dlgsaveas As Dialog
Set dlgsaveas = Dialogs(wdDialogFileSaveAs)
With dlgsaveas
.Name = ActiveDocument.SelectContentControlsByTag("Name").Item(1).Range.Text & " - " & ActiveDocument.SelectContentControlsByTag("Number").Item(1).Range.Text
.Format = wdFormatXMLDocumentMacroEnabled
.Show
End With
End Sub
-------------------------------------------------------------------------------------------------------------------

Unfortunately though this code doesn't work with Word 2016 since the dialog box is different and I guess it's triggered through a different code. I found some code to call the SaveAs Dialog in Word 2016 but I couldn't fine any code where I can set the "Name" and "Format" as I was able to easily do in in the code above.

Thank you in advance for your support!

Regards

Massimo

gmayor
03-22-2018, 12:47 AM
It works here in Word 2016?
If you want it to save with the indicated name the first time it is saved then (and you shoukld include some error trapping to ensure the fields are filled, and you won't be able to use Save to save your changes to the code.


Option Explicit

Sub SaveAs_Dialog()
Dim dlgsaveas As Dialog
Set dlgsaveas = Dialogs(wdDialogFileSaveAs)
With dlgsaveas
.Name = ActiveDocument.SelectContentControlsByTag("Name").Item(1).Range.Text & " - " & _
ActiveDocument.SelectContentControlsByTag("Number").Item(1).Range.Text
.Format = wdFormatXMLDocumentMacroEnabled
.Show
End With
End Sub

Sub FileSave()
If Not ActiveDocument.Name = ActiveDocument.SelectContentControlsByTag("Name").Item(1).Range.Text & " - " & _
ActiveDocument.SelectContentControlsByTag("Number").Item(1).Range.Text & ".docm" Then
SaveAs_Dialog
Else
ActiveDocument.Save
End If
End Sub

max76
03-22-2018, 08:47 AM
Hi Graham,

thank you for your reply. However, the code that I was already using does not trigger a Save Dialog when the button Save is clicked in Word 2016 (whereas it worked well on Word 2010 because the interface has changed unfortunately...)

Sub SaveAs_Dialog()
Dim dlgsaveas As Dialog
Set dlgsaveas = Dialogs(wdDialogFileSaveAs)
With dlgsaveas
.Name = ActiveDocument.SelectContentControlsByTag("Name").Item(1).Range.Text & " - " & _
ActiveDocument.SelectContentControlsByTag("Number").Item(1).Range.Text
.Format = wdFormatXMLDocumentMacroEnabled
.Show
End With
End Sub

If I click on Save to Save the file for the first time I get the attached Save dialogue which is different. I'm trying to get a control on the attached SaveAs dialogue which the code does not.

Awaiting your feedback.

Thank you in advance for your support.

Regards

Massimo

max76
03-24-2018, 07:19 AM
Hi again,

what I'm trying to accomplish with Word 2016 (which I'm forced to use for work purposes) is what I was EASILY able to do with Word 2010 and I really find it hard to believe that such an (apparently) easy task to perform is actually very complicated with Word 2016.

► In Word 2010 when saving a file for the first time the SaveAs dialog below is displayed and I could simply take control over it by populating the default name with the name (based on 2 text strings in the document) and the file format (in my case was a .docm file) that I wanted simply by using the following macro (placed within a BeforeSave event)

Dim dlgsaveas As Dialog
Set dlgsaveas = Dialogs(wdDialogFileSaveAs)
With dlgsaveas
.Name = ActiveDocument.SelectContentControlsByTag("DBA").Item(1).Range.Text & " - " & ActiveDocument.SelectContentControlsByTag("Case").Item(1).Range.Text
.Format = wdFormatXMLDocumentMacroEnabled
.Show
End With

https://public.boxcloud.com/api/2.0/internal_files/284722608346/versions/299580105706/representations/jpg_paged_2048x2048/content/1.jpg?access_token=1!D6otT8tF183WBNB8emdSo94GC6w_J3RkNuDgIowBBSbO95qbWmH_XW qkhhFmqJARTXmFUMTsGV7EzkHY3902gfkaev779bDciUvTUaoYdoZXWC-ILzzChx1pF8Xa8MxxlPMqSeiWqXG5U6IUHsEsxGFZ6ChFYTG9aJhKS2RNpjZR6dp44lKdAcSs20 32xZgkgzcg5OE7G4Lp-Yi1QuWydDNwEmUlCwRPKO199t1h_Eett7K5IRVDdIOZ05iF_MeeHLTxyd1_2mHv8lTbsNix2uTE Qf1mTkw3z8cfIlWdODmdaZ_KG7rZ1_mqYih8hIpNFmMJuHLWqDzaQbl9d10R91IL6ZwUUnH6uC7 KkwZvLmvGI7dmTPT8jMCSBRVVxi7erKMuBAQENYs.&box_client_name=box-content-preview&box_client_version=1.34.0


► Now, with Word 2016 unfortunately it's a different story. When saving the file for the first time the following dialog box is displayed and there seems to be no way to control the default suggested filename not to mention the file format. In fact, even when I use a class module to intercept Saving and use the code before I cannot get it done. It sounds like the new SaveAs dialog cannot be fully controlled by VBA.

https://public.boxcloud.com/api/2.0/internal_files/284718772155/versions/299576272971/representations/jpg_paged_2048x2048/content/1.jpg?access_token=1!ey2t8y2XSt3joMadtLawZX6XB_GkA7pFFGxSbdKg66Z5znVBC_VlJM 93cRTePDRhbghUGBWvHwLHsn8PHD6iWw0lgvBAITJcjLdVKOJECv4_YwjWu6bhWJUkqMLzKCaJg 3DPk9GL08pHog1Il0tqL8RIKL4VDdDDTwzHpPg3ewyYRwXwyl3IbXMC0t32qeVPuzGmWCq4Weji gmwRJU3LzW49IOq_mgvL8L4kbNkEeuh3lWNDy40CGanpO4Sl3hr2aI32pru57sqmFnNgOqM7qw3 _c-gQqugg6Z_tYUrKnk__4zfCh8rtZm0HjSkFm_Zr2rnV8PH9q0-64o_Vr_fC0vGSkvN3TpHtZrQMMuJcZdnq42DGacbflcwAlM1tZ0s9VYleNFu4QJc.&box_client_name=box-content-preview&box_client_version=1.34.0

Someone has suggested programatically editing the title of the document which is then reflected into the default suggested file name but unfortunately delimiters (such as -, _, ' & and others) are not transferred from the title name to the file name in the SaveAs dialog which is an issue for my purposes. So this workaround is not satisfactory.

I noticed that if one clicks on "More option" right under the file format one can access the "old" SaveAs dialog.

I'm wondering if someone can help me figure out how to accomplish something that used to be quite hassle-free with Word 2010:

• either by taking full control over the "new" SaveAs dialog
• or at least programatically clicking on "More Options" to get to the VBA-friendlier dialog.

Thank you in advance for your support. There must be a way to get it done!!

Massimo

gmaxey
03-25-2018, 05:34 AM
You have to use brute force. Add the following RbbonX to your Word 2016 template:

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" >

<backstage>

<!--Kill the offending backstage tab-->
<tab idMso ="TabSave" visible="false"/>

<!--Add your new custom button to take its place-->

<button id="btnFileSaveAs" label="SaveAs" insertAfterMso="FileSave"
onAction="ButtonOnAction" isDefinitive="true" />

</backstage>

</customUI>


Add the following to a standard code module:


Sub ButtonOnAction(control As IRibbonControl)
Select Case control.ID
Case "btnFileSaveAs": FileSaveAs
Case Else
'Do Nothing
End Select
lbl_Exit:
Exit Sub
End Sub

Sub FileSaveAs()
Dim oDialog As Dialog
Set oDialog = Application.Dialogs(wdDialogFileSaveAs)
With oDialog
.Name = "Test"
.Format = wdFormatXMLDocumentMacroEnabled
.Show
End With
lbl_Exit:
Exit Sub
End Sub

max76
03-25-2018, 06:53 AM
Hi Greg,

thank you very much for your email. How do I go about adding the RbbonX to the Word 2016 template? Can this be done programatically via VBA? Keep in mind that my template will be used by many other users on different computers therefore this should be made programatically.

In addition, once this ribbon is added to Word 2016 in a PC is that forever or should it be one each time my template is used?

Out of curiosity I checked that website and I got the message: "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.". Does it mean it cannot be used?

Sorry for my questions but believe me I've been trying to get this accomplished for a few weeks and I really hope a proper solution can be found since it was extremely easy with Word 2010 whereas it's turning out to be a nightmare with Word 2016.

Awaiting your feedback.

Regards

Massimo

gmaxey
03-25-2018, 01:51 PM
You need the Office CustomUI Editor: http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2009/08/06/7293.aspx