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:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
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:
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,213
Members
448,554
Latest member
Gleisner2

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
Back
Top