Convert early to late binding

confusion123

Active Member
Joined
Jul 27, 2014
Messages
400
I am trying to convert code from early to late binding:

Rich (BB code):
    '*****

    ' This I early

    Dim objOutlook As Outlook.Application
    Set objOutlook = New Outlook.Application
    
    Dim objMail As Outlook.MailItem
    
    '***** 

    ' This is late
   
    'Dim objOutlook As Object
    'Set objOutlook = CreateObject("Outlook.Application")
    
    'Dim objMail As Object
    
    'Const olMailItem As Long = 0
    
    '*****

    Set objMail = objOutlook.CreateItem(olMailItem)
    
    With objMail
    
        .Subject = "E-Mail subject"
                         
With wksLists
            
            Call Create_Picture(wks:=wksLists, _
                                MyRng:=Range(.Cells(1, 1), .Cells(6, 3)), _
                                TempName:="AnyName")
            
        End With
        
        TempFilePath = Environ$("Temp") & ""
        
        .Attachments.Add Source:=TempFilePath & "AnyName.jpg", _
                         Type:=olByValue, _
                         Position:=0

   



the problem is when I uncomment the lines of code to make it late binding, I don't know what to change this line to:

Rich (BB code):
        .Attachments.Add Source:=TempFilePath & "AnyName.jpg", _
                         Type:=olByValue, _
                         Position:=0

in particular, the property olByValue

Can someone please help?

Thanks
 
Last edited:

Some videos you may like

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Derek Brown

Well-known Member
Joined
Dec 26, 2005
Messages
2,390
In Outlook VBE Editor, using the Immediate Window you can use the following:
Code:
? olByValue
That will give you a value of '1' for the eNum.
You can then either use that in the code or, alternatively, declare the value at the beginning of your procedure so that you can still use 'olByValue' as a named variable:
Code:
Const olByValue As Integer = 1
 
Last edited:

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,624
With the code using early binding (reference to Outlook Object Library), the Immediate Window command displays the value in Excel VBA as well, and in the Object Browser (F2) searching for olByValue shows its value and related enums.
 

confusion123

Active Member
Joined
Jul 27, 2014
Messages
400

ADVERTISEMENT

Unfortunately I added the line you suggested:

Code:
Const olByValue As Integer = 1
    
    Set objMail = objOutlook.CreateItem(olMailItem)
    
    With objMail
    
        .Subject = "E-Mail subject"
        .HTMLBody = "<span LANG=EN>" & _
                    "<p class=style2><span LANG=EN><font FACE=Calibri SIZE=3>" & _
                    "E-Mail body<br>"
        
        With wksLists
            
            Call Create_Picture(wks:=wksLists, _
                                MyRng:=Range(.Cells(1, 1), .Cells(6, 3)), _
                                TempName:="AnyName")
            
        End With
        
        TempFilePath = Environ$("Temp") & "\"
        
        .Attachments.Add Source:=TempFilePath & "AnyName.jpg", _
                         Type:=olByValue, _
                         Position:=0

but got an error message

Code:
object doesn't support named arguments.
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,624
As it says, you can't use named arguments (Source:=, Type:=, etc.) with late binding. Instead, you have to supply the arguments in their expected positions. Arguments inside square brackets [] are optional.

Code:
        '.Attachments.Add Source, [Type], [Position], [DisplayName]
        .Attachments.Add TempFilePath & "AnyName.jpg", olByValue, 0
 

Watch MrExcel Video

Forum statistics

Threads
1,123,279
Messages
5,600,696
Members
414,400
Latest member
Damocles2021

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top