我试图让这个程序将我的pdf文件保存在保存excel模板的同一文件夹中,这是我的模板代码文件夹。 这是我到目前为止的代码。我被卡住的部分位于" Dim Save Path的底部。"
非常感谢任何建议!
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
答案 0 :(得分:1)
首先,我看到你有两个SavePath
变量赋值。首先,Cstr("Q...")
,然后在下一行重置wb.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
或者,如果要在wb.Path
方法中使用ExportAsFixedFormat
进行连接,则错误是这样的:wb.Path
不会在路径分隔符中结束,因此会引发尝试保存时出错。
要解决此问题,请尝试:
SavePath = wb.Path
If Not Right(SavePath,1) = Application.PathSeparator Then
SavePath = SavePath & Application.PathSeparator
End If