Question : type mismatch error when creating an Outlook mail item from Access VBA

I am using Access VBA to create an Outlook mail message. The code has worked fine on my previous setup, but fails on my new setup. The code extract is below, but the line which causes the problem ("Type Mismatch", error 13) is:

Set objOutlookMsg = OutlookApp.CreateItem(olMailItem)

This works fine on the PC I originally used (Windows XP running Access 2000, with Outlook XP), but I am now using a Vista PC running Access 2003 and Outlook 2003 (but with the Access app still in 2000 format). If I copy the app back to the previous PC, it still works, even though the reference is to Outlook 11.

I have found references to this problem on other sites, mostly relating to the previous line of code (Set OutlookApp = GetObject(, "Outlook.Application")), but nobody seems to have resolved it in any case. It makes no difference whether Outlook is already active or not - for me creating the Outlook object is not a problem - just the attempt to create the mail item.

Some of the other comments talk about interference from anti virus, buit I don't think that this can be the cause - both of my PCs run Norton 360 V2 and as I said one of them executes the code without a problem.

I would like to continue to use this code if I can - I see no reason why it shouldn't work in principle. The only significant difference is that the new PC is using Outlook 2003 and not Outlook XP. I am aware that security is tighter in this version, but I don't see why it would generate a "Type Mismatch" error. Failing a direct solution, is there any way in which I could achieve the same result (setting up and populating a draft mail message) any other way?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
Dim OutlookApp As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem
    Dim objOutlookRecip As Outlook.Recipient
    Dim objOutlookAttach As Outlook.Attachment
    Dim strTitle As String
    Dim strMessage As String
    
    'create the Outlook session
    Set OutlookApp = GetObject(, "Outlook.Application")
    If Err.Number <> 0 Then
        Set OutlookApp = CreateObject("Outlook.Application")
    End If
    On Error GoTo ErrPoint
 
    'create the message.
    Set objOutlookMsg = OutlookApp.CreateItem(olMailItem)
    'On Error Resume Next
    With objOutlookMsg
        'add the Recipient(s), Subject, Body, and attachments to the message.
        Set objOutlookRecip = .Recipients.Add(pstrTo)
        objOutlookRecip.Type = olTo
        .CC = pstrCC
        .Subject = pstrSub
        .Body = pstrMsg & vbCrLf & vbCrLf & pstrSig & vbCrLf & vbCrLf
        If Not IsMissing(pvarAttachmentPath) Then
            Set objOutlookAttach = .Attachments.Add(pvarAttachmentPath)
        End If
        'resolve each Recipient's name.
        For Each objOutlookRecip In .Recipients
            objOutlookRecip.Resolve
            If Not objOutlookRecip.Resolve Then
                objOutlookMsg.Display
            End If
        Next
        .Display
    End With
    'On Error GoTo ErrPoint

Answer : type mismatch error when creating an Outlook mail item from Access VBA

I would try rewriting this using late binding.  Remove there reference to the Outlook library, and make the code:



    Dim OutlookApp As Object 'Outlook.Application
    Dim objOutlookMsg As Object 'Outlook.MailItem
    Dim objOutlookRecip As Object 'Outlook.Recipient
    Dim objOutlookAttach As Object 'Outlook.Attachment
    Dim strTitle As String
    Dim strMessage As String
   
    Const olMailItem As Long = 0

    'create the Outlook session
    Set OutlookApp = GetObject(, "Outlook.Application")
    If Err.Number <> 0 Then
        Set OutlookApp = CreateObject("Outlook.Application")
    End If
    On Error GoTo ErrPoint
 
    'create the message.
    Set objOutlookMsg = OutlookApp.CreateItem(olMailItem)
    'On Error Resume Next
    With objOutlookMsg
        'add the Recipient(s), Subject, Body, and attachments to the message.
        Set objOutlookRecip = .Recipients.Add(pstrTo)
        objOutlookRecip.Type = olTo
        .CC = pstrCC
        .Subject = pstrSub
        .Body = pstrMsg & vbCrLf & vbCrLf & pstrSig & vbCrLf & vbCrLf
        If Not IsMissing(pvarAttachmentPath) Then
            Set objOutlookAttach = .Attachments.Add(pvarAttachmentPath)
        End If
        'resolve each Recipient's name.
        For Each objOutlookRecip In .Recipients
            objOutlookRecip.Resolve
            If Not objOutlookRecip.Resolve Then
                objOutlookMsg.Display
            End If
        Next
        .Display
    End With
    'On Error GoTo ErrPoint
Random Solutions  
 
programming4us programming4us