vbams-wordword-2013

Match SaveAs2 Dialog File Type To Application.FileDialog


Say you want to have a button that the user can click and save a copy of the current file as a PDF(Documentation):

Application.ActiveDocument.SaveAs2 fileName:="fileName.pdf", FileFormat:=wdFormatPDF

This works fine, the user is presented with a save dialog, selects a location and the file is saved, however a few things are not correct:

enter image description here

The type displayed does not match what was specified in the VBA, how can this be correct? It still saves as type "PDF" without issue, even after showing "DOCX" as the file type in the "Save as Type" drop down. Also the "fileName.pdf" is not placed in the "File Name" box, its as if the dialog box is unaware of the options set in the VBA code(This same issue is also referenced in this post).

UPDATE 1

After taking a second look at my code I now realize that the SaveAs2 Method was not displaying the dialog menu, the correct version of the code(simplified) can be described as:

Dim selected As String: selected = Application.FileDialog(msoFileDialogSaveAs).Show()
Dim filePath As String

If selected <> 0 Then
    filePath = Application.FileDialog(msoFileDialogSaveAs).SelectedItems(1)
    Application.ActiveDocument.SaveAs2 fileName:=Split(filePath, ".")(0), FileFormat:=wdFormatPDF
End If

So then the real question(I guess) is how do you get "Application.FileDialog" to display the proper type you wish to save in under the "Save as type" drop down, and this has already been answered by @PatricK. Thanks everyone for the help, I apologize for the initial confused nature of this question.


Solution

  • I am surprised for SaveAs2 will bring you a prompt to be honest - Only a new document and .Save will bring you that prompt.

    If you want to get something similar to that prompt, you use Application.FileDialog with type msoFileDialogSaveAs.

    Use this code below (perhaps as an AddIn suits more):

    Option Explicit
    
    Sub MySaveAs()
        Dim oPrompt As FileDialog, i As Long, sFilename As String
        Set oPrompt = Application.FileDialog(msoFileDialogSaveAs)
        With oPrompt
            ' Find the PDF Filter from Default Filters
            For i = 1 To .Filters.Count
                'Debug.Print i & " | " & .Filters(i).Description & " | " & .Filters(i).Extensions
                ' Locate the PDF filter
                If InStr(1, .Filters(i).Description, "PDF", vbTextCompare) = 1 Then
                    .FilterIndex = i
                    Exit For
                End If
            Next
            ' Change the title and button text
            .Title = "Saving """ & ActiveDocument.Name & """ to PDF format"
            .ButtonName = "Save to PDF"
            ' Default name
            .InitialFileName = ActiveDocument.Name
            ' Show the Prompt and get Filename
            If .Show = -1 Then
                sFilename = .SelectedItems(1)
                Debug.Print "Final filename: " & sFilename
                ' Save the file as PDF
                ActiveDocument.SaveAs2 sFilename, wdFormatPDF
            End If
        End With
        Set oPrompt = Nothing
    End Sub
    

    Screenshot sample:
    ResultingFileDialogBox