简体   繁体   English

PDF打印到文件路径VBA

[英]PDF print to file path VBA

I have the below code I have researched and got to work in one of my workbooks. 我有下面的代码,我已经研究过并开始在其中一本工作簿中工作。 The idea of the code is to automatically print a sheet to PDF and save down in a directory of my choosing naming it the contents of a specific cell. 代码的想法是自动将工作表打印为PDF,然后保存到我选择的目录中,将其命名为特定单元格的内容。

All works fine until I try to copy into a new workbook (I designed in a test workbook as not to corrupt the original) then I get a 一切正常,直到我尝试将其复制到新工作簿中(在测试工作簿中设计为不破坏原始工作簿),然后我得到了

run time error 1004 运行时错误1004

and the ThisWorkbook.Sheets(Array("sheet 2")).Select is highlighted when I debug. ThisWorkbook.Sheets(Array("sheet 2")).Select在我调试时突出显示。 Am I missing something simple? 我是否缺少简单的东西? as the code works in the original workbook but not in if I paste into a new module in a new workbook? 因为代码在原始工作簿中有效,但是如果我将其粘贴到新工作簿中的新模块中则无效? Sorry i'm quite new to this! 抱歉,我对此很陌生!

sub PrintPDFRT()

Sheets("test").Activate
ActiveSheet.UsedRange.Select
Sheets("malbru1").Activate
ActiveSheet.UsedRange.Select
Sheets("sheet 2").Activate
Range("A1:j137").Select

ThisWorkbook.Sheets(Array("sheet 2")).Select
Selection.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:="FIle path\" & Range("L7").Value, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
 End Sub

Try it like this: (without Array ) 像这样尝试:(不使用Array

sub PrintPDFRT()

WorkSheets("test").Activate
ActiveSheet.UsedRange.Select
WorkSheets("malbru1").Activate
ActiveSheet.UsedRange.Select
WorkSheets("sheet 2").Activate
Range("A1:j137").Select

ThisWorkbook.WorkSheets("sheet 2").ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:="FIle path\" & Range("L7").Value, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
 End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM