Mister H
09-24-2012, 11:47 AM
Hi All:
I am hoping that someone here can assist or at least guide me in modifying the code below. First off this code was compiled with the help of others (THANKS JP) as well as me searching the web.
I have started a new Post as one part of my problem was fixed but I now need to further revise the code and my post was getting buried in replies to myself. (My apologies if I have wrongly started a new thread)
Code: This code is being used to Save and then Print all PDF attachments in a specified folder. The attachment prints and then the email message is Deleted (or moved to another specified folder if uncomment the code). I am looking for help to modify this code so it can be better performing (see QUESTIONS below)
Here is the code along with my comments:
Public Sub Print_Email_PDF_Attachments()
Dim Inbox As MAPIFolder
Dim Item As mailItem
Dim Atmt As Attachment
Dim Filename As String
Dim i As Integer
'Folder " 1) Faxes (PDFs) TO BE PRINTED " needs to be created under the User
Set Inbox = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Parent.Folders.Item("1) Faxes (PDFs) TO BE PRINTED")
For i = Inbox.Items.Count To 1 Step -1
For Each Atmt In Inbox.Items.Item(i).Attachments
'This line creates specific file names for each attachment with the
'Date/time received and 'uses a counter so the chance of file name 'duplication is reduced.
'make sure the folder " C:\PDF Faxes " exists
Filename = "C:\PDF Faxes\" & Right(Format(Timer, "#0.00"), 2) & "-" & Atmt.Filename
'This line saves the attachments.
Atmt.SaveAsFile Filename
'Now the PDF is to be printed
'please change the program folder accordingly if the Acrobat Reader is
'not installed on drive C:
'Shell """C:\Program Files\Adobe\Reader 8.0\Reader\acrord32.exe"" /h /p """ + FileName + """", vbHide
Shell """C:\Program Files\Adobe\Reader\acrord32.exe"" /h /p """ + Filename + """", vbHide
'Is there more then 1 attachment? If so then goto the next one
Next
'Mark the email as Read
Inbox.Items.Item(i).UnRead = False
'Now what do you want to do with the email message?
'If you want the email be deleted automatically
Inbox.Items.Item(i).Delete
'To move the item to s specified folder
'make sure the folder " C:\PDF Faxes " exists
'Use
'Inbox.Items.Item(i).Move Outlook.Session.GetDefaultFolder(olFolderInbox).Parent.Folders.Item("2) Faxes (PDFs) ALREADY PRINTED")
'I tried to DELETE the PDF file from C:\PDF Faxes\ but unfortunately I can
'not get the file to delete as the code tries to delete the file before it
'has finished printing
'I tried to unsuccessfully delay the code using this:
'Application.Wait Now + TimeSerial(0, 0, 10)
'Kill (Filename)
'Goto the Next email message
Next
Set Inbox = Nothing
End Sub
QUESTIONS (Mod Request):
A) How can I alter this code so that if the email contains NO ATTACHMENT or contains an attachment other then PDF it just moves onto the next message and leaves the email in the folder 1) Faxes (PDFs) TO BE PRINTED
OTHER Wishes:
1) Does anyone know how to make the delete code work properly so it deletes the file from the folder C:\PDF Faxes after it has printed OR possibly delete all files from that folder at the end of the code? The files are not required after they have been printed.
2) Right now after the code finishes running Adobe is open. Can the VBA close it?
THANKS to anyone that can assist.
Have a GREAT day ALL,
Mark
:hi:
EDIT:
Played around a bit more and got one portion working (I think). Question A seems to be solved but due to my VBA ignorance I have no idea if it will create other problems. While Testing it SEEMS to be working. I have bolded in red what I added. Here it is:
Public Sub Print_Email_PDF_Attachments()
Dim Inbox As MAPIFolder
Dim Item As mailItem
Dim Atmt As Attachment
Dim Filename As String
Dim i As Integer
'Folder " 1) Faxes (PDFs) TO BE PRINTED " needs to be created under the User
Set Inbox = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Parent.Folders.Item("1) Faxes (PDFs) TO BE PRINTED")
For i = Inbox.Items.Count To 1 Step -1
For Each Atmt In Inbox.Items.Item(i).Attachments
'This line creates specific file names for each attachment with the
'Date/time received and 'uses a counter so the chance of file name 'duplication is reduced.
'make sure the folder " C:\PDF Faxes " exists
Filename = "C:\PDF Faxes\" & Right(Format(Timer, "#0.00"), 2) & "-" & Atmt.Filename
'if its an Acrobat file then continue on to Save and Print VBA
'otherwise move onto next attachment
If Right(Atmt.Filename, 3) = "pdf" Then
'This line saves the attachments.
Atmt.SaveAsFile Filename
'Now the PDF is to be printed
'please change the program folder accordingly if the Acrobat Reader is
'not installed on drive C:
'Shell """C:\Program Files\Adobe\Reader 8.0\Reader\acrord32.exe"" /h /p """ + FileName + """", vbHide
Shell """C:\Program Files\Adobe\Reader\acrord32.exe"" /h /p """ + Filename + """", vbHide
'Mark the email as Read
Inbox.Items.Item(i).UnRead = False
'Now what do you want to do with the email message?
'If you want the email be deleted automatically
Inbox.Items.Item(i).Delete
'If it is not a PDF attachment based on this line of code
'If Right(Atmt.Filename, 3) = "pdf" Then move onto next attachment
End If
'Is there more then 1 attachment? If so then goto the next one
Next
'To move the item to s specified folder
'make sure the folder " C:\PDF Faxes " exists
'Use
'Inbox.Items.Item(i).Move Outlook.Session.GetDefaultFolder(olFolderInbox).Parent.Folders.Item("2) Faxes (PDFs) ALREADY PRINTED")
'I tried to DELETE the PDF file from C:\PDF Faxes\ but unfortunately I can
'not get the file to delete as the code tries to delete the file before it
'has finished printing
'I tried to unsuccessfully delay the code using this:
'Application.Wait Now + TimeSerial(0, 0, 10)
'Kill (Filename)
'Goto the Next email message
Next
Set Inbox = Nothing
End Sub
Outstanding Mod Requests:
1) Does anyone know how to make the delete code work properly so it deletes the file from the folder C:\PDF Faxes after it has printed OR possibly delete all files from that folder at the end of the code? The files are not required after they have been printed.
2) Right now after the code finishes running Adobe is open. Can the VBA close it?
THANKS,
Mark
I am hoping that someone here can assist or at least guide me in modifying the code below. First off this code was compiled with the help of others (THANKS JP) as well as me searching the web.
I have started a new Post as one part of my problem was fixed but I now need to further revise the code and my post was getting buried in replies to myself. (My apologies if I have wrongly started a new thread)
Code: This code is being used to Save and then Print all PDF attachments in a specified folder. The attachment prints and then the email message is Deleted (or moved to another specified folder if uncomment the code). I am looking for help to modify this code so it can be better performing (see QUESTIONS below)
Here is the code along with my comments:
Public Sub Print_Email_PDF_Attachments()
Dim Inbox As MAPIFolder
Dim Item As mailItem
Dim Atmt As Attachment
Dim Filename As String
Dim i As Integer
'Folder " 1) Faxes (PDFs) TO BE PRINTED " needs to be created under the User
Set Inbox = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Parent.Folders.Item("1) Faxes (PDFs) TO BE PRINTED")
For i = Inbox.Items.Count To 1 Step -1
For Each Atmt In Inbox.Items.Item(i).Attachments
'This line creates specific file names for each attachment with the
'Date/time received and 'uses a counter so the chance of file name 'duplication is reduced.
'make sure the folder " C:\PDF Faxes " exists
Filename = "C:\PDF Faxes\" & Right(Format(Timer, "#0.00"), 2) & "-" & Atmt.Filename
'This line saves the attachments.
Atmt.SaveAsFile Filename
'Now the PDF is to be printed
'please change the program folder accordingly if the Acrobat Reader is
'not installed on drive C:
'Shell """C:\Program Files\Adobe\Reader 8.0\Reader\acrord32.exe"" /h /p """ + FileName + """", vbHide
Shell """C:\Program Files\Adobe\Reader\acrord32.exe"" /h /p """ + Filename + """", vbHide
'Is there more then 1 attachment? If so then goto the next one
Next
'Mark the email as Read
Inbox.Items.Item(i).UnRead = False
'Now what do you want to do with the email message?
'If you want the email be deleted automatically
Inbox.Items.Item(i).Delete
'To move the item to s specified folder
'make sure the folder " C:\PDF Faxes " exists
'Use
'Inbox.Items.Item(i).Move Outlook.Session.GetDefaultFolder(olFolderInbox).Parent.Folders.Item("2) Faxes (PDFs) ALREADY PRINTED")
'I tried to DELETE the PDF file from C:\PDF Faxes\ but unfortunately I can
'not get the file to delete as the code tries to delete the file before it
'has finished printing
'I tried to unsuccessfully delay the code using this:
'Application.Wait Now + TimeSerial(0, 0, 10)
'Kill (Filename)
'Goto the Next email message
Next
Set Inbox = Nothing
End Sub
QUESTIONS (Mod Request):
A) How can I alter this code so that if the email contains NO ATTACHMENT or contains an attachment other then PDF it just moves onto the next message and leaves the email in the folder 1) Faxes (PDFs) TO BE PRINTED
OTHER Wishes:
1) Does anyone know how to make the delete code work properly so it deletes the file from the folder C:\PDF Faxes after it has printed OR possibly delete all files from that folder at the end of the code? The files are not required after they have been printed.
2) Right now after the code finishes running Adobe is open. Can the VBA close it?
THANKS to anyone that can assist.
Have a GREAT day ALL,
Mark
:hi:
EDIT:
Played around a bit more and got one portion working (I think). Question A seems to be solved but due to my VBA ignorance I have no idea if it will create other problems. While Testing it SEEMS to be working. I have bolded in red what I added. Here it is:
Public Sub Print_Email_PDF_Attachments()
Dim Inbox As MAPIFolder
Dim Item As mailItem
Dim Atmt As Attachment
Dim Filename As String
Dim i As Integer
'Folder " 1) Faxes (PDFs) TO BE PRINTED " needs to be created under the User
Set Inbox = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Parent.Folders.Item("1) Faxes (PDFs) TO BE PRINTED")
For i = Inbox.Items.Count To 1 Step -1
For Each Atmt In Inbox.Items.Item(i).Attachments
'This line creates specific file names for each attachment with the
'Date/time received and 'uses a counter so the chance of file name 'duplication is reduced.
'make sure the folder " C:\PDF Faxes " exists
Filename = "C:\PDF Faxes\" & Right(Format(Timer, "#0.00"), 2) & "-" & Atmt.Filename
'if its an Acrobat file then continue on to Save and Print VBA
'otherwise move onto next attachment
If Right(Atmt.Filename, 3) = "pdf" Then
'This line saves the attachments.
Atmt.SaveAsFile Filename
'Now the PDF is to be printed
'please change the program folder accordingly if the Acrobat Reader is
'not installed on drive C:
'Shell """C:\Program Files\Adobe\Reader 8.0\Reader\acrord32.exe"" /h /p """ + FileName + """", vbHide
Shell """C:\Program Files\Adobe\Reader\acrord32.exe"" /h /p """ + Filename + """", vbHide
'Mark the email as Read
Inbox.Items.Item(i).UnRead = False
'Now what do you want to do with the email message?
'If you want the email be deleted automatically
Inbox.Items.Item(i).Delete
'If it is not a PDF attachment based on this line of code
'If Right(Atmt.Filename, 3) = "pdf" Then move onto next attachment
End If
'Is there more then 1 attachment? If so then goto the next one
Next
'To move the item to s specified folder
'make sure the folder " C:\PDF Faxes " exists
'Use
'Inbox.Items.Item(i).Move Outlook.Session.GetDefaultFolder(olFolderInbox).Parent.Folders.Item("2) Faxes (PDFs) ALREADY PRINTED")
'I tried to DELETE the PDF file from C:\PDF Faxes\ but unfortunately I can
'not get the file to delete as the code tries to delete the file before it
'has finished printing
'I tried to unsuccessfully delay the code using this:
'Application.Wait Now + TimeSerial(0, 0, 10)
'Kill (Filename)
'Goto the Next email message
Next
Set Inbox = Nothing
End Sub
Outstanding Mod Requests:
1) Does anyone know how to make the delete code work properly so it deletes the file from the folder C:\PDF Faxes after it has printed OR possibly delete all files from that folder at the end of the code? The files are not required after they have been printed.
2) Right now after the code finishes running Adobe is open. Can the VBA close it?
THANKS,
Mark