![更改宏打印为 pdf 时的默认保存目录](https://linux22.com/image/1614881/%E6%9B%B4%E6%94%B9%E5%AE%8F%E6%89%93%E5%8D%B0%E4%B8%BA%20pdf%20%E6%97%B6%E7%9A%84%E9%BB%98%E8%AE%A4%E4%BF%9D%E5%AD%98%E7%9B%AE%E5%BD%95.png)
给定一个预先选择的打印区域,Ryan Wells 编写的以下宏将打印为 PDF,然后指示用户将 PDF 文件命名并保存到XLSTART 目录在本地机器上。
请帮我找到代码中可以插入路径语句的行,以便当“另存为”对话框打开时,所需的路径(位于 OneDrive 中)就存在。
谢谢!
Sub PrintSelectionToPDF()
'SUBROUTINE: PrintSelectionToPDF
'DEVELOPER: Ryan Wells
'DESCRIPTION: Print your currently selected range to a PDF
Dim ThisRng As Range
Dim strfile As String
Dim myfile As Variant
If Selection.Count = 1 Then
Set ThisRng = Application.InputBox("Select a range", "Get Range", Type:=8)
Else
Set ThisRng = Selection
End If
'Prompt for save location
strfile = "Selection" & "_" _
& Format(Now(), "yyyymmdd_hhmmss") _
& ".pdf"
strfile = ThisWorkbook.Path & "\" & strfile
myfile = Application.GetSaveAsFilename _
(InitialFileName:=strfile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and File Name to Save as PDF")
If myfile <> "False" Then 'save as PDF
ThisRng.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
myfile, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
Else
MsgBox "No File Selected. PDF will not be saved", vbOKOnly, "No File Selected"
End If
End Sub
'Once you select the range and click OK, it will show a dialog box where you can select where you want to save the PDF.
'It will automatically pick the date and time from your system's clock and add it to the file name.
答案1
该文件夹作为InitialFileName
参数的一部分,如下所示:
strfile = "C:\mypath\Selection_" & Format(Now(), "yyyymmdd_hhmmss") & ".pdf"
请注意,这实际上会覆盖并取消以下应删除的行:
strfile = ThisWorkbook.Path & "\" & strfile