Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
711 views
in Technique[技术] by (71.8m points)

excel - How to do late binding in VBA?

I have a function that creates an email via VBA.

I made this through Excel 2016. When some of my colleagues try to use it there an error of missing references (Outlook Library 16.0).

I looked in the internet for solutions and found the best is Late Binding. I have read about it but I don't understand how to make it work in the following example code.

Sub EscalateCase(what_address As String, subject_line As String, email_body As String)
    
    Dim olApp As Outlook.Application
    Set olApp = CreateObject("Outlook.Application")
    
    Dim olMail As Outlook.MailItem
    Set olMail = olApp.CreateItem(olMailItem)
        
    olMail.To = what_address
    olMail.Subject = subject_line
    olMail.BodyFormat = olFormatHTML
    olMail.HTMLBody = email_body
    olMail.Send
        
End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is early binding:

Dim olApp As Outlook.Application
Set olApp = New Outlook.Application

And this is late binding:

Dim olApp As Object
Set olApp = CreateObject("Outlook.Application")

Late binding does not require a reference to Outlook Library 16.0 whereas early binding does. However, note that late binding is a bit slower and you won't get intellisense for that object.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...