PDA

View Full Version : Solved: How to auto print .PDF files



pitspawn8
03-19-2009, 06:25 AM
I'm extremely new to VBA so go easy on me. I constantly receive emails with .pdf files that I have to print. I'm trying to get vba to do it for me without me having to go into each individual email.

This is what I have so far. It saves the attachments to the specified folder and opens adobe, but it doesn't open the attachment itself and print it.

Sub GetAttachments()


Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim i As Integer

Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
i = 0

If Inbox.Items.Count = 0 Then
MsgBox "There are no messages in the Inbox.", vbInformation, _
"Nothing Found"
Exit Sub
End If

For Each Item In Inbox.Items
For Each Atmt In Item.Attachments
FileName = "C:\Email Attachments\" & Atmt.FileName
Atmt.SaveAsFile FileName
Shell """c:\program files\adobe\reader 8.0\reader\acrord32.exe"""
i = 1 + 1



Next Atmt


Next Item

If i > 0 Then
MsgBox "I found " & i & " attached files." _
& vbCrLf & "I have saved them into the C:\Email Attachments folder." _
& vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
Else
MsgBox "I didn't find any attached files in your mail.", vbInformation, _
"Finished!"
End If

GetAttachments_exit:
Set Atmt = Nothing
Set Item = Nothing
Set ns = Nothing
Exit Sub


Exit Sub



End Sub
Any ideas?

Charlize
03-20-2009, 08:10 AM
Try this command for using acrobat reader :

AcroRd32.exe /t path printername drivername portname
- Initiates Acrobat Reader, prints a file while suppressing the Acrobat print dialog box, then terminates Reader.
The four parameters of the /t option evaluate to path, printername, drivername, and portname (all strings).
printername - The name of your printer.
drivername - Your printer driver's name. Whatever appears in the Driver Used box when you view your printer's properties.
portname - The printer's port. portname cannot contain any "/" characters; if it does, output is routed to the default port for that printer.
If using Acrobat, substitute Acrobat.exe in place of AcroRd32.exe in the command lines.

Charlize

JP2112
03-24-2009, 10:30 AM
After saving the PDF, run this code:

Public Sub print_pdf(xSomeFile as string)
Shell "C:\Program Files\Adobe\Acrobat 5.0\Reader\AcroRd32.exe /p /h " & xSomeFile, vbHide
End Sub

