none
Print Outlook attachment from add-in code

    Question

  • Hi all,

    I am developing an Outlook add-in and want to add a feature that prints out attachments of e-mail messages.
    Exactly as Outlooks does when you go to the File menu, Print... and check the "Print attached files. Attachments will print to the default printer only" checkbox.

    Does anyone has a code sample about how to achieve that, after I have the EmailItem object selected?

    Many thanks!
    Mauricio.-
    Monday, November 23, 2009 3:20 PM

Answers

  • Hello Mauricio,

    As far as I know, Outlook object model dose not expose such a method to print a attachment. In Outlook, there is only a PrintOut method [See: MailItem.PrintOut Method], which is used in Item of Outlook.

    Best regards,
    Bessie
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bessie Zhao Monday, November 30, 2009 1:59 AM
    Wednesday, November 25, 2009 6:30 AM

All replies

  • Hello Mauricio,

    As far as I know, Outlook object model dose not expose such a method to print a attachment. In Outlook, there is only a PrintOut method [See: MailItem.PrintOut Method], which is used in Item of Outlook.

    Best regards,
    Bessie
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bessie Zhao Monday, November 30, 2009 1:59 AM
    Wednesday, November 25, 2009 6:30 AM
  • It's true there is only the PrintOut Method.

    If the "print attached files. Attachments will print to the default printer only" is checked Outlook will print the mail followed by all attachments.

    But if you want more control over the process you can
    1)Save the attachment to file
    2)Use windows ShellExecute with the verb "print" to let Windows handle the print.
    3)The downside of this is
        - windows must know the file type to know how to print.
        - if the user had set the check in the aboved mentioned dialog the attachment are printed several times.

    In VBA
    This is a sniff you need code to open the folder etc.

    'Declare Windows Shell API shell32.dll that we will use to print
    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
    ...
    ...

    If (Not myFolder Is Nothing) And (Not myFolderDest Is Nothing) Then

        Count = myFolder.Items.Count

         Set olExp = Application.ActiveExplorer

        

        For i = Count To 1 Step -1

            Set Itm = myFolder.Items(i)

            If (Itm.MessageClass = "IPM.Post") Or (Itm.MessageClass = "IPM.Note") Then

                olExp.Activate

                Itm.PrintOut

                ' is there any attachments?
                Set myAttachments = Itm.Attachments

                If myAttachments.Count > 0 Then

                    'for all attachments

                    For iAttachment = 1 To myAttachments.Count

                        'save attachment in temp temp folder

                        myAttachments(iAttachment).SaveAsFile strTemp & myAttachments(iAttachment).DisplayName

                        'print attachment using Windows shell print

                        strFile = strTemp & myAttachments(iAttachment).DisplayName

                        ReturnVal = ShellExecute(0&, "print", strFile, 0&, 0&, 0&)

                    Next iAttachment ' For iAttachment = 1 To myAttachments.Count

                End If

                Set myAttachments = Nothing

            End If
    ...
    ...


    If anyone know how one programmaticly check or set the "print attached files. Attachments will print to the default printer only" I would be very glad...
    I tried to find it in gooogle but nope...

    Best regards
    Michael


    MCSD .Net, MCSE Windows 2003, MCDBA SQL 2000, MCTS SQL 2005
    Tuesday, December 22, 2009 5:07 PM