Joeseph Mother
Joeseph Mother

Reputation: 48

Control already created Outlook message from excel VBA

longtime reader, first time messenger (not my first time making a bad joke though),

I would like to know if it is possible to gain control of an Outlook email message that has already been created. At work we have to download new work orders from a secure website, thanks mostly to this site, I have been able to set up a macro that logs in, finds the new work orders, and clicks the button to open the work order. Once this button is clicked, a new IE window is opened with a pdf file, and the "Send page by email" command is used to create a new outlook message. I have the outlook 12 reference (using Office 2007), and am able to take control of an existing outlook session to create a new email using:

Dim SendOrder As Outlook.Application
Set SendOrder = GetObject(, "Outlook.Application")

But I cannot figure out how to make it control the email message that was opened by IE. I tried using GetObject(, "Outlook.Application.MailItem), and a few other failed ideas, but 3 ideas were all I had, so I'm hoping someone on here can help me out with this, otherwise I'll probably have to save the file in IE and create a new email message, which seems like adding an extra step.

Upvotes: 1

Views: 898

Answers (1)

David Zemens
David Zemens

Reputation: 53623

You're on the right path, I think. Something like this works with Outlook mailItems opened from Outlook. I have not tested it on mailItems opened from IE, though.

Sub GetAMailItem()

'## Requires reference to MS Outlook object library ##
Dim oApp As Outlook.Application
Dim mItem As MailItem

Set oApp = GetObject(, "Outlook.Application")

If TypeName(oApp.ActiveWindow) = "Inspector" Then
    Set mItem = oApp.ActiveWindow.CurrentItem
End If

Set oApp = Nothing

End Sub

Found the guts of that code here, just made a modification or two to give you a structured example that might suit your needs.

Upvotes: 3

Related Questions