【发布时间】:2017-12-02 17:05:31
【问题描述】:
我的代码运行良好,并从工作表创建了 PDF。但我希望这种情况发生,而不必按下文件路径的保存单选按钮。 还是我需要创建一个确切的文件名并没有选择地保存
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "ddmmyyyy\_hhmm")
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")
strFile = strName & "_" & strTime & ".pdf"
strPathFile = strPath & strFile
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
'export to PDF if a folder was selected
If myFile <> "False" Then
wsA.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False _
'confirmation message with file info
MsgBox "PDF file has been created: " _
& vbCrLf _
& myFile
End If
exitHandler:
CommandButton2.Visible = True
CommandButton1.Visible = False
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
【问题讨论】:
-
如果您不想要保存对话框,您需要在
ExportAsFixedFormat中指定Filename:=myFileWithFullPath的完整路径。所以它直接保存文件而不选择。如果您需要使用对话框选择路径,则需要自己按保存按钮。
标签: excel pdf-generation vba