如何在 VBA 中进行后期绑定? [英] How to do late binding in VBA?

查看:31
本文介绍了如何在 VBA 中进行后期绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过 VBA 创建电子邮件的函数.

I have a function that creates an email via VBA.

我是通过 Excel 2016 完成的.当我的一些同事尝试使用它时,会出现缺少引用的错误(Outlook Library 16.0).

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

推荐答案

这是早期绑定:

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

这是后期绑定:

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

后期绑定不需要对 Outlook Library 16.0 的引用,而早期绑定需要.但是,请注意后期绑定有点慢,并且您不会获得该对象的智能感知.

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.

这篇关于如何在 VBA 中进行后期绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