none
print email in Outlook with VB.NET RRS feed

  • Question

  • Hello

    How can I print a eMail which is activated in Outlook 2016 as VB.NET-Code.

    Perhaps select to print only the first page ?

    Hint:

    In Word 2016 I have this Code, but I can not transform from word to outlook:

    Dim WordAGBdruck As New Microsoft.Office.Interop.Word.Application

    Dim WordAGBdruckDoc As Microsoft.Office.Interop.Word.Document

    WordAGBdruckDoc = WordAGBdruck.Documents.Open(AGBpfad)

    WordAGBdruckDoc.PrintOut(

    Background:=True,

    Append:=False,

    Copies:="1",

    Pages:="1-2",

    PrintToFile:=False,

    Collate:=True,

    ManualDuplexPrint:=False)

    System.Threading.Thread.Sleep(3000)

    WordAGBdruck = Nothing

    WordAGBdruckDoc = Nothing

    Have someone a sample ???

    Thanks GM


    G. M.

    • Moved by Neda Zhang Wednesday, November 2, 2016 3:11 AM related to outlook
    Tuesday, November 1, 2016 4:50 PM

All replies

  • VBA code below may help:

    http://www.vbaexpress.com/kb/getarticle.php?kb_id=942


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Tuesday, November 1, 2016 5:44 PM
  • Hi 7-7,

    Thank you for posting in MSND Forum.

    Since your issue is more related to Outlook, I will move this thread to the more related forum.

    Reference: https://social.msdn.microsoft.com/Forums/en-US/home?forum=outlookdev&filter=alltypes&sort=lastpostdesc

    Thanks for your understanding and support.

    Best Regards,

    Neda Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 2, 2016 3:09 AM
  • Thank you --- but I need VB.NET --- not VBA.

    In VBA I found many solutions, but none in VB.NET

    Danke - in VBA habe ich jede Menge gefunden - ABER in VB.NET (was ich benötige) leider GAR NICHTS


    G. M.


    • Edited by 7-7 Wednesday, November 2, 2016 6:24 AM
    Wednesday, November 2, 2016 6:20 AM
  • Hello,

    The PrintOut method of the MailItem class prints the Outlook item using all default settings.The PrintOut method is the only Outlook method that can be used for printing.


    [custom.development]

    Wednesday, November 2, 2016 8:41 AM
  • Are you having troubles translating VBA to VB.Net?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, November 2, 2016 2:15 PM
  • Hello, 

    the writing of VBA is complete different to VB.NET for me.

    I can´t change this.


    G. M.

    Wednesday, November 2, 2016 2:32 PM
  • That is VBA again.

    I can not change this to VB.NET


    G. M.

    Wednesday, November 2, 2016 2:34 PM
  • Hello, 

    the writing of VBA is complete different to VB.NET for me.

    I can´t change this.


    G. M.

    Well it's not that different, but there are some tweaks. Here is the converted code. I would recommend adding Try...Catch exception handling.

        Sub PrintSelectedOutlookEmails()
    
            Dim objOL As Microsoft.Office.Interop.Outlook.Application
            Dim objMsg As Microsoft.Office.Interop.Outlook.MailItem
            Dim objAttachments As Microsoft.Office.Interop.Outlook.Attachments
            Dim objSelection As Microsoft.Office.Interop.Outlook.Selection
            Dim i As Integer
            Dim lngCount As Integer
            Dim Response As Short
            Dim msg As String
            Dim strSubject As String
    
    
            ' Instantiate an Outlook Application object.
            objOL = GetObject(, "Outlook.Application")
            ' Get the collection of selected objects.
            objSelection = objOL.ActiveExplorer.Selection
    
            ' Check selected item for attachments.
            For i = objSelection.Count To 1 Step -1
                lngCount = objSelection(i).Attachments.Count
                If lngCount > 0 Then
                    strSubject = objSelection(i).Subject
                    If strSubject = "" Then
                        msg = "Selected item #" & i
                    Else
                        msg = strSubject
                    End If
                    Response = MsgBox(msg & " has attachments." & vbCr & "Do you wish to delete?", MsgBoxStyle.YesNo)
                    If Response = MsgBoxResult.No Then
                        objSelection(i).PrintOut()
                    Else
                        objSelection(i).PrintOut()
                        objSelection(i).Delete()
                    End If
                Else
                    objSelection(i).PrintOut()
                    objSelection(i).Delete()
                End If
            Next
    
    ExitSub:
            objAttachments = Nothing
            objMsg = Nothing
            objSelection = Nothing
    
            objOL = Nothing
    
        End Sub


    Paul ~~~~ Microsoft MVP (Visual Basic)



    Wednesday, November 2, 2016 3:06 PM
  • Hi,

    Neda moved this case because you issue is related to Office and Our forum is not specific for VBA.

    If you want to use VB.NET, community member and we could help you convert to VB.NET.

    The code you shared is used for Word. It is different in Outlook. We could not convert the code and apply to Outlook.

    In Outlook, if you want to print out emails using VB.NET, you could use the code below. It prints out unread messages.

    Imports System.Reflection
    Imports System.Runtime.InteropServices
    Imports Outlook = Microsoft.Office.Interop.Outlook
    
    Module Module1
    
        Sub Main()
            Dim app As Outlook.Application
            Dim ns As Outlook._NameSpace
            Dim inbox As Outlook.Folder
            Dim items As Outlook.Items
            Dim item As Object
            Dim mailItem As Outlook.MailItem
            app = New Outlook.Application()
            ns = app.GetNamespace("MAPI")
            inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
            items = inbox.Items.Restrict("[UnRead] = true")
            For i As Integer = 1 To items.Count
                item = items(i)
                mailItem = TryCast(item, Outlook.MailItem)
                If mailItem IsNot Nothing Then
                    mailItem.PrintOut()
                End If
                Marshal.ReleaseComObject(item)
            Next
            If items IsNot Nothing Then Marshal.ReleaseComObject(items)
            If inbox IsNot Nothing Then Marshal.ReleaseComObject(inbox)
            If ns IsNot Nothing Then Marshal.ReleaseComObject(ns)
            If app IsNot Nothing Then Marshal.ReleaseComObject(app)
        End Sub
    

    Regards,

    Celeste



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 3, 2016 5:32 AM
    Moderator
  • Thank you very much.

    It prints out EVERY unread eMail.

    I tried to change to the active email, but no function:

    items = inbox.CurrentView
    

    items = inbox.Items.Restrict("[Active] = true") 

    But I can't find something

    Is there a website in the internet with information what combinations are possible.

    For example:

    https://msdn.microsoft.com/en-us/library/office/ff869662.aspx

    But there are only a few examples and VBA, but I need VB.NET


    G. M.

    Thursday, November 3, 2016 11:41 AM
  • Thank you very much.

    It prints out EVERY unread eMail.

    I tried to change to the active email, but no function:

    items = inbox.CurrentView
    

    items = inbox.Items.Restrict("[Active] = true") 

    But I can't find something

    Is there a website in the internet with information what combinations are possible.

    For example:

    https://msdn.microsoft.com/en-us/library/office/ff869662.aspx

    But there are only a few examples and VBA, but I need VB.NET


    G. M.


    Did you actually look at my code or no? My understanding was that you want to print out the selected item or items? Is this correct? If not, then you need to define what you mean by "active email".

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, November 3, 2016 1:49 PM
  • Hi,

    I tested code Paul shared and failed to work, so I create a new, but it seems that Paul's code is more suitable.

    For the demo form Paul, objOL = GetObject(, "Outlook.Application") doesn’t work for me, I need to use objOL = Marshal.GetActiveObject("Outlook.Application")

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 4, 2016 6:08 AM
    Moderator
  • Answer to the comment of Paul ~~~~ Microsoft MVP (Visual Basic)

    Thank you very much for your help.

    Your code delete some of my emails.

    Perhaps because of the different querys of you.

    I shortened your code for function:

            Dim objOL As Microsoft.Office.Interop.Outlook.Application

            Dim objMsg As Microsoft.Office.Interop.Outlook.MailItem
            Dim objAttachments As Microsoft.Office.Interop.Outlook.Attachments
            Dim objSelection As Microsoft.Office.Interop.Outlook.Selection
            Dim strSubject As String
            Dim Seite As System.Object = 1
            objOL = CType(GetObject(, "Outlook.Application"), Outlook.Application)
            objSelection = objOL.ActiveExplorer.Selection
            strSubject = objSelection(1).Subject
            ' Interaction.MsgBox(vbCrLf & "Titel des Aktiven eMails (ist Nummer 1):" & vbCrLf & strSubject & vbCrLf & vbCrLf & vbCrLf & vbCrLf & "OK drücken" & vbCrLf & vbCrLf & vbCrLf & vbCrLf, Microsoft.VisualBasic.MsgBoxStyle.MsgBoxSetForeground, "Titel des Aktiven eMails (ist Nummer 1):")
            objSelection(1).PrintOut()
    ' ExitSub
            objAttachments = Nothing
            objMsg = Nothing
            objSelection = Nothing
            objOL = Nothing
    

    My Problem ist to include Information which of the pages should be print AND how often these should be print.

    I tried different things, but can not find parameters, which have function.

    Perhaps by ID....

    Or by selection.....

    Can you help me ???

    GM


    G. M.

    Wednesday, November 9, 2016 1:34 PM
  • The Outlook object model is common for all kind of applications and programming languages. 

    [custom.development]

    Wednesday, November 9, 2016 1:38 PM
  • The Outlook object model doesn't provide anything for that. You can't exclude some part of the email from printing. However, you can create a copy of the message somewhere, edit it according to your needs, print it and then delete a copy.

    Also you may try to use the Word object model. The fact is that Outlook uses Word as an email editor. 

    There are three main ways for working with bodies in Outlook:

    1. Body.
    2. HTMLBody.
    3. Word editor. The Inspector class provides the WordEditor property which returns an instance of the Document class from the Word object model which represents the message body. You can read more about that in the Chapter 17: Working with Item Bodies .

    The PrintOut method of the Document class allows to specify the following parameters:

    Name

    Required/Optional

    Data Type

    Description

    Background

    Optional

    Variant

    Set to True to have the macro continue while Microsoft Word prints the document.

    Append

    Optional

    Variant

    Set to True to append the specified document to the file name specified by the OutputFileName argument. False to overwrite the contents of OutputFileName.

    Range

    Optional

    Variant

    The page range. Can be any WdPrintOutRange constant.

    OutputFileName

    Optional

    Variant

    If PrintToFile is True, this argument specifies the path and file name of the output file.

    From

    Optional

    Variant

    The starting page number when Range is set to wdPrintFromTo.

    To

    Optional

    Variant

    The ending page number when Range is set to wdPrintFromTo.

    Item

    Optional

    Variant

    The item to be printed. Can be any WdPrintOutItem constant.

    Copies

    Optional

    Variant

    The number of copies to be printed.

    Pages

    Optional

    Variant

    The page numbers and page ranges to be printed, separated by commas. For example, "2, 6-10" prints page 2 and pages 6 through 10.

    PageType

    Optional

    Variant

    The type of pages to be printed. Can be any WdPrintOutPages constant.

    PrintToFile

    Optional

    Variant

    True to send printer instructions to a file. Make sure to specify a file name with OutputFileName.

    Collate

    Optional

    Variant

    When printing multiple copies of a document, True to print all pages of the document before printing the next copy.

    FileName

    Optional

    Variant

    The path and file name of the document to be printed. If this argument is omitted, Word prints the active document. (Available only with the Application object.)

    ActivePrinterMacGX

    Optional

    Variant

    This argument is available only in Microsoft Office Macintosh Edition. For additional information about this argument, consult the language reference Help included with Microsoft Office Macintosh Edition.

    ManualDuplexPrint

    Optional

    Variant

    True to print a two-sided document on a printer without a duplex printing kit. If this argument is True, the PrintBackground and PrintReverseproperties are ignored. Use the PrintOddPagesInAscendingOrder and PrintEvenPagesInAscendingOrder properties to control the output during manual duplex printing. This argument may not be available to you, depending on the language support (U.S. English, for example) that you?ve selected or installed.

    PrintZoomColumn

    Optional

    Variant

    The number of pages you want Word to fit horizontally on one page. Can be 1, 2, 3, or 4. Use with the PrintZoomRow argument to print multiple pages on a single sheet.

    PrintZoomRow

    Optional

    Variant

    The number of pages you want Word to fit vertically on one page. Can be 1, 2, or 4. Use with the PrintZoomColumn argument to print multiple pages on a single sheet.

    PrintZoomPaperWidth

    Optional

    Variant

    The width to which you want Word to scale printed pages, in twips (20 twips = 1 point; 72 points = 1 inch).

    PrintZoomPaperHeight

    Optional

    Variant

    The height to which you want Word to scale printed pages, in twips (20 twips = 1 point; 72 points = 1 inch).


    [custom.development]

    Wednesday, November 9, 2016 1:45 PM
  • Hello

    I tried very much but at the end there are failures which I did can not eleminate.


    I also find the solution in VBA, but after hours I give up to convert it to VB.NET:

    http://www.vboffice.net/en/developers/print-first-page-of-an-email/

    I also find a beginning, but can't do any further:

    objDoc = objOL.ActiveInspector.WordEditor
    objSel = objDoc.Windows(1).Selection

    Perhaps I can get a complete code in VB.NET.

    I have Visual Studio Community 2015.

    GM



    G. M.


    • Edited by 7-7 Sunday, November 13, 2016 2:03 AM
    Sunday, November 13, 2016 2:02 AM
  • That's exactly what I suggested to use - the PrintOut method of the Document class from the Word object model. 

    What failures are you talking about? Do you get any errors in the code?

    VBA and VB.NET are very close to each other. But in VB.NET add-ins you don't get global properties like VBA provides (for example, the Application object). You need to declare your objects in the code and then use them. The only thing you can start from is the Application property of the add-in class (ThisAddin). 

    Finally, you may find the Converting Code from VBA to Visual Basic .NET article helpful. 


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Sunday, November 13, 2016 8:16 PM