vba Application.FileDialog(msoFileDialogSaveAs) 以 .pdf 作为过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21334829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:31:38  来源:igfitidea点击:

Application.FileDialog(msoFileDialogSaveAs) with .pdf as filter

excelvba

提问by CaptainABC

I want the user to select the path and the file name of a PDF file that they are exporting.

我希望用户选择他们正在导出的 PDF 文件的路径和文件名。

I created the below to display the SaveAs dialog:

我创建了以下内容来显示另存为对话框:

Sub Test3()

    Set myFile = Application.FileDialog(msoFileDialogSaveAs)
    With myFile
        .Title = "Choose File"
        .AllowMultiSelect = False
        .ButtonName = "&Save As"
        .initialFilename = "Export"
        .Title = "Save PDF as"
        If .Show <> -1 Then
            MsgBox "You have cancelled"
            Exit Sub
        End If
        FileSelected = .SelectedItems(1)
    End With

End Sub

The value of FileSelectedis later fed into the pdf export code.

的值FileSelected稍后会输入到 pdf 导出代码中。

I want to add a filter to the above FileSave dialog so that it only shows PDF as the file save type.

我想在上面的 FileSave 对话框中添加一个过滤器,以便它只显示 PDF 作为文件保存类型。

Currently the value of FileSelectedends up being "Driveletter:/Folder/Name.xlsm" so when I feed it into the pdf export it saves the pdf with xlsm extension.

目前,FileSelected最终的值是“Driveletter: /Folder/Name.xlsm”,所以当我将它输入到 pdf 导出中时,它会保存带有 xlsm 扩展名的 pdf。

I tried .Filter.Clearand then .Filters.Add.

我试过.Filter.Clear然后.Filters.Add

回答by CaptainABC

Never mind I figured it out:

没关系我想通了:

Sub Test3()

On Error Resume Next
Dim FileSelected As String

FileSelected = Application.GetSaveAsFilename(initialFilename:="Export", _
                                         FileFilter:="PDF Files (*.pdf), *.pdf", _
                                         Title:="Save PDF as")
If Not FileSelected <> "False" Then
MsgBox "You have cancelled"
Exit Sub
End If

If FileSelected <> "False" Then
MsgBox FileSelected 'where FileSelected is what I will be
                    'later feeding into the PDF Export Code
Exit Sub
End If

End Sub

回答by Trex

I added a loop within the option list to run through the file types and select the one I wanted. It has its faults, but works...

我在选项列表中添加了一个循环来遍历文件类型并选择我想要的文件类型。它有它的缺点,但有效......

For I = 1 To .Filters.Count
    fName = .Filters(I).Description
    If fName = "PDF" Then
        .FilterIndex = I
        Exit For
    End If
Next

回答by Rtronic

you can use : .Filters.Add "*.PDF", 1 or .FilterIndex = 25 Depends on the order number of PDF in the listindex you can know it from your Save as dialog ---> type: Sub Test3()

您可以使用: .Filters.Add "*.PDF", 1 或 .FilterIndex = 25 取决于列表索引中 PDF 的订单号,您可以从另存为对话框中知道它---> 类型:Sub Test3()

Set myFile = Application.FileDialog(msoFileDialogSaveAs)
With myFile
.Title = "Choose File"
.AllowMultiSelect = False
.ButtonName = "&Save As"
.initialFilename = "Export"
.Title = "Save PDF as"
.Filters.Add "*.PDF", 1
If .Show <> -1 Then
MsgBox "You have cancelled"
Exit Sub
End If
FileSelected = .SelectedItems(1)
End With


End Sub