locked
Late binding not some workstations RRS feed

  • Question

  • Good morning,

    I changed two references from early to late binding as follows and the result compiles fine, works on some machines and not on others.  What could cause this problem?

    /Joe

    Wintransdll.dll

    Replaced: New Wintrans
    With: CreateObject("Wintrans")

    MSOUTL.OLB
    Replaced: 
       Dim objOutlook As Outlook.Application
       Dim objOutlookMsg As Outlook.MailItem
       Dim objOutlookRecip As Outlook.Recipient
       Dim objOutlookAttach As Outlook.Attachment

    With: 
       Dim objOutlook As Object
       Dim objOutlookMsg As Object
       Dim objOutlookRecip As Object
       Dim objOutlookAttach As Object

       Set objOutlook = CreateObject("Outlook.Application")
       Set objOutlookMsg = CreateObject("Outlook.MailItem")
       Set objOutlookRecip = CreateObject("Outlook.Recipient")
       Set objOutlookAttach = CreateObject("Outlook.Attachment")

    Tuesday, May 15, 2012 6:34 AM

Answers

  • Model_m wrote:
     
    > I changed two references from early to late binding as follows and the
    > result compiles fine, works on some machines and not on others.  What
    > could cause this problem?
    >
    > MSOUTL.OLB
    > Replaced:
    >    Dim objOutlook As Outlook.Application
    >    Dim objOutlookMsg As Outlook.MailItem
    >    Dim objOutlookRecip As Outlook.Recipient
    >    Dim objOutlookAttach As Outlook.Attachment
    > With:
    >    Dim objOutlook As Object
    >    Dim objOutlookMsg As Object
    >    Dim objOutlookRecip As Object
    >    Dim objOutlookAttach As Object
    >    Set objOutlook = CreateObject("Outlook.Application")
    >    Set objOutlookMsg = CreateObject("Outlook.MailItem")
    >    Set objOutlookRecip = CreateObject("Outlook.Recipient")
    >    Set objOutlookAttach = CreateObject("Outlook.Attachment")
     
    The declarations are fine, also the application object, but MailItem,
    Recipient and Attachment are children of objOutlook or objOutlookMsg and
    require the parent object as starting point:
     
    [vbnet]
    Set objOutlook = CreateObject("Outlook.Application")
    Set objOutlookMsg = objOutlook.CreateItem(olMailItem) 'olMailItem=0
    objOutlookMsg.Recipients.Add "some@email.com"
    objOutlookMsg.Attachments.Add "C:\Path\File.ext"
    [/vbnet]
     
    --
    Peter Doering [MVP Access]
     
    • Marked as answer by Model_m Tuesday, May 15, 2012 11:32 AM
    Tuesday, May 15, 2012 7:05 AM