(From http://www.daniweb.com/forums/thread17156-2.html)

Update the filepath to reflect your local copy of AA.

HTH,
JP


I'm extremely new to VBA so go easy on me. I constantly receive emails with .pdf files that I have to print. I'm trying to get vba to do it for me without me having to go into each individual email.

This is what I have so far. It saves the attachments to the specified folder and opens adobe, but it doesn't open the attachment itself and print it.

pitspawn8
03-24-2009, 12:54 PM
Try this command for using acrobat reader :

AcroRd32.exe /t path printername drivername portname - Initiates Acrobat Reader, prints a file while suppressing the Acrobat print dialog box, then terminates Reader.
The four parameters of the /t option evaluate to path, printername, drivername, and portname (all strings).
printername - The name of your printer.
drivername - Your printer driver's name. Whatever appears in the Driver Used box when you view your printer's properties.
portname - The printer's port. portname cannot contain any "/" characters; if it does, output is routed to the default port for that printer.
If using Acrobat, substitute Acrobat.exe in place of AcroRd32.exe in the command lines.

Charlize

How would I set this as the default printer for my PC. I'm on an extremely secure network and some of this info is eluding me.

Charlize
03-25-2009, 02:04 AM
Just use shell "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe /p /h " & pathlike JP2112 said.


For Each Atmt In Item.Attachments
FileName = "C:\Email Attachments\" & Atmt.FileName
Atmt.SaveAsFile FileName
'If using version 7 = Acrobat 7.0
shell "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe /p /h " & FileName
i = 1 + 1
Next Atmt

You also have to change the way how the variable i gets the first value. You give it 0 but what if there are files with the same name the next time you save a file ? You always start with 0. Maybe use Dir statement to loop through directory and count no of files to determine the next value for i ?

Charlize

pitspawn8
03-25-2009, 05:54 AM
Okay, I added that stuff and now adobe gives me an error saying that the file can't be found. This is what I have now.

For Each Item In Inbox.Items
For Each Atmt In Item.Attachments
FileName = "C:\Email Attachments\" & Atmt.FileName
Atmt.SaveAsFile FileName
Shell "c:\program files\adobe\reader 8.0\reader\acrord32.exe /p /h " & Atmt.FileName
i = 1 + 1
Next Atmt


I've also tried it like this but with the same result.

For Each Item In Inbox.Items
For Each Atmt In Item.Attachments
FileName = "C:\Email Attachments\" & Atmt.FileName
Atmt.SaveAsFile FileName
Shell "c:\program files\adobe\reader 8.0\reader\acrord32.exe /p /h " & FileName

i = 1 +1
Next Atmt


Charlize, The attachments are deleted at the end of the day so I'm not really too worried about the message box.

Charlize
03-25-2009, 06:37 AM
Change Shell "c:\program files\adobe\reader 8.0\reader\acrord32.exe /p /h " & FileName in
Shell "c:\program files\adobe\acrobat 8.0\reader\acrord32.exe /p /h " & FileName Charlize

pitspawn8
03-25-2009, 07:15 AM
That file path doesn't have anything but *.bak files. I tried copying the files from the other path but it still gives me the same error.

JP2112
03-25-2009, 08:34 AM
You have to locate the folder containing the Acrobat reader executable.

Otherwise, check the following for a list of command line switches for use with AA. You might need to reorder the switches/filepath.

http://partners.adobe.com/public/developer/en/acrobat/sdk/pdf/intro_to_sdk/DeveloperFAQ.pdf

--JP


That file path doesn't have anything but *.bak files. I tried copying the files from the other path but it still gives me the same error.

pitspawn8
03-25-2009, 09:49 AM
I'm pretty sure that the print commands are correct now. It just seems like adobe isn't opening the atmt to print it. Adobe just says the file can't be found.

pitspawn8
03-25-2009, 09:50 AM
Also, I appreciate both of your help. Like I said, I'm extremely new to this and am still learning a great bit. I appreciate your patience.

JP2112
03-26-2009, 04:40 AM
Can you post your updated code?

--JP


I'm pretty sure that the print commands are correct now. It just seems like adobe isn't opening the atmt to print it. Adobe just says the file can't be found.

pitspawn8
03-26-2009, 04:42 AM
Sub GetAttachments()


Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim i As Integer

Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
i = 0

If Inbox.Items.Count = 0 Then
MsgBox "There are no messages in the Inbox.", vbInformation, _
"Nothing Found"
Exit Sub
End If

For Each Item In Inbox.Items
For Each Atmt In Item.Attachments
FileName = "C:\Email Attachments\" & Atmt.FileName
Atmt.SaveAsFile FileName
Shell "c:\program files\adobe\acrobat 8.0\reader\acrord32.exe /p /h " & Atmt.FileName

i = 1 + 1




Next Atmt


Next Item

If i > 0 Then
MsgBox "I found " & i & " attached files." _
& vbCrLf & "I have saved them into the C:\Email Attachments folder." _
& vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
Else
MsgBox "I didn't find any attached files in your mail.", vbInformation, _
"Finished!"
End If

GetAttachments_exit:
Set Atmt = Nothing
Set Item = Nothing
Set ns = Nothing
Exit Sub


Exit Sub



End Sub

JP2112
03-26-2009, 08:09 AM
It should be

Shell "c:\Program Files\Adobe\Acrobat 8.0\Reader\AcroRd32.exe /p /h " & FileName

because the variable FileName includes the filename and path. Atmt.FileName won't be found since it's a temporary file attached to your email.

--JP



Sub GetAttachments()


Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim i As Integer

Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
i = 0

If Inbox.Items.Count = 0 Then
MsgBox "There are no messages in the Inbox.", vbInformation, _
"Nothing Found"
Exit Sub
End If

For Each Item In Inbox.Items
For Each Atmt In Item.Attachments
FileName = "C:\Email Attachments\" & Atmt.FileName
Atmt.SaveAsFile FileName
Shell "c:\program files\adobe\acrobat 8.0\reader\acrord32.exe /p /h " & Atmt.FileName

pitspawn8
03-26-2009, 08:51 AM
I've tried that but still the same error with adobe.

Charlize
03-30-2009, 02:49 AM
I've tried that but still the same error with adobe.You are sure you only save pdf's with this routine.

Charlize

pitspawn8
03-31-2009, 04:39 AM
Yes.

I've solved the problem for the most part. The last thing that I want to do with it is set it to pull files from an archive or public folder. I've tried a couple of different thing but I can't figure it out. Any thoughts? Here is the corrected code.

'This macro was written by Cpl Malcolm K. Quick with the help of Sgt Jeremiah G. Mort
'This code is used to automatically print all *.pdf files from the specified folder.

Option Explicit

Sub Printattachments()

'The next four variable declarations are specific to Microsoft Outlook.
Dim ns As NameSpace
Dim smbbox As MAPIFolder
Dim item As Object
Dim atmt As Attachment

'These are standard counters and variables.
Dim filename, thedate As String
Dim i As Integer

'These lines are responsible for the folder that the e-mails will be pulled from.
Set ns = GetNamespace("MAPI")
Set smbbox = ns.GetDefaultFolder(olFolderDrafts)

'This If statement checks to see if there are any e-mails in the folder.
If smbbox.Items.Count = 0 Then
MsgBox "There are no messages in the Inbox.", vbInformation, "Nothing Found!"
Exit Sub
End If

'This block is responsible for looking for attachments in an Outlook folder and dealing with them.
i = 0
'This looks for e-mails in the specified folder
For Each item In smbbox.Items
'This If Statement looks for only *.pdf files.
For Each atmt In item.Attachments
If UCase(Right(atmt.filename, 3)) = "PDF" Then
'This next line creates a string with a specific Date/Time format.
thedate = Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now), 2) & " " & _
Right("0" & Hour(Now), 2) & "." & Right("0" & Minute(Now), 2)

' This ine creates specific file names for each attachment with the Date/time and _
'counter so the chance of file name duplication is reduced.
filename = "c:\E-Mail\" & thedate & " - " & i & " - " & atmt.filename

'This line saves the attachments.
atmt.SaveAsFile filename

'This line runs Acrobat Reader in a command prompt format. It opens and prints a _
'a specified .pdf file.
Shell """C:\Program Files\Adobe\reader 8.0\Reader\AcroRd32.exe"" /t """ _
& filename & """"

End If

i = i + 1

Next atmt
Next item

'This block checks to see if any attachments were found and informs the user.
If i > 0 Then
MsgBox "Attachments have been found and dealt with."
Else
MsgBox "I didn't find any attached files in your mail.", vbInformation, _
"Finished!"
End If

'This sets the microsoft Outlook specific variables to nothin.
Set atmt = Nothing
Set item = Nothing
Set ns = Nothing



End Sub

Charlize
03-31-2009, 05:26 AM
You are processing the drafts folder ? Try this because I don't know which folder you want to handle.Set smbbox = Application.GetNamespace("MAPI").PickFolder
Charlize

pitspawn8
03-31-2009, 06:44 AM
That worked wonderfully. Thanks for all your help. Topic solved.

Agni1978
04-21-2009, 03:52 AM
Incase Adobe is already installed on your machine simply use ShellExecute API.

Put this on top of all declarations

Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, _
ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

and simply use as

ShellExecute 0&, "open", fileFullPath, 0, "Directory", 0

Charlize
04-27-2009, 01:19 PM
Incase Adobe is already installed on your machine simply use ShellExecute API.

Put this on top of all declarations

Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, _
ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

and simply use as

ShellExecute 0&, "open", fileFullPath, 0, "Directory", 0The question of the original poster was ... PRINT pdf files. So you need to change the "open" command to a "Print" command.

More details regarding this subject can be found here : http://www.xcelfiles.com/ShellExecuteA.html

Charlize

mdmackillop
05-25-2009, 04:44 AM
Can someone put together a KB Item for this?

cricure
09-05-2010, 12:06 PM
You can find something about this in the Adobe Developer FAQ. (It's a PDF document rather than a web page, which I guess is unsurprising in this particular case.)

The FAQ notes that the use of the command line switches is unsupported.

To open a file it's:

AcroRd32.exe <filename>

The following switches are available:

* /n - Launch a new instance of Reader ever if one is already open
* /s - Don't show the splash screen
* /o - Don't show the open file dialog
* /h - Open as a minimized window
* /p <filename> - Open and go straight to the print dialog
* /t <filename> <printername> <drivername> <portname> - Print the file the specified printer.

For command line pdf printing is bether use CLPrint

savosamson
05-05-2015, 09:54 PM
Charlize[/QUOTE]

Clicking Print command for each file is really a daunting task when there are hundred of files to be printed. The better alternate is to use FolderMill for automatic Printing PDF Files and Folders. I got it from foldermill.com It can print the entire Folders without any hassle. It really saves a lot of time while i can do other tasks.

rocky678
08-26-2015, 08:44 AM
Hello,

I suggest PrintConductor for Bulk PDF Printing if u have to print hundreds of PDFs. It lets you select the entire folder at once and print it all. PrintConductor is a good printing management software. Search PrintConductor more info