Share via


Opening Outlook from Visual Basic

Question

Tuesday, January 27, 2015 4:00 PM

Hello all;

My program is integrated with Microsoft Outlook and this works well, but I have an issue. On an number of different processes (packing slips, Invoices, PO's, Sales Confirmations, etc.) I create a Mail Item in Outlook that automatically attaches a pdf of the document, fills out the subject line of the email, and writes in the body of them email. This works perfect. Once the user fills out the email address to be sent to, and hits "SEND" the email goes to the OutBox in Outlook. The issue is, if Outlook is not open, that email will not send and sometimes the user forgets to open Outlook before they log out so they may have pending emails waiting to be sent. So what I am doing now is forcing Outlook to open after the program creates the Mail Item.

This is my code.

       

Dim myProcess As New Process()

        myProcess.StartInfo.FileName =

"C:\Program Files (x86)\Microsoft Office\Office12\OUTLOOK.exe"

        myProcess.Start()

This works fine, but I need to know two things;

1) How to open Outlook minimized, because when I do this, it opens on top of the Mail Item (and the tendency is to close...)

2) How to not open Outlook if Outlook is already open. I tested this, and if I keep doing this it opens multiple copies of Outlook.

Any help would be appreciated.

Todd

Todd Lerew

All replies (12)

Tuesday, January 27, 2015 5:49 PM ✅Answered

Thanks for the response...

The code

myProcess.StartInfo.WindowStyle=ProcessWindowStyle.Minimized

does not work - it still comes up maximized in front of all other windows.

Todd Lerew

 That is strange. It shows the splash screen on mine but, as soon as that goes away the window is opened minimized in my taskbar. What does your code look like now, is it copied just how it is shown above or did you change it around a little?

If you say it can`t be done then i`ll try it


Tuesday, January 27, 2015 6:58 PM ✅Answered

 I really am not familiar with using Outlook or mail items. I just figured your question was just about starting a process which i am familiar with. Perhaps, someone else that knows about using outlook from vb.net can see a reason why that is happening.   8)

If you say it can`t be done then i`ll try it


Tuesday, January 27, 2015 4:17 PM

Hi,

 You can do it like this if you do not want multiple instances to be opened and you want it opened minimized.

Public Class Form1
    Private WithEvents P As Process

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If P Is Nothing Or (P IsNot Nothing AndAlso P.HasExited) Then
            P = New Process
            P.StartInfo.FileName = "OUTLOOK.exe"
            P.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
            P.Start()
        End If
    End Sub
End Class

If you say it can`t be done then i`ll try it


Tuesday, January 27, 2015 4:19 PM

1. You cannot only set the myProcess.StartInfo.WindowStyle to ProcessWindowStyle.Minimized like you normally would when you start Outlook but you can try to use the SetForegroundWindow native API as described in the following thread: https://social.msdn.microsoft.com/Forums/windows/en-US/bf320bbe-c20c-4b51-b2dd-e0a65e62d503/keeping-outlook-out-of-the-way-systemdiagnosticsprocessstart?forum=winforms

  1. Check whether there is an Outlook process already running using the Process.GetProcessesByName method before you start the process:
        If Not Process.GetProcessesByName("OUTLOOK").Any() Then

            Dim myProcess As New Process()
            myProcess.StartInfo.FileName = "C:\Program Files (x86)\Microsoft Office\Office12\OUTLOOK.exe"
            myProcess.Start()

        End If

Please remember to mark helpful posts as answer and/or helpful.


Tuesday, January 27, 2015 5:41 PM

Thanks for the response...

The code

myProcess.StartInfo.WindowStyle=ProcessWindowStyle.Minimized

does not work - it still comes up maximized in front of all other windows.

Todd Lerew


Tuesday, January 27, 2015 5:45 PM

Thanks for the response...

If Outlook is already open, and I attempt to run your code --

 (If Not Process.GetProcessesByName("OUTLOOK").Any() Then)

it errors out on me when creating the new Mail Item (see below)

'creating outlook mailitem 

Dim mail As MailItem

'creating newblank mail message

mail = OLApp.CreateItem(OlItemType.olMailItem)

Todd Lerew


Tuesday, January 27, 2015 5:52 PM

I changed the code to

If Not Process.GetProcessesByName("OUTLOOK.EXE").Any() Then

(I added the EXE to the process name)

and it no longer errors out on me, but opens a new instance of Outlook, like it was doing before.

Todd Lerew


Tuesday, January 27, 2015 5:58 PM

Here is my entire code, starting with where I create the Mail Item...

   

'creating outlook mailitem

Dim mail As MailItem

'creating newblank mail message

mail = OLApp.CreateItem(OlItemType.olMailItem)

'adding subject information to the mail message

mail.Subject =

"Truweld / Trufit Packing List"

'adding body message information to the mail message

 mail.Body =

"This is a Packing List for a Shipment from Truweld / TFP Corporation."

'adding attachment

mail.Attachments.Add(

"\Tfp5\TransferData\TruweldPackList\TWPL" & ConfirmName & ".pdf")

'display mail

mail.Display()

If Not Process.GetProcessesByName("OUTLOOK.EXE").Any() Then

Dim myProcess As New Process()

myProcess.StartInfo.FileName =

"C:\Program Files (x86)\Microsoft Office\Office12\OUTLOOK.exe"

myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Minimized

myProcess.Start()

End If

Todd Lerew


Tuesday, January 27, 2015 6:06 PM

The process name should not have the ".EXE" extension on it in this line. It should be just the name "OUTLOOK".

If Not Process.GetProcessesByName("OUTLOOK.EXE").Any() Then

 If that does not help then please edit your post and use the Code Block button to insert your code. It is kind of hard to follow like that.   8)

If you say it can`t be done then i`ll try it


Tuesday, January 27, 2015 6:09 PM

Yeah, I figured that after it opened the new instance of Outlook.

But if I do that and Outlook is already open, it errors out on me.

This is the line in my code that it errors out on...

mail = OLApp.CreateItem(OlItemType.olMailItem)

Todd Lerew


Tuesday, January 27, 2015 7:26 PM | 1 vote

Hi Todd,

my suggestion is use the Microsoft.Interop.Outlook.dll. I think this is the best way to use Outlook from your VB.NET Application. So you have almost full control of Outlook. There are a lot of Examples in the internet. 

Jens Gerber


Tuesday, January 27, 2015 7:41 PM

I appreciate your help. I am not so much worried about having multiple instances of Outlook open, because the chances are that they would not open Outlook first before they print and attach the documents to an email.

I was just hoping to figure out how to automatically open Outlook minimized so that when they do attach the document (in this case, a packing slip), they don't have to remember to open Outlook before they log out so that the document will send.

At one of our divisions, a woman who emails Purchase Orders from my program constantly forgets to open Outlook when she is done, so that the emails (Mail Items) she created in my program sit there for days sometimes. Either way, you showed me how to force Outlook open, which helps, but it does open maximized in front of all other windows, and if Outlook is already open, it opens a new instance (which I can deal with...).

Thanks!

Todd Lerew