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

Vba 以编程方式将PDF保存在特定文件夹中,vba,excel,pdf,Vba,Excel,Pdf,我试图让这个程序将我的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 Workb

我试图让这个程序将我的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

首先,我看到有两个赋值给
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

您有两个分配给
SavePath
,您想使用哪一个?现在我明白为什么这是失败的。如果删除
SavePath=wb.Path
,我认为它应该可以工作。如果没有,请告诉我。嗨,大卫。非常感谢。我尝试过,现在将[SavePath=wb.Path]退出。PDF文件仍保存在错误的文件夹中。有什么想法吗?错误保存它们的文件夹的位置是什么?我进入了文件编辑首选项安全(增强)取消检查启动时的启用保护模式。如果我对pdf进行更改,我只需点击“保存”按钮,它将覆盖更改并正常保存。我不确定取消勾选是否会影响文档的安全性。。但到目前为止,它工作得很好,但我只是在几天前做了这个改变。