Consulting

Results 1 to 7 of 7

Thread: SaveAs Dialog box in Word 2016

  1. #1
    VBAX Regular
    Joined
    Apr 2017
    Posts
    34
    Location

    SaveAs Dialog box in Word 2016

    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
    Attached Files Attached Files
    Last edited by max76; 03-21-2018 at 03:14 PM.

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Apr 2017
    Posts
    34
    Location
    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
    Attached Images Attached Images

  4. #4
    VBAX Regular
    Joined
    Apr 2017
    Posts
    34
    Location
    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





    ► 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.



    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

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    VBAX Regular
    Joined
    Apr 2017
    Posts
    34
    Location
    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

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    You need the Office CustomUI Editor: http://openxmldeveloper.org/blog/b/o...8/06/7293.aspx
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •