What "type" should olMailItem be?

squidgeny

Board Regular
Joined
Jul 26, 2011
Messages
130
Hi all,

I can make the below subroutine work if I declare olMailItem as Variant. But, as I understand it (and my understanding is somewhat limited - I'm new to VBA), Variant is a sort of wildcard. Can anyone tell me what it specifically should be?

I've tried Outlook.MailItem but that gives me a "User-defined type not defined" error.

Code:
Option Explicit
Sub testEmail()

    Dim myOutlook As Object
    Dim myMailItem As Object
    
    Set myOutlook = CreateObject("Outlook.Application")
    
    Set myMailItem = myOutlook.CreateItem(olMailItem)

    myMailItem.Display

    Set myMailItem = Nothing
    Set myOutlook = Nothing
    
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
You can use OutLook.MailItem if you have set a reference to Outlook (early binding). Otherwise it's Object.
 
Upvote 0
Hi,
olMailItem is the Outlook's constant equal to zero.
Without reference (VBE-Tools-References) to Outlook define this constant in Excel explicitly.

Rich (BB code):

Option Explicit
Sub TestEmail()

    ' olMailItem is the Outlook Application's constant,
    ' therefore define it here (in Excel) explicitly
    Const olMailItem As Long = 0
    
    Dim myOutlook As Object
    Dim myMailItem As Object
    
    ' Try using of already open Outlook Application
    On Error Resume Next
    Set myOutlook = GetObject(, "Outlook.Application")
    
    ' If Outlook Application was not active then create it
    If Err Then Set myOutlook = CreateObject("Outlook.Application")
    On Error GoTo 0
    
    Set myMailItem = myOutlook.CreateItem(olMailItem)

    myMailItem.Display

    Set myMailItem = Nothing
    Set myOutlook = Nothing
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,181
Messages
6,129,355
Members
449,506
Latest member
nomvula

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top