【问题标题】:Print word file using pdf printer in excel project在excel项目中使用pdf打印机打印word文件
【发布时间】:2018-05-07 12:02:02
【问题描述】:

我正在使用 excel vba 项目创建一个 word 文件,然后使用以下代码将其保存为 pdf。

wordapp.ActiveDocument.SaveAs2 "" & folder & "" & pdfname & ".pdf", 17

我想使用 PDF 打印机打印相同的 word 文件,路径为

& folder &

和文件名

& pdf 名称 &

我曾尝试将其用作 Word 文件中的宏,但它要求提供路径和文件名。不自动化。

【问题讨论】:

  • folderpdfname 在运行时的确切值是多少?我猜你的路径是错误的或不存在!
  • 转储您的文件夹和 pdfname 并确保它们的格式正确并带有反斜杠
  • 它们的格式正确。没问题。我想将其打印为 PDF,因为通常的“另存为 pdf”会导致更大的 pdf 大小。但是打印到 pdf 结果几乎是一半大小。
  • 我试过这段代码。 wordapp.ActiveDocument.PrintOut PrintToFile:=True, _ OutputFilename:=sItem & pdfname & "_temp" & ".pdf" 它将word文档转换为pdf。但我无法打开它说它不是 pdf 或损坏的 PDF 文档。仅当我将 hp Laserjet 打印机设为默认打印机时才会发生这种情况。当我将默认 printre 切换到 foxit 打印机时,它会要求我提供 pdf 文件的路径。

标签: vba excel pdf ms-word


【解决方案1】:

可能最简单的方法是使用pdfName 作为字符串并在它和文件夹之前放置\ 符号。

尝试从 Excel 文件运行此代码,只要确保 Excel 文件保存成功,否则ThisWorkbook.Path 将等于空字符串,并且在运行时会要求您保存文件 -> If ThisWorkbook.Path = vbNullString Then ThisWorkbook.Save

Public Sub TetMe()

    Dim wordApp As Object
    Dim WordDoc As Object

    Dim folder  As String
    Dim pdfName As String: pdfName = "someName"

    If ThisWorkbook.Path = vbNullString Then ThisWorkbook.Save        
    Set wordApp = CreateObject("Word.Application")
    Set WordDoc = wordApp.documents.Add

    folder = ThisWorkbook.Path & "\"
    wordApp.ActiveDocument.SaveAs2 folder & pdfName & ".pdf", 17

End Sub

如果您想知道.SaveAs2 后面的17 是什么,那是wdSaveFormat Enumeration for wdFormatPDF

WdSaveFormat Enumeration MSDN

【讨论】:

  • 您好@Vityata 我已经开发了您在评论中给出的内容。我已经安装了 foxit pdf,我想将它用作 PDF 打印机并将 word 文件转换为 PDF。我已经尝试过宏,但它没有按我的意愿工作。以下是宏代码。
  • ActivePrinter = "Foxit Reader PDF Printer" wordapp.ActiveDocument.PrintOut Filename:="" & sItem & "" & pdfname & "_temp", Range:=wdPrintAllDocument, Item:= _ wdPrintDocumentWithMarkup, Copies :=1, Pages:="", PageType:= _ wdPrintAllPages, Collat​​e:=True, Background:=True, PrintToFile:=False, _ PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _PrintZoomPaperHeight:= 0 此代码无法帮助我实现我想要的。
  • @ShekharNalawade - 您是否尝试过答案中的代码?如果您有“foxit pdf”,这会大大改变问题,最好问一个新问题。
  • 我已经提到了同样的问题。请您仔细阅读。
【解决方案2】:

以下代码可用于让 Foxit PDF Printer 为您的代码运行。

主要代码

ActivePrinter = "Foxit Reader PDF Printer"
printoutcommand = "wordapp.ActiveDocument.PrintOut
Range:=wdPrintAllDocument, PrintToFile:=True,OutputFilename:=" & sItem &
pdfname & "_temp" & ".pdf"
wordapp.Application.Run "FoxitPrint2PDF"
Application.Wait (Now + TimeValue("0:00:02"))
Name "C:\Users\shena\Documents\Document1.pdf" As pdfname & "_temp" & ".pdf"
Application.Wait (Now + TimeValue("0:00:02"))
FileCopy "C:\Users\shena\Documents\" & pdfname & "_temp" & ".pdf", sItem & "" & pdfname & "_temp" & ".pdf"
Application.Wait (Now + TimeValue("0:00:02"))
Kill "C:\Users\shena\Documents\" & pdfname & "_temp" & ".pdf"
Application.Wait (Now + TimeValue("0:00:02"))
wordapp.activedocument.Close SaveChanges:=wdDoNotSaveChanges
wordapp.Quit
Set wordapp = Nothing

FoxitPrint2PDF 是一个宏,用于将 Foxit PDF 打印机设置为默认打印机,然后将其重置。代码如下

Dim sCurrentPrinter As String
On Cancel GoTo Cancelled:
sCurrentPrinter = ActivePrinter
ActivePrinter = "Foxit Reader PDF Printer"
Application.PrintOut FileName:=""
Cancelled:
ActivePrinter = sCurrentPrinter

这只是一个想法,给定相同的代码可以写入进行打印操作的模块中。 在主代码中可以看到一些剪切粘贴操作。这是相同的解释。当我们选择 PDF 作为打印机时,我们选择了转换后的 PDF 的默认位置。从那个位置我们把它带到我们想要的位置。您可以在“Foxit Reader PDF Printer”上右击看到这些选项,然后点击Printing Preferences。下面给出了相同的屏幕截图以供参考。

突出显示的选项对我们很重要。

【讨论】:

    猜你喜欢
    • 2016-10-09
    • 2013-02-19
    • 1970-01-01
    • 2019-12-06
    • 2021-11-02
    • 2015-10-19
    • 2017-11-26
    • 2013-04-08
    • 2012-03-31
    相关资源
    最近更新 更多