All replies

  • Model_m wrote:
     
    > I changed two references from early to late binding as follows and the
    > result compiles fine, works on some machines and not on others.  What
    > could cause this problem?
    >
    > MSOUTL.OLB
    > Replaced:
    >    Dim objOutlook As Outlook.Application
    >    Dim objOutlookMsg As Outlook.MailItem
    >    Dim objOutlookRecip As Outlook.Recipient
    >    Dim objOutlookAttach As Outlook.Attachment
    > With:
    >    Dim objOutlook As Object
    >    Dim objOutlookMsg As Object
    >    Dim objOutlookRecip As Object
    >    Dim objOutlookAttach As Object
    >    Set objOutlook = CreateObject("Outlook.Application")
    >    Set objOutlookMsg = CreateObject("Outlook.MailItem")
    >    Set objOutlookRecip = CreateObject("Outlook.Recipient")
    >    Set objOutlookAttach = CreateObject("Outlook.Attachment")
     
    The declarations are fine, also the application object, but MailItem,
    Recipient and Attachment are children of objOutlook or objOutlookMsg and
    require the parent object as starting point:
     
    [vbnet]
    Set objOutlook = CreateObject("Outlook.Application")
    Set objOutlookMsg = objOutlook.CreateItem(olMailItem) 'olMailItem=0
    objOutlookMsg.Recipients.Add "some@email.com"
    objOutlookMsg.Attachments.Add "C:\Path\File.ext"
    [/vbnet]
     
    --
    Peter Doering [MVP Access]
     
    • Marked as answer by Model_m Tuesday, May 15, 2012 11:32 AM
    Tuesday, May 15, 2012 7:05 AM
  • Peter,

    Thank you for your help.  I changed the code as you wrote.  I noticed that for my function I did not need the recipients added and that the attachment is a string passed to the function.  Here is the code as I have it now.  Please let me know if you have any further comments.

    Thank you,

    Joe

    Public Function SendMessage(Optional emailAdd As String, Optional AttachmentPath)
    
        Dim objOutlook As Object
        Dim objOutlookMsg As Object
        Dim objOutlookRecip As Object
        Dim objOutlookAttach As Object
    
        On Error GoTo SendMessage_Error
    
        ' Create the Outlook session.
        Set objOutlook = CreateObject("Outlook.Application")
        ' Create the message.
        Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
    
        With objOutlookMsg
            ' Set the Subject, Body, and Importance of the message.
            .Subject = " "    '"This is an Automation test with Microsoft Outlook"
            ''''.Body = " " '"Hello!!!" & vbCrLf & vbCrLf
            .Importance = olImportanceHigh  'High importance
    
            ' Add attachments to the message.
            If Not IsMissing(AttachmentPath) Then
                .Attachments.add AttachmentPath
             End If
    
        End With
        Set objOutlookMsg = Nothing
        Set objOutlook = Nothing
    
        On Error GoTo 0
        Exit Function
    
    SendMessage_Error:
    
        MsgBox "Error " & Err.Number & " (" & Err.DESCRIPTION & ") in procedure SendMessage of Module Send Message"
    End Function

    Tuesday, May 15, 2012 7:48 AM
  • Joe,
     
    Model_m wrote:
     
    >  Please let me know if you have any further comments.
     
    In late binding the standard Outlook constants won't be available. You have
    to declare them in the module header ...
     
    Private Constant olMailItem = 0
    Private Constant olImportanceHigh = 2
     
    ... or use their values instead:
     
    > Public Function SendMessage(Optional emailAdd As String, Optional AttachmentPath)
    > [...]
    >     Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
     
    Set objOutlookMsg = objOutlook.CreateItem(0) 'olMailItem
     
    >         .Importance = olImportanceHigh  'High importance
     
    .Importance = 2 'High importance
     
    For olMailItem it would still work without declaration, as its value is 0,
    which is what you get for undeclared constants.
     
    --
    Peter Doering [MVP Access]
     
    Tuesday, May 15, 2012 8:07 AM
  • Peter,

    Just tried it.  Not working.  What am I missing?  Nothing happens.  Not even an error message.

        Dim objOutlook As Object
        Dim objOutlookMsg As Object
        Dim objOutlookRecip As Object
        Dim objOutlookAttach As Object
    
        ' Create the Outlook session.
        Set objOutlook = CreateObject("Outlook.Application")
        ' Create the message.
        Set objOutlookMsg = objOutlook.CreateItem(0) 'olMailItem = 0
    
        With objOutlookMsg
            ' Set the Subject, Body, and Importance of the message.
            .Subject = " "    '"This is an Automation test with Microsoft Outlook"
            ''''.Body = " " '"Hello!!!" & vbCrLf & vbCrLf
            .Importance = 2 'olImportanceHigh =2  'High importance
    
            ' Add attachments to the message.
            If Not IsMissing(AttachmentPath) Then
                .Attachments.add AttachmentPath
             End If
    
        End With
        Set objOutlookMsg = Nothing
        Set objOutlook = Nothing

    Tuesday, May 15, 2012 8:57 AM
  • Model_m wrote:
     
    > Just tried it.  Not working.  What am I missing?  Nothing happens.  Not
    > even an error message.
     
    Sure, you're just preparing the mail so far. If you want to send it
    automatically you have to add following line:
     
    > [...]
    >         If Not IsMissing(AttachmentPath) Then
    >             .Attachments.add AttachmentPath
    >          End If
     
    .Send
     
    >     End With
    > [...]
     
    --
    Peter Doering [MVP Access]
     
    Tuesday, May 15, 2012 9:13 AM
  • Peter,

    It's not supposed to send automatically.  It's supposed to leave the message open for the user to type in a recipient, subject, and body.

    Just some context: My application creates a PDF of a report and saves it. This function is supposed to take the PDF, attach it to an email message and leave it open for the user to fill in details and to send it out.

    /Joe

    OK.  Found the problem.  By mistake I deleted the line of ".Display" so it was creating the message and not showing it.

    We'll do some more testing but I think we can close this now.  Thank you for your help.

    /Joe

    • Edited by Model_m Tuesday, May 15, 2012 9:39 AM Found the problem.
    Tuesday, May 15, 2012 9:18 AM
  • Joe,
     
    Model_m wrote:
     
    > It's not supposed to send automatically.  It's supposed to leave the
    > message open for the user to type in a recipient, subject, and body.
     
    In this case you have to set the message visible and hand over control to
    the user. I'm typing this from memory, without having tested:
     
    objOutlook.Visible=True
    objOutlook.UserControl=True
     
    Then you have to destroy the references as usual, but do not quit the
    application object as otherwise Outlook will be ended.
     
    --
    Peter Doering [MVP Access]
     
    Tuesday, May 15, 2012 11:05 AM