• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Automating printing existing PDF documents using VBA

Explorer ,
Feb 26, 2021 Feb 26, 2021

Copy link to clipboard

Copied

Hi,

 

I have a mixture of Word/PDF documents which I would like to print in the right sequence using MS Word VBA. Everything works fine. My code for printing PDF documents using DDE is as follows:

Dim RetVal
Dim chanNo
'Open Adobe Acrobat
RetVal = Shell("C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\Acrobat.exe", vbHide)

chanNo = DDEInitiate("AcroViewA21", "Control")

'File is opened and displayed with DocOpen
DDEExecute chanNo, "[DocOpen(""" & myDoc & """)]"

'Print the file
DDEExecute chanNo, "[FilePrintTo(""" & myDoc & """,""" & myPrinter & ""","""")]"

'Close PDF file
DDEExecute chanNo, "[DocClose(""" & myDoc & """)]"

'Close Acrobat
DDEExecute chanNo, "[AppExit( )]"

 

Where myDoc and myPrinter need to be defined.

 

I have read that DDE is now old technology and is only available for backwards compatibility and that OLE should be used instead. I am not a programmer, so I'm not sure how important this is.

 

Can someone please provide an OLE solution to do the same task, just to ensure this is futureproof. In particular, it is important to be able set the printer.

 

Any help would be greatly appreciated. Thanks!

Views

2.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Feb 27, 2021 Feb 27, 2021

I've managed to answer my own problem. Here's the code I've cobbled together by referring to various sources:

 

Dim AcroApp As AcroApp
Dim AcroAVDoc As AcroAVDoc
Dim AcroPDDoc As AcroPDDoc

Dim myDoc As String
Dim myPrinter As String

Dim RetVal


CreateObject("WScript.Network").SetDefaultPrinter myPrinter

Set AcroApp = CreateObject("AcroExch.App")
Set AcroAVDoc = CreateObject("AcroExch.AVDoc")
If AcroAVDoc.Open(myDoc, "") Then

Set AcroPDDoc = AcroAVDoc.GetPDDoc
NumOfPages = AcroPDDoc.GetNumPages - 1
Set AcroPDDo

...

Votes

Translate

Translate
Community Expert ,
Feb 26, 2021 Feb 26, 2021

Copy link to clipboard

Copied

Try the forum for Acrobat SDK.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 26, 2021 Feb 26, 2021

Copy link to clipboard

Copied

Thank you. Is there an easy way to do that - to change my post to another forum?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 26, 2021 Feb 26, 2021

Copy link to clipboard

Copied

Hi,

 

Moved for you.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 26, 2021 Feb 26, 2021

Copy link to clipboard

Copied

Thank you. I hope someone can help!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 27, 2021 Feb 27, 2021

Copy link to clipboard

Copied

I've managed to answer my own problem. Here's the code I've cobbled together by referring to various sources:

 

Dim AcroApp As AcroApp
Dim AcroAVDoc As AcroAVDoc
Dim AcroPDDoc As AcroPDDoc

Dim myDoc As String
Dim myPrinter As String

Dim RetVal


CreateObject("WScript.Network").SetDefaultPrinter myPrinter

Set AcroApp = CreateObject("AcroExch.App")
Set AcroAVDoc = CreateObject("AcroExch.AVDoc")
If AcroAVDoc.Open(myDoc, "") Then

Set AcroPDDoc = AcroAVDoc.GetPDDoc
NumOfPages = AcroPDDoc.GetNumPages - 1
Set AcroPDDoc = CreateObject("AcroExch.PDDoc")
RetVal = AcroAVDoc.PrintPagesSilent(0, NumOfPages, 2, False, False)
AcroApp.CloseAllDocs
End If

AcroApp.Exit
Set AcroPDDoc = Nothing
Set AcroAVDoc = Nothing
Set AcroApp = Nothing

 

Where myDoc and myPrinter need to be defined.

 

This is all done in the background. If you need to see things happening, you can also add in:

 

AcroApp.Show

AcroAVDoc.BringToFront

 

The above is for a Windows 10 system and sets the main default printer of the Windows system. Can anyone please tell me how to set the printer within Acrobat. What would the VBA code be for that? Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 24, 2024 Jan 24, 2024

Copy link to clipboard

Copied

LATEST

Hi, were you able to indicate the number of copies to print?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines