PDA

View Full Version : Automating Outlook to save & print pdf attachments



javavibe
04-12-2007, 05:09 PM
Let me start off by stating that I know very little about VBA programing. I have done a few simple marco's and that's about it. I have been looking at Killian's KB article 522 on how to save and print attachments. It is exactly what I want to do, except with a pdf attachment. Outlook watches the directory, when an attachment arrives it is saved and printed automatically. The problem is I don't know how to call Acrobat to print the attachment. I am using Outlook 2003, Acrobat 7.0 Professional, on an XP Pro box. Specifically in the Print Routine section, how do I call Acrobat to silently open and print the saved file without any user intervention. Help? :help and thank you.

Charlize
04-12-2007, 11:52 PM
Take a look at this thread :

http://vbaexpress.com/forum/showpost.php?p=94609&postcount=4

This will print pdf and doc for unread items that comes in and saves them to a certain folder.

Charlize

javavibe
04-17-2007, 02:06 PM
I think I have a viable solution here to save and print a pdf attachment. I have cobbled together the watch/save/print portions of Killian's code in KB 522 with some Acrobat command line code I found from Masaru Kaji (I can't post a link yet, but it's from Colo's Excel Junk Room webpage) . It does work in Outlook 2003, with Acrobat 7. Outlook will watch the designated directory, save the pdf attachement with a unique name, open Acrobat, print the file and close Acrobat. All of this is done silently. I thought I would post the code and get some feedback. Like I said this not something I do on a regular basis. And thanks to Killian and Masaru Kaji for posting their code.

VBA:
'########################################################################## #####
'### Module level Declarations
'expose the items in the target folder to events
Option Explicit
Dim WithEvents TargetFolderItems As Items
'set the string constant for the path to save attachments
Const FILE_PATH As String = "D:\Temp\"

'########################################################################## #####
'### this is the Application_Startup event code in the ThisOutlookSession module
Private Sub Application_Startup()
'some startup code to set our "event-sensitive" items collection
Dim ns As Outlook.NameSpace

Set ns = Application.GetNamespace("MAPI")
Set TargetFolderItems = ns.Folders.Item( _
"Personal Folders").Folders.Item("Cansew").Items

End Sub

'########################################################################## #####
'### this is the ItemAdd event code
Sub TargetFolderItems_ItemAdd(ByVal Item As Object)
'when a new item is added to our "watched folder" we can process it
Dim olAtt As Attachment
Dim i As Integer

If Item.Attachments.Count > 0 Then
For i = 1 To Item.Attachments.Count
Set olAtt = Item.Attachments(i)
'save the attachment with prepended Timer for unique filenames
olAtt.SaveAsFile FILE_PATH & CLng(Timer()) & olAtt.FileName

'if its an Acrobat file, pass the filepath to the print routine
If UCase(Right(olAtt.FileName, 3)) = "PDF" Then
PrintAtt (FILE_PATH & olAtt.FileName)
End If
Next
End If

Set olAtt = Nothing

End Sub

'########################################################################## #####
'### this is the Application_Quit event code in the ThisOutlookSession module
Private Sub Application_Quit()

Dim ns As Outlook.NameSpace
Set TargetFolderItems = Nothing
Set ns = Nothing

End Sub

'########################################################################## #####
'### print routine
Sub PrintAtt(fFullPath As String)
PrintPDF2 (fFullPath), 1

End Sub

Sub PrintPDF2(ByVal FileName As String, Optional Copies As Long = 1)
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'++ Prints the PDF files using a command line.
'++ Written by Masaru Kaji aka Colo
'++ Syntax
'++ FileName : Required String expression that specifies a file name
'++ - may include directory or folder, and drive..
'++ Copies : Optional Long. The number of copies to print.
'++ If omitted one copy is printed.
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dim cnt As Long
Dim myShell
Set myShell = CreateObject("WScript.Shell")
Dim Acro As AcroApp
Set Acro = New AcroApp

For cnt = 1 To Copies
'opens Acrobat and prints file
myShell.Run ("""C:\Program Files\Adobe\Acrobat 7.0\Acrobat\Acrobat.exe""" & "/t " & FileName)
Next
'tidy up
Acro.Exit
End Sub

javavibe
04-24-2007, 10:11 AM
I've realized there was a mistake in the code I posted. The ItemAdd event portion doesn't pass the file name correctly and it give a missing file name message when Acrobat tries to print the file. The corrected code for that module is below. Sorry if anyone is trying this, like I said I don't do this often.

'########################################################################## #####
'### this is the ItemAdd event code
Sub TargetFolderItems_ItemAdd(ByVal Item As Object)
'when a new item is added to our "watched folder" we can process it
Dim olAtt As Attachment
Dim i As Integer

If Item.Attachments.Count > 0 Then
For i = 1 To Item.Attachments.Count
Set olAtt = Item.Attachments(i)
'save the attachment with prepended Timer for unique filenames
olAtt.SaveAsFile FILE_PATH & CLng(Timer()) & olAtt.FileName

'if its an Acrobat file, pass the filepath to the print routine
If UCase(Right(olAtt.FileName, 3)) = "PDF" Then
PrintAtt (FILE_PATH & CLng(Timer()) & olAtt.FileName)
End If
Next
End If

Set olAtt = Nothing

End Sub