Late Binding...? HOW??

pookie62

Registered User.
Local time
Today, 05:35
Joined
Jan 16, 2005
Messages
47
Hi,
I've been reading some posts about late binding to prevent having errors with different Office versions. I have this issue..
However, I don't know how to implement it in my own code.. :-(
Anyone willing to help me with this ??


Code:
Private Sub Exportmail_Click() 

      
' Private Sub testmail(DisplayMsg As Boolean, Optional AttachmentPath) 

          Dim objOutlook As Outlook.Application 
          Dim objOutlookMsg As Outlook.MailItem 
          Dim objOutlookRecip As Outlook.Recipient 
          Dim objOutlookAttach As Outlook.Attachment 
          Dim AttachmentPath 
          Dim DisplayMsg 

          ' Create the Outlook session. 
          Set objOutlook = CreateObject("Outlook.Application") 

          ' Create the message. 
          Set objOutlookMsg = objOutlook.CreateItem(olMailItem) 

          With objOutlookMsg 
              ' Add the To recipient(s) to the message. 
              Set objOutlookRecip = .Recipients.Add("webmaster@somedomain.nl") 
              objOutlookRecip.Type = olTo 

              ' Add the CC recipient(s) to the message. 
              'Set objOutlookRecip = .Recipients.Add("") 
              'objOutlookRecip.Type = olCC 
              
             ' Set the Subject, Body, and Importance of the message. 
             .Subject = "Export wedstrijd" 
             .Body = "Export van wedstrijd" & vbCrLf & vbCrLf 
             .Importance = olImportanceHigh 'High importance 

             ' Add attachments to the message. 
             AttachmentPath = Application.CurrentProject.Path & "\Export.xls" 
             Set objOutlookAttach = .Attachments.Add(AttachmentPath) 
            .Display 
            .Save 
            .Send 
          End With 
          Set objOutlook = Nothing 
      End Sub
 
In early binding you declare a specific object type. This is more efficient because your code knows exactly what's coming, and allocates the storage space in advance. Also, your objects display intellisense, and you can explore the class members in the object browser.
The drawback of early binding is that you must set a reference to a particular file which exposes the objects you're going to use. Particularly with Office products this is prone to versioning problems.

In late binding you declare your objects of type "Object" and use the CreateObject() method rather than the "New" keyword to create class instances.
This is a little slower, but far less likely to fail.

Change your code as follows, and remove the reference to Outlook.
Code:
Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim objOutlookRecip As Object
Dim objOutlookAttach As Object
What I do is set references and early bind during developement.
 
Good link from stallyon. In addition, I declare any named constants I want to use in a standard module.
Code:
Public Const xlMaximized = -4137
Public Const xlBottom = -4107
Public Const xlCenterAcrossSelection = 7
Public Const xlEdgeLeft = 7
Public Const xlEdgeTop = 8
Public Const xlEdgeBottom = 9
 
Thanks for your replies,

So the code should look like this then?
Code:
Private Sub Exportmail_Click() 

Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim objOutlookRecip As Object
Dim objOutlookAttach As Object
Dim AttachmentPath 
Dim DisplayMsg 

          ' Create the Outlook session. 
          Set objOutlook = CreateObject("Outlook.Application") 

          ' Create the message. 
          Set objOutlookMsg = objOutlook.CreateItem(olMailItem) 

          With objOutlookMsg 
              ' Add the To recipient(s) to the message. 
              Set objOutlookRecip = .Recipients.Add("webmaster@somedomain.nl") 
              objOutlookRecip.Type = olTo 

              ' Add the CC recipient(s) to the message. 
              'Set objOutlookRecip = .Recipients.Add("") 
              'objOutlookRecip.Type = olCC 
              
             ' Set the Subject, Body, and Importance of the message. 
             .Subject = "Export wedstrijd" 
             .Body = "Export van wedstrijd" & vbCrLf & vbCrLf 
             .Importance = olImportanceHigh 'High importance 

             ' Add attachments to the message. 
             AttachmentPath = Application.CurrentProject.Path & "\Export.xls" 
             Set objOutlookAttach = .Attachments.Add(AttachmentPath) 
            .Display 
            .Save 
            .Send 
          End With 
          Set objOutlook = Nothing 
      End Sub

Seems to work oke for me, sended it to a guy with Office 2000, hopefullu it's going oke there too..
Thanks again, guys !
 
Last edited:
No. You still have named constants: olMailItem, olTo, olCC, olImportanceHigh.
These are members of the referenced object model which should have been removed. You can replace them with the numbers they represent, or find out the values and declare them within your program as constants with the same names. I do the latter.
Code:
Public Const olMailItem = 0
 
Really appreciate your help..
Where or how do I find those values you're talking about ?
Have no clue...:confused:
Small example perhaps to put me on track ?
 
Look at response #8 in this post. All of the constants can be found both in Access Help and in the Object Browser. To make it a little easier, I made a quick little DB that gives you all the constants for all the built-in parameters, and that program is in the link above.

EDIT: Crap. I forgot that my constants DB only has the intrinsic Access constants in it. Outlook constants are in the Outlook constant object. To see what those constants are, make sure you have a reference to Microsoft Outlook X.0 Object Library set for your project, and then in the code window, press F2 to open up the object browser. In the search text box at the top, type in olMailItem and you see all the constants and their values related to that at the bottom where it's telling you the class members. You can type in whatever constants you need (olTO, olCC, etc.) to get each constant. Note that once you have found the set you're looking for (I.e., searching for olTO exposes olTO, olCC, olBCC, and olOriginator), you can just click each member in the right pane to see their constant value.

Sorry about the initial confusion.
 
Last edited:
To find the value of a constant you can also,
1) Make sure you have a reference set to Outlook.
2) Type...
Code:
? olMailItem
...in the immediate window
 
I searched and found the values like Moniker explained, declared them in a standard module like lagbolt said and things look oke.
I think I read to remove the references to all office libraries , correct ?
 
Well, remove the references to libraries whose objects you plan to late bind to. In your case, certainly remove the ref to Outlook.
The network I'm on people have a range of versions of Excel, but everybody has Access10, so everybody has Office10. I late bind to Excel objects, but early bind to Office.
 
Oke, I think I get the picture..
Thanks for helping out !
 

Users who are viewing this thread

Back
Top Bottom