• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

VB Code to Print All Emails in a PST

How can i modify this code:

Private 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
  
Dim objFSO As Object, _
    objTempFolder As Object

Sub PrintController()
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTempFolder = objFSO.GetSpecialFolder(2)
    PrintItems Application.ActiveExplorer.CurrentFolder
    Set objTempFolder = Nothing
    Set objFSO = Nothing
    MsgBox "Finished"
End Sub
  
Public Sub PrintItems(olkFolder As Outlook.Folder)
    Dim olkAttachment As Outlook.Attachment, _
        olkItem As Object, _
        olkSubfolder As Outlook.Folder
    On Error Resume Next
    For Each olkItem In olkFolder.Items
        For Each olkAttachment In olkItem.Attachments
            olkItem.PrintOut
            olkAttachment.SaveAsFile objTempFolder & "\" & olkAttachment.FileName
            ShellExecute 0&, "print", objTempFolder & "\" & olkAttachment.FileName, 0&, 0&, 0&
        Next
    Next
    For Each olkSubfolder In olkFolder.Folders
        PrintItems olkSubfolder
    Next
    On Error GoTo 0
    Set olkItem = Nothing
    Set olkAttachment = Nothing
End Sub

Open in new window


so that it does *NOT* print attachments? Or better yet, asks me whether I want to print attachments, giving me the option at runtime.

My ultimate goal is to be able print all emails in a PST to an Acrobat PDF. I have Acrobat 9 Pro installed. I can accept one PDF with all the emails in it, or one PDF per email *chain*, or one PDF per individual email.


NOTE:  code snippet written by Genius-level BlueDevilFan (@techniclee).
0
GrayStrickland
Asked:
GrayStrickland
1 Solution
 
David LeeCommented:
Hi Gray.

I've modified the code to prompt you and ask if you want to print the attachments to each message.  Simply replace the PrintItems subroutine from the original code with the version below.
Public Sub PrintItems(olkFolder As Outlook.Folder)
    Dim olkAttachment As Outlook.Attachment, _
        olkItem As Object, _
        olkSubfolder As Outlook.Folder
    On Error Resume Next
    For Each olkItem In olkFolder.Items
        olkItem.PrintOut
        If msgbox("Do you want to print the attachments to this message?", vbQuestion + vbYesNo, "Message - " & olkItem.Subject) = vbYes Then
            For Each olkAttachment In olkItem.Attachments
                olkAttachment.SaveAsFile objTempFolder & "\" & olkAttachment.FileName
                ShellExecute 0&, "print", objTempFolder & "\" & olkAttachment.FileName, 0&, 0&, 0&
            Next
        End If
    Next
    For Each olkSubfolder In olkFolder.Folders
        PrintItems olkSubfolder
    Next
    On Error GoTo 0
    Set olkItem = Nothing
    Set olkAttachment = Nothing
End Sub

Open in new window

0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now