以编程方式将PDF保存在特定的文件夹中

我试图让这个程序来保存我的PDF文件在Excel模板保存在我的模板代码文件夹相同的文件夹。 这是我迄今为止的代码。 我卡住的部分在“Dim Save Path”的底部。

任何build议,非常感谢!

Sub SaveToPDF() Dim fp As String Dim fp1 As String Dim i As Integer Dim Max As Integer Dim numprints As Integer Dim fnum As String Dim wb As Workbook i = 1 fnum = Sheets("Sheet2").Range("B3").Value Worksheets("Printable").Activate Set wb = ActiveWorkbook 'counting the number of pagebreaks to identify number of prints Max = ActiveSheet.HPageBreaks.Count numprints = Max / 2 k = 10 'to get the file name for each PDF and setting the folder to print For j = 1 To numprints fp1 = CStr(fnum & " - " & (Replace(Sheets("Printable").Range("f" & k).Value, "/", "-"))) fp = CStr("Q:\PATS\24-7\Partnership Accounting\2013\2013 TAX\2013 Clients\8392 - 8413 AAF Master Folder\PDF\" & fp1 & ".pdf") ' exports 2 pages at a time and creates a PDF, then loops wb.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fp, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False, From:=i, To:=i + 1 i = i + 2 k = k + 70 Next j MsgBox ("Print to PDF Complete, Check if you have " & numprints & " PDF Files") 'save the pdf in the 8392-8413 Master Folder under the Template Code Folder Dim SaveName As String SaveName = ActiveSheet.Range("G3").Text ActiveWorkbook.SaveAs Filename:=SaveName & ".xls" Dim SavePath As String SavePath = CStr("Q:\PATS\24-7\Partnership Accounting\2013\2013 TAX\2013 Clients\8392 - 8413 AAF Master Folder\Template Code" & fp1 & ".pdf") SavePath = wb.Path ChDir SavePath ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ SavePath & "Form 8392-8413 - " & ".pdf" _ , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _ :=False, OpenAfterPublish:=True End Sub 

那么先看看你有两个SavePathvariables的赋值。 首先, Cstr("Q...") ,并在下一行中重新设置wb.path 。 这可能是你的错误的来源。

我想你不需要这一行,看起来它是从debugging剩余的,因为这是一个完整的文件名+path

如果你想使用这一行:

 SavePath = CStr("Q:\PATS\24-7\Partnership Accounting\2013\2013 TAX\2013 Clients\8392 - 8413 AAF Master Folder\Template Code" & fp1 & ".pdf") 

那么你必须删除这一行

 SavePath = wb.Path 

或者,如果要在ExportAsFixedFormat方法中使用wb.Path进行连接,那么错误是: wb.Path不会在path分隔符中结束,因此当您尝试保存时会引发错误。

要解决它,请尝试:

 SavePath = wb.Path If Not Right(SavePath,1) = Application.PathSeparator Then SavePath = SavePath & Application.PathSeparator End If