我有這段代碼要求用戶選擇一個檔案,然后要求用戶選擇檔案路徑來保存宏將創建的 PDF。代碼的問題在于它要求用戶選擇將 PDF 保存到的檔案路徑的部分。
例如,假設用戶選擇此檔案路徑將所有 PDF 保存到“C:\Users\Tom.James\Desktop\Tom\Macros”中,PDF 全部保存在以下檔案路徑“C:\Users\Tom .James\Desktop\Tom”我不知道為什么它不將它們保存在檔案路徑的最后一部分,而是保存在一個位置之前。我認為問題出在該代碼區域中的“-1”部分,但我無法完全排除故障。
With Application.FileDialog(msoFileDialogFilePicker)
'Makes sure the user can select only one file
.AllowMultiSelect = False
'Filter to just the following types of files to narrow down selection options
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
'Show the dialog box
.Show
'Store in fullpath variable
fullpath = .SelectedItems.Item(1)
Workbooks.Open fullpath
End With
Dim dlgSaveFolder As FileDialog
Dim sFolderPathForSave As String
'''
'Open a Folder picker dialog box.
Set dlgSaveFolder = Application.FileDialog(msoFileDialogFolderPicker)
With dlgSaveFolder
.Title = "Select a Folder to save the PDF's to"
.AllowMultiSelect = False
.InitialFileName = ThisWorkbook.Path & "\"
If .Show <> -1 Then GoTo CancelFolderSelection
sFolderPathForSave = .SelectedItems(1)
End With
Set dlgSaveFolder = Nothing
Sheets("Balance Sheet").Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"02 - Balance Sheet", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Sheets("Reserve Statement").Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"03 - Statement of Reserves", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Sheets("Income Statement").Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"04 - Income Statement", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
uj5u.com熱心網友回復:
從檔案中WorkSheet.ExportAsFixedFormat:FileName - “...包含完整路徑,或 Excel 將檔案保存在當前檔案夾中。”。
要保存在目標目錄中,您只需要在 FileName 中包含該目錄路徑。您的代碼已經保存了這個值,sFolderPathForSave所以您只需要更改所有FileName引數以包含sFolderPathForSave
Filename:= sFolderPathForSave & "\02 - Balance Sheet"
Filename:= sFolderPathForSave & "\03 - Statement of Reserves"
Filename:= sFolderPathForSave & "\04 - Income Statement"
此外,在檔案的示例中,它們在 FileName 中包含檔案擴展名。因此,如果您遇到檔案型別應用不正確的問題,您可能需要添加“.pdf”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/314254.html
