1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

VBA append a pdf file with date/time it was received in Outlook

Discussion in 'Software Development' started by carnick, Dec 22, 2011.

Thread Status:
Not open for further replies.
  1. carnick

    carnick Thread Starter

    Joined:
    Dec 22, 2011
    Messages:
    2
    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. So the paper copy says the date/time it was received. Or it could say the filename on the printout since I have it adding the date/time to the filename when saving. Is there a way to add this to the printout?

    Here is the code I have so far:

    Code:
    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 
     
  2. carnick

    carnick Thread Starter

    Joined:
    Dec 22, 2011
    Messages:
    2
    I setup a watermark through my printer to add the date/time to everything that is printed. Is their a way to make it specific to just pdf files through vba code in Outlook?
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1032421

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice