Automate resetting reference to object library

  • Thread starter Thread starter OssieMac
  • Start date Start date
O

OssieMac

Access 2002 and 2003.

I currently have the code below to send emails. (Developed in Access 2003)
and it works fine.

I have set the Reference in Tools to Microsoft Outlook 11.0 Object Library.

If I load the project into Access 2002 then I get an error due to Missing
Reference and have to manually delete it and reset it to Microsoft Outlook
10.0 Object Library.

Is there any method of automating this so that I can allow a general user to
copy in updated Front Ends?

Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem
Dim strToAddress As String
Dim strBCC
Dim strSubject As String
Dim strBody As String
Dim strInvoicePathAndFile As String

Set objOutlook = CreateObject("Outlook.Application")
Set objEmail = objOutlook.CreateItem(olMailItem)

With objEmail
.To = strToAddress

If strBCC <> "" Then
.BCC = strBCC
End If

.Subject = strSubject

.Body = strBody

If Me.txtInvOrTxt = "Invoice" Then
.Attachments.Add (strInvoicePathAndFile)
End If

If Me.chkEmailEdit Then 'User can edit email
.Display

'Ensure message is top Active Window
AppActivate (strSubject & " - Message")
Else
.Send
End If

End With

Set objEmail = Nothing

Set objOutlook = Nothing
 
Far, far better would be to use Late Binding instead. Don't set any
reference to Outlook, and change your code to:

Dim objOutlook As Object ' Outlook.Application
Dim objEmail As Object ' Outlook.MailItem
Dim strToAddress As String
Dim strBCC
Dim strSubject As String
Dim strBody As String
Dim strInvoicePathAndFile As String

Const olMailItem As Long = 0

Set objOutlook = CreateObject("Outlook.Application")
Set objEmail = objOutlook.CreateItem(olMailItem)

With objEmail
.To = strToAddress

If strBCC <> "" Then
.BCC = strBCC
End If

.Subject = strSubject

.Body = strBody

If Me.txtInvOrTxt = "Invoice" Then
.Attachments.Add (strInvoicePathAndFile)
End If

If Me.chkEmailEdit Then 'User can edit email
.Display

'Ensure message is top Active Window
AppActivate (strSubject & " - Message")
Else
.Send
End If

End With

Set objEmail = Nothing

Set objOutlook = Nothing
 
Almost. You also need to supply a value for the intrinsic constant
olMailItem.
 
Thankyou Doug that works perfectly.

Also thanks ruralguy for your input.

--
Regards,

OssieMac


Douglas J. Steele said:
Far, far better would be to use Late Binding instead. Don't set any
reference to Outlook, and change your code to:

Dim objOutlook As Object ' Outlook.Application
Dim objEmail As Object ' Outlook.MailItem
Dim strToAddress As String
Dim strBCC
Dim strSubject As String
Dim strBody As String
Dim strInvoicePathAndFile As String

Const olMailItem As Long = 0

Set objOutlook = CreateObject("Outlook.Application")
Set objEmail = objOutlook.CreateItem(olMailItem)

With objEmail
.To = strToAddress

If strBCC <> "" Then
.BCC = strBCC
End If

.Subject = strSubject

.Body = strBody

If Me.txtInvOrTxt = "Invoice" Then
.Attachments.Add (strInvoicePathAndFile)
End If

If Me.chkEmailEdit Then 'User can edit email
.Display

'Ensure message is top Active Window
AppActivate (strSubject & " - Message")
Else
.Send
End If

End With

Set objEmail = Nothing

Set objOutlook = Nothing
 
One disadvantage to Late Binding is that since you don't declare the
object types, the VBA editor/debug environment cannot provide you with
the Intellisense (drop down list of properties & methods) when typing
the object's name.

Therefore, during development its better to set the reference and
declare the object types explicitly. Then when the code is finalized,
you can convert to late-binding.
 
Back
Top