changing from reference to late binding

R

red6000

Hi,

I have some code that calls Outlook. Unfortunatley I have to write the code
on a pc with OL2003, but the PC that it will run on is OL2002 (and so the
reference needs to be Outlook 10.0 not 11.0). I can't change the reference
on the pc that it will run on, so I think i need to remove the reference and
use late binding, but I'm struggling. My code is below, what changes do I
need to make?

Public Sub EmailImage(FName As String)


Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem

On Error Resume Next
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
MsgBox ("There has been a problem connecting to your email
account and the mail will not be sent")
Exit Sub
End If

Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem
.To = "(e-mail address removed)"
.subject = "***FILEONLY***"
.Attachments.Add Source:=FName
.body = "Please scan the attached document as FILEONLY"
.BodyFormat = olFormatPlain
.Send
End With

Set oItem = Nothing
Set oOutlookApp = Nothing

End Sub
 
J

Jonathan West

red6000 said:
Hi,

I have some code that calls Outlook. Unfortunatley I have to write the
code on a pc with OL2003, but the PC that it will run on is OL2002 (and so
the reference needs to be Outlook 10.0 not 11.0). I can't change the
reference on the pc that it will run on, so I think i need to remove the
reference and use late binding, but I'm struggling. My code is below,
what changes do I need to make?

Public Sub EmailImage(FName As String)


Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem

On Error Resume Next
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
MsgBox ("There has been a problem connecting to your email
account and the mail will not be sent")
Exit Sub
End If

Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem
.To = "(e-mail address removed)"
.subject = "***FILEONLY***"
.Attachments.Add Source:=FName
.body = "Please scan the attached document as FILEONLY"
.BodyFormat = olFormatPlain
.Send
End With

Set oItem = Nothing
Set oOutlookApp = Nothing

End Sub

With any luck, you need only change two lines. Change this

Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem

to this

Dim oOutlookApp As Object
Dim oItem As Object


The essence of late binding is that you declare your variables As Object,
and it is only when they are first assigned that the program knows what sort
of object they are. The advantage is being able to deal with multiple
versions, the disadvantage is that you lose type-checking when writing the
code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top