Assigning References Dynamically

  • Thread starter Thread starter PiperSkip
  • Start date Start date
P

PiperSkip

Guest
Is It Possible to Assign References Dynamically?

I'm developing an enhancement for a 35 user business application written in Access 2000 (largely VBA) on the fromt end and SQL Server on the back end. I'm attempting to generate an Outlook e-mail message in response to a particular event. I've do this before in closely controlled environments where all user were running the same version of Outlook, but in this case we have users running versions 8, 9 and 10; primarily Outlook 2000 and Outlook XP. My enhancement runs out of a mdb library to the main program were the same library is delivered to all users.

If I reference a particular version of Outlook and a user has a different one installed that user gets an error during program loading and emails will not be generated.

If I try late binding using the GetObject function, I can't reference Outlook properties and methods necessary to create the message.

I'm looking for some way to determine the user's installed Outlook version and establish the appropriate reference at run time, perhaps involving conditional compilation.

Anyone have any experiece solving this problem or suggestions?

Thanks for your time and brain power.

PiperSkip (now Drum Major Skip because of bad wrists)
 
late-binding

I cant think of a way to do this other than late-binding.

If I try late binding using the GetObject function, I can't reference Outlook properties and methods necessary to create the message.

Intellisense won't work with late binding, but you should still be able to use the properties and methods. I'm assuming that you are using the Object data-type to represent all the outlook objects.

Having said all that, does it work if you build using the lowest version of Outlook?
 
First thing to say is that I haven't tried this myself yet - but I may need to do something similar soon, so I would be very interested If you posted back your solution when you find one.

I would experiment with the References object:
- IsBroken property, could list your broken references, there is an example in the help, and the
- References.AddFromFile "C:\WINNT\system32\scrrun.dll" could add a reference

(Access2002)
hth

SWK
 
Yes, you can assign references dynamically.

On this thread I posted some functions that determine whether the user is using DAO or ADO and can disable one or the other and switch between them.

They might give you an idea of how to set the references dynamically.
 
With the encouragement that I should be able to get this to work with late binding I have pressed on... I am now close but not quite there.

The following code works fine when Outlook9 is present as a reference:

Public Sub SendOutlookMailItem(strRecipients As String, strSubject As String, varMessage As Variant)
Dim objApp As Object
Dim objNameSpace As Object
Dim objMailItem As Object

Set objApp = GetObject("", "Outlook.Application")
Set objNameSpace = objApp.GetNamespace("MAPI")
Set objMailItem = objApp.CreateItem(olMailItem)

With objMailItem
.To = strRecipients
.Subject = strSubject
.Body = varMessage
.Send
End With

Set objMailItem = Nothing
Set objNameSpace = Nothing
Set objApp = Nothing

End Sub

Using a calling statement like:

Call SendOutlookMailItem("LOHCo@comcast.net","Test2","This is the second test message.")

Without the reference however, I get a compile error on the statement:
Set objMailItem = objApp.CreateItem(olMailItem)
because the complier does not recognize the Outlook object type 'olMailItem'. Putting olMailItem in double quotes ( as in the GetNamespace method just above it satisfies the compiler, but generates a runtime compile error of "Variable not defined."

Any suggestions on how to overcome this last hurtle?

Thanks again
 
olMailItem

olMailItem is an enumerated constant try defining it like this:

Public Const olMailItem = 0
 
Ah duh!!! I suppose I could blame that oversight on the medication I'm taking for the cold, but probably it was just a brain cramp.

With your addition the code works just fine. Now back to work enhancing the application for the customer.

Thanks everyone for your help.
 

Users who are viewing this thread

Back
Top Bottom