Log in

View Full Version : Print pdf files and mark as read



carnick
12-12-2011, 01:16 PM
I have a script I have modified to print all unread pdf files in the inbox and mark them as read. Now I would like to add a date and timestamp to the attachments that print out. Is there a way to add this? If not enjoy using this code because it took me a while to modify and it works so far. :)

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

Function printFile(pdfName As String)
ShellExecute 0, "Print", pdfName, vbNullString, "", 1
End Function

Public Sub PrintAttachments()
Dim myInbox As MAPIFolder
Dim mailItem As mailItem
Dim attchmt As Attachment
Dim myItem As Outlook.mailItem
Dim pdfName As String
Set myInbox = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
For Each mailItem In myInbox.Items
For Each attchmt In mailItem.Attachments
If mailItem.UnRead = True Then
If (InStr(1, attchmt, ".pdf", vbTextCompare) <> 0) Then
pdfName = "C:\Temp\" & attchmt.Filename
attchmt.SaveAsFile pdfName
Call printFile(pdfName)
End If
End If
Next
For Each attchmt In mailItem.Attachments
If UCase(Right(attchmt.Filename, 3)) = "PDF" Then
mailItem.UnRead = False
End If
Next
Next
Set myInbox = Nothing
End Sub

carnick
12-12-2011, 01:38 PM
Also for this script to work you need to create a folder on the C drive called Temp.

C:\Temp\

monarchd
12-12-2011, 03:50 PM
If you want to add the Received Date and Time use:


pdfName = "C:\Temp\" & attchmt.FileName & Format(mailItem.ReceivedTime, "yyyymmdd_hhnnss_")

carnick
12-13-2011, 06:42 AM
Thanks for the reply. I want to be able to add that to the pdf when it prints out. So the paper copy says the date/time it was printed. Any ideas?

carnick
12-13-2011, 08:38 AM
*So the paper copy says the date/time it was received.

monarchd
12-13-2011, 08:56 AM
I can't think of a way to append a PDF.... I think with text it can be done but not sure of how to do that with PDF's.

carnick
12-13-2011, 09:28 AM
Here is the script so far if anyone needs it. I modified it a little further but still no luck with adding the filename to the pdf print out.

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

Function printFile(pdfName As String)
ShellExecute 0, "Print", pdfName, vbNullString, "", 1
End Function

Public Sub PrintAttachments()
Dim myInbox As MAPIFolder
Dim mailItem As mailItem
Dim attchmt As Attachment
Dim myItem As Outlook.mailItem
Dim pdfName As String
Set myInbox = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
For Each mailItem In myInbox.Items
For Each attchmt In mailItem.Attachments
If mailItem.UnRead = True Then
If (InStr(1, attchmt, ".pdf", vbTextCompare) <> 0) Then
pdfName = "C:\Temp\" & Format(mailItem.ReceivedTime, "mm-dd-yyyy_hh-nn_") & attchmt.Filename
attchmt.SaveAsFile pdfName
Call printFile(pdfName)
End If
End If
Next
For Each attchmt In mailItem.Attachments
If UCase(Right(attchmt.Filename, 3)) = "PDF" Then
mailItem.UnRead = False
End If
Next
Next
Set myInbox = Nothing
End Sub

carnick
12-13-2011, 10:46 AM
My printer can add a watermark to the header of a document with the date/time. I tried it with a pdf file and it does work. I just dont know how to code it to add that to any pdf that prints out. Any ideas?

monarchd
12-14-2011, 07:57 AM
Maybe something like this, add in your default printer name where noted. Backup and then test.


Option Explicit
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
Function printFile(pdfName As String)
Dim pdfprinter As New CDIntf.CDIntf
Dim lRet As Long
pdfprinter.DriverInit ("Enter Printer Name HERE")
pdfprinter.SetDefaultConfig
pdfprinter.SetDefaultPrinter
lRet = pdfprinter.SetWatermark("Test Watermark" & vbNewLine & Now, "Arial", 20, 0, &HFF, 50, -700, True)
pdfprinter.FileNameOptions = 64
lRet = ShellExecute(Me.hWnd, "Print", pdfName, 0&, 0&, vbMaximizedFocus)
'Restore default configuration
pdfprinter.DefaultFileName = ""
pdfprinter.FileNameOptions = 0
pdfprinter.SetDefaultConfig
pdfprinter.DriverEnd

End Function
Public Sub PrintAttachments()
Dim myInbox As MAPIFolder
Dim mailItem As mailItem
Dim attchmt As Attachment
Dim myItem As Outlook.mailItem
Dim pdfName As String

Set myInbox = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)

For Each mailItem In myInbox.Items
For Each attchmt In mailItem.Attachments
If mailItem.UnRead = True Then
If (InStr(1, attchmt, ".pdf", vbTextCompare) <> 0) Then
pdfName = "C:\Temp\" & Format(mailItem.ReceivedTime, "mm-dd-yyyy_hh-nn_") & attchmt.FileName
attchmt.SaveAsFile pdfName
Call printFile(pdfName)
End If
End If
Next
For Each attchmt In mailItem.Attachments
If UCase(Right(attchmt.FileName, 3)) = "PDF" Then
mailItem.UnRead = False
End If
Next
Next
Set myInbox = Nothing
End Sub

monarchd
12-14-2011, 08:12 AM
I'm trying to mix in these calls with the ShellExecute...I got to test it here and looks like my attempt above will need more adjustments:

http://www.amyuni.com/WebHelp/Amyuni_Document_Converter/Printer_Installation_and_Activation/Methods/DriverInit.htm

carnick
12-14-2011, 01:46 PM
Thanks, I wouldn't even begin to know how to code that or where to go from here. Thank you for working on it I appreciate it. Please let me know how your test goes when you get a chance. Thanks again.

carnick
12-21-2011, 09:10 AM
Here is an update to the script. We found ourselves getting orders with the same file name at the same second and they were automatically copying over the first file without warning. My solution was to make it also place the millisecond in the file name. It has been working perfect. This script is perfect now besides it not appending the date/timestamp on the print out. I have been playing around with the code above but cannot get it to work. Any ideas? Any help would be greatly appreciated. Here is the working script so far. I have tested it on multiple computers and with Outlook 2007 and 2010. It definitely works if anyone needs it.

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

Function printFile(pdfName As String)
ShellExecute 0, "Print", pdfName, vbNullString, "", 1
End Function

Public Sub PrintAttachments()
Dim myInbox As MAPIFolder
Dim mailItem As mailItem
Dim attchmt As Attachment
Dim myItem As Outlook.mailItem
Dim pdfName As String
Set myInbox = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
For Each mailItem In myInbox.Items
For Each attchmt In mailItem.Attachments
If mailItem.UnRead = True Then
If (InStr(1, attchmt, ".pdf", vbTextCompare) <> 0) Then
pdfName = "C:\Temp\" & Format(mailItem.ReceivedTime, "mm-dd-yyyy_hh-nn-ss") & "-" & Right(Format(Timer, "#0.00"), 2) & "_" & attchmt.Filename
attchmt.SaveAsFile pdfName
Call printFile(pdfName)
End If
End If
Next
For Each attchmt In mailItem.Attachments
If UCase(Right(attchmt.Filename, 3)) = "PDF" Then
mailItem.UnRead = False
End If
Next
Next
Set myInbox = Nothing
End Sub

carnick
01-04-2012, 11:35 AM
Please help :)