How to do late binding in VBA?

前端 未结 3 1484
你的背包
你的背包 2020-11-30 12:21

I have this little function that achieves the creation of an email via VBA,
It gets the data from another function that works together with an Excel file.

The pr

相关标签:
3条回答
  • 2020-11-30 12:33

    In that situation I generally define the constant globally in a module like this, that way you preserve the descriptive value of the variable.

    Public Const olFormatHTML = 2

    0 讨论(0)
  • 2020-11-30 12:37

    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.

    0 讨论(0)
  • 2020-11-30 12:41

    As Callum pointed out, late binding involves changing your application reference to an object and not setting a reference to the library.

    Without a reference Excel doesn't know anything about Outlook until runtime. This also means that not only will intellisense not work, the constant names for values in Outlook won't work either.

    e.g. In Outlooks Immediate window if you type Application.CreateItem( you'll get a whole load of item types pop up to choose from. olContactItem for instance.

    Excel hasn't a clue what olContactItem means - it's an Outlook constant that only Outlook or an application with a reference to Outlook understands.

    In Outlooks immediate window type ?olContactItem and it will return 2. That's the number you need to use instead of the constant name.

    So your code changes from
    Application.CreateItem(olContactItem) to olApp.CreateItem(2)

    You need to do this throughout your code.

    0 讨论(0)
提交回复
热议问题