Preventing Excel VBA compile errors due to users having an older version of MS Office (MS Outlook) installed?

后端 未结 1 1093
生来不讨喜
生来不讨喜 2020-12-08 16:25

If I have a spreadsheet where I\'ve referenced MS Outlook 14.0 Object Library from the VBA editor, and a user who only has MS Outlook 12.0 installed, then when that user ope

相关标签:
1条回答
  • 2020-12-08 17:00

    tbone, what you refer to as Strong Typing is called "Early Binding".

    Unfortunately one of the drawbacks with Early Binding is that if the end user doesn't have the same version as yours then you will get those errors.

    If you ask me, I prefer Late Binding (where you don't create references and use CreateObject to create an instance)

    An interesting read.

    Topic: Using early binding and late binding in Automation

    Link: http://support.microsoft.com/kb/245115

    My Suggestion

    Don't give up Early Binding if you like it because of intellisense. However before you distribute your application, change the code to Latebinding. There is not much of a difference in the way you code in Early Binding and Late Binding.

    Here is an example

    Early Binding

    '~~> Set reference to Excel Object Library
    Sub Sample()
        Dim oXLApp As Excel.Application
        Dim oXLBook As Excel.Workbook
        Dim oXLSheet As Excel.Worksheet
    
        '~~> Create a new instance of Excel
        Set oXLApp = New Excel.Application
        '~~> Add a new workbook
        Set oXLBook = oXLApp.Workbooks.Add
        Set oXLSheet = oXLBook.Worksheets(1)
    
        '
        '~~> Rest of the code
        '
    End Sub
    

    Late Binding

    '~~> Doesn't require a reference to Excel Object Library
    Sub Sample()
        Dim oXLApp As Object
        Dim oXLBook As Object
        Dim oXLSheet As Object
    
        '~~> Create a new instance of Excel
        Set oXLApp = CreateObject("Excel.Application")
        '~~> Add a new workbook
        Set oXLBook = oXLApp.Workbooks.Add
        Set oXLSheet = oXLBook.Worksheets(1)
        '
        '~~> Rest of the code
        '
    End Sub
    

    HTH

    Sid

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