PDA

View Full Version : Automatically saving & print messages - PDFs



benjaminjb
08-21-2007, 06:48 AM
Good morning everyone,

After finding Killian's quite popular post on how to save and print attachments in .xls format, it got me thinking about a problem I had late last year. Instead of saving and printing Microsoft file formats (e.g. ppt, xls, doc) automatically, has anyone altered Killian's code to automatically save and print Adobe Acrobat PDF file formats received in email?

Apologies for not being able to post a link to Killian's post, the forum requires 5 or more posts before allowing me to post links.

Much appreciated,

Benjamin

benjaminjb
08-21-2007, 02:12 PM
Good evening,

I've figured out how to save my multiple incoming PDF email attachments to assigned folders... the final battle is to have them automatically print after being saved. Any additional help will be extremely helpful!

Thanks everyone.
Benjamin

PS:
I've pasted the code currently being used:


'save attachment routine
Sub SavAttachment002()
Dim oOutlook As Outlook.Application
Dim oNs As Outlook.Namespace 'Main Outlook Today
Dim oFldrSb As Outlook.MAPIFolder 'Sub Folder in Outlook Today
Dim oFldrSbSb As Outlook.MAPIFolder 'Sub in Sub Folder
Dim oMessage As Object
Dim sPathName As String
Dim oAttachment As Outlook.Attachment
Dim iCtr As Integer
Dim iAttachCnt As Integer

sPathName = "C:\Location" 'My Folder Path where to save attachments

Set oOutlook = New Outlook.Application
Set oNs = oOutlook.GetNamespace("MAPI")
Set oFldrSb = oNs.Folders("Mailbox - Last Name, First Name")
Set oFldrSbSb = oFldrSb.Folders("Inbox")
Set oFldrSbSbSb = oFldrSbSb.Folders("Temp")

For Each oMessage In oFldrSbSbSb.Items
With oMessage.Attachments
iAttachCnt = .Count

If iAttachCnt > 0 Then
For iCtr = 1 To iAttachCnt
.Item(iCtr).SaveAsFile sPathName _
& .Item(iCtr).Filename
Next iCtr
End If
End With

DoEvents
Next oMessage

SaveAttachments = True
End Sub

geekgirlau
08-21-2007, 06:11 PM
Welcome to the Board Benjamin :006:

When you post code, make sure you use the VBA tags (select the text, then click on the "vba" button). I've edited your post to do this for you.

The attachment object in Outlook doesn't have a Print method - my understanding (although I could be wrong) is that you need to open it within the relevant application and print from there. Of course you would also need to test for the document type to do this properly.

benjaminjb
08-23-2007, 08:51 AM
Thanks for the reply.

I've a new approach to my problem in finding a way to automatically print PDF attachments through MSFT Outlook. I've found a neat way using HTML to auto convert .PDF to .Doc and save to my harddrive. The next step is how can I auto print .Doc file attachments through Outlook. Keep in mind, the code below works pefectly in saving the attachments to a designated folder. Anyone have a decent solution and/or code to auto print .Doc file attachments?

Thanks a bunch,
Benjamin

javavibe
08-28-2007, 10:58 AM
I found a way to automatically print PDF's, but geekgirlau is right you have use Acrobat to print the attachment. I have Acrobat Professional 7.0 loaded on my system and have turned on the reference to it in the VBA editor. You can do command line calls to Acrobat reader as well. I have posted the code to the forum, but haven't done enough posts to put it here. If you do a search for my username (javavibe) you should find the thread. The title is "Automating Outlook to save & print pdf attachments".

benjaminjb
08-29-2007, 08:22 AM
Awesome, thanks a ton Javavibe! I'm going to play around with this code this afternoon for sure... I'll let ya know how it goes.

Benjamin

benjaminjb
08-30-2007, 07:40 AM
Good morning Javavibe,

I'm new to VBA more or less, and well as I try to work with the code you've posted, I continue to strike a wall when trying to make this a macro. My preference would be to have this code within a module, and run the macro to process the assigned sub folder within the Inbox.

The folder directory is:
("Mailbox - Last, First"), ("Inbox"), ("AutoPrint")

I was not certain how to code down to two or three subfolders...

Again, much appreciated for the help.

Benjamin

javavibe
08-30-2007, 01:53 PM
My apologies, I didn't read the request to run this as a Macro. This code is actually placed in the ThisOutlookSession under the Microsoft Office Outlook Objects when you open the VBA project editor (Atl-F11). The way I have this setup is I have a rule that checks messages when they arrive in the Inbox by a specific sender. Once the message is identified, it is moved into a different folder (called Cansew). The code checks the message when they arrive in the Cansew folder (located in the "this is the Application_Startup event code in the ThisOutlookSession module"). You can change that reference to any folder in your list. For example you could substitute AutoPrint for Cansew, as I think that's where you would like to print from. I was just reviewing my original post, I realized I have not provided the full code. Sorry, I'm a noob. :p I'll put it down below this message. You might have to change the command line that prints the attachement in Acrobat. My code is based on having Acrobat 7.0 loaded on your system. I know Acrobat Reader can also print from command line if that the software you will be using but I would have to research it a little further if that's what you are going to use. They have slightly different syntax, so I'm not sure that would work with Acrobat Reader. Also you have to have your Acrobat references turned on. You can find them in the Tools/References in the menu bar. Just check the ones you want to use. I hope this helps. Full code below:
'########################################################################## #####
'### 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 & CLng(Timer()) & 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 (FILE_PATH & olAtt.FileName), 1
PrintPDF2 (fFullPath), 1
'MsgBox "Data has been sent. " & vbLf & _
"Please close the instance of Acrobat Reader after printing."
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