语法问题,出现运行TIme错误

时间:2019-07-25 13:55:41

标签: excel vba file pdf save

我正在尝试将文件另存为PDF,但得到

  

运行时错误'1004'

我可以写excel文件没有问题,当我更改代码以写PDF文件时,出现此错误。

我试图以几种方式编写语法,但没有用。我在想我可能缺少参数了吗?我看了多个示例,但看不到我做错了什么。


CODE :::: >>>>

fileName = "O:\Paula\Z test write PDF file exhibit A\" & Cells(initial + 1, 1).Text & ".pdf"

Set DestBook = Workbooks.Add

With DestBook
    .Title = "Installment Trans History"
    .Subject = "legal Request"
    .SaveAs fileName:=fileName, FileFormat:=xlTypePDF 
    Set DestSheet = .Worksheets("sheet1")
End With

Set DestSheet = DestBook.Worksheets("sheet1")

如果我将文件更改为.xlms并删除FileFormat:=xlTypePDF,则会按预期创建文件。

当我将文件更改为.pdf并添加时,FileFormat:=xlTypePDF出现运行时错误。我尝试过四处移动,但是却遇到了同样的错误。

1 个答案:

答案 0 :(得分:0)

您要使用workbook.exportasfixedformat

不起作用的原因是您正在尝试将pdf保存到工作簿中。无需添加新工作簿,它正在失败,因为这是您现在要处理的对象。

fileName = "O:\Paula\Z test write PDF file exhibit A\" & Cells(initial + 1, 1).Text & ".pdf"

With activeworkbook 'You should make this an explicit reference to your current workbook.
    .exportasfixedformat xltypepdf, filename
end with