Excel:要打印到 PDF 的 VBA - 运行时错误 1004
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17379700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel: VBA to print to PDF - Run time error 1004
提问by Matthias
I have tried using the following solution to print from Excel to PDF:
我尝试使用以下解决方案从 Excel 打印到 PDF:
Excel 2013 Print to PDF in VBA
While the solution seems to have worked for other people, it produces a run time error 1004 in my case. Does anyone have any further ideas as to why this may be? I have searched aroudn and still cannot figure out what the reason might be? Could it be that my version of Excel is different (I use 2007)?
虽然该解决方案似乎对其他人有效,但在我的情况下它会产生运行时错误 1004。有没有人对为什么会这样有任何进一步的想法?我已经搜索过,但仍然无法弄清楚可能是什么原因?会不会是我的 Excel 版本不同(我用的是 2007)?
The only mod I made to the original solution was to change the file path so it would save to my desktop. My code is the following:
我对原始解决方案所做的唯一修改是更改文件路径,以便将其保存到我的桌面。我的代码如下:
Sub Invoice_to_PFD()
'Saves the invoice print area to a PDF file
Dim fp As String
Dim wb As Workbook
fp = "C:\desktop\NewInvoice.pdf"
Set wb = ActiveWorkbook
wb.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fp, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
采纳答案by Matthias
So this is the working code now for Windows users (Mac OS might have to adjust file path):
所以这是 Windows 用户现在的工作代码(Mac OS 可能需要调整文件路径):
Sub Invoice_to_PDF()
'Saves the invoice print area to a PDF file
Dim fp As String
Dim wb As Workbook
Dim ws As Worksheet
fp = "C:\Users\[username]\Desktop\NewInvoice.pdf"
Set wb = ActiveWorkbook
Set ws = Worksheets("Invoice")
ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fp, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
回答by SVCooper
If the worksheet that you are exporting contains no data then the export fails. I found this solution on the MrExcel forum, where someone appears to have stumbled over the answer!
如果您要导出的工作表不包含数据,则导出失败。我在 MrExcel 论坛上找到了这个解决方案,那里似乎有人偶然发现了答案!
回答by Gareth Hayter
Another cause of this error is if the filename contains illegal characters such as:
此错误的另一个原因是文件名是否包含非法字符,例如:
- < (less than)
- > (greater than)
- : (colon)
- " (double quote)
- / (forward slash)
- \ (backslash)
- | (vertical bar or pipe)
- ? (question mark)
- * (asterisk)
- <(小于)
- >(大于)
- : (冒号)
- "(双引号)
- /(正斜杠)
- \(反斜杠)
- | (垂直条或管)
- ? (问号)
- *(星号)
See 'Naming conventions' here: https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
请参阅此处的“命名约定”:https: //docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
Solution:replace illegal characters with legal ones, such as '_'.
解决方法:将非法字符替换为合法字符,例如'_'。
回答by BillR
OK, I just played with mine some more, and if I take the ".pdf" off the filename specification, I don't get the error 1004.
好的,我只是玩了一些,如果我从文件名规范中删除“.pdf”,我就不会收到错误 1004。