Ken Keniee Ken Keniee -4 years ago 75
Vb.net Question

Using Outlook events with automation

Using Events with Automation explains how to use Outlook Events with Automation. I don't however understand how to implement that code into my project.

I want to use the

InspectorBeforeMinimizeEvent
method as explained in Inspector.BeforeMinimize Event (Outlook)

Whenever the user moves/resizes/maximizes/minimizes the inspector manually then the
MessageBox
should pop up.

Following code is for testing:

Imports Microsoft.Office.Interop

Public Class Form1

Public WithEvents myIns As Outlook.Inspector

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

Process.Start("Outlook.exe")
Threading.Thread.Sleep(3000)
Dim olApp As New Outlook.Application

Dim myMailItem As Outlook.MailItem
myMailItem = CType(olApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
myMailItem.Subject = "Hello"
myMailItem.To = "anybody@example.com"
myMailItem.Body = "Hi there..."

Dim myIns As Outlook.Inspector
myIns = myMailItem.GetInspector

myIns.Display(False)

Dim myWord As Word.Document
myWord = CType(myIns.WordEditor, Word.Document)

Dim mySel As Word.Selection
mySel = myWord.Application.Selection

Threading.Thread.Sleep(10000)

'Following line just for testing. Normally following line doesnt exist in my original code. The real scenario is the user minimizes the inspector manually.
myIns.WindowState = Outlook.OlWindowState.olMinimized

myWord.InlineShapes.AddPicture(FileName:="C:\Example.png", LinkToFile:=False, SaveWithDocument:=True, Range:=mySel.GoTo(What:=Word.WdGoToItem.wdGoToLine, Which:=Word.WdGoToDirection.wdGoToLast, Count:=-4))

'myMailItem.Send()

End Sub

End Class


Event code:

Private Sub myIns_BeforeMinimize(Cancel As Boolean)
MessageBox.Show("You are minimizing this inspector.", "Title", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
End Sub

Answer Source

To get this to work you need to add the handler for Activate and everytime the MailItem is activated you would set the WindowState:

Private Sub myIns_Activate() Handles myIns.Activate
    myIns.WindowState = Outlook.OlWindowState.olMinimized
End Sub

I would also remove these lines:

Process.Start("Outlook.exe")
Threading.Thread.Sleep(3000)

Dim myIns As Outlook.Inspector

Threading.Thread.Sleep(10000)

You don't need the Process.Start as that is handled with Dim olApp As New Outlook.Application. You also don't need Dim myIns As Outlook.Inspector as that is handled with Public WithEvents myIns As Outlook.Inspector. I appreciate the Threading.Thread.Sleep lines may be for testing purposes.

Your code would look something similar to this:

Imports Microsoft.Office.Interop

Public Class Form1

    Public WithEvents myIns As Outlook.Inspector

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Dim olApp As New Outlook.Application

        Dim myMailItem As Outlook.MailItem = CType(olApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
        myMailItem.Subject = "Hello"
        myMailItem.To = "anybody@example.com"
        myMailItem.Body = "Hi there..."

        myIns = myMailItem.GetInspector
        myIns.Display(False)

        Dim myWord As Word.Document
        myWord = CType(myIns.WordEditor, Word.Document)

        Dim mySel As Word.Selection
        mySel = myWord.Application.Selection

        myWord.InlineShapes.AddPicture(FileName:="C:\Example.png", LinkToFile:=False, SaveWithDocument:=True, Range:=mySel.GoTo(What:=Word.WdGoToItem.wdGoToLine, Which:=Word.WdGoToDirection.wdGoToLast, Count:=-4))

        myMailItem.Send()

    End Sub

    Private Sub myIns_Activate() Handles myIns.Activate
        myIns.WindowState = Outlook.OlWindowState.olMinimized
    End Sub

End Class
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download