MS Office 14.0 object library references in VB 6 program

前端 未结 1 976
北荒
北荒 2021-01-14 21:50

My VB 6 program code is referring to 14.0 object library, it is compiled and executed..working fine with the new code (accessing 14.0 libraries).

In my development

相关标签:
1条回答
  • 2021-01-14 22:07

    The problem is that you are using Early Binding. You have to use Late Binding.

    In Early Binding, you set a reference to the Excel Object xx.xx Library where as in late Binding you do not.

    Here is an example of both

    Early Binding

    Sub EarlyBindingEx()
        Dim oXLApp As Excel.Application
        Dim oXLWb As Excel.Workbook
        Dim oXLWs As Excel.Worksheet
    
        '~~> Establish an EXCEL application object
        On Error Resume Next
        Set oXLApp = GetObject(, "Excel.Application")
    
        '~~> If not found then create new instance
        If Err.Number <> 0 Then
            Set oXLApp = New Excel.Application
        End If
        Err.Clear
        On Error GoTo 0
    
        '~~> Show Excel
        oXLApp.Visible = True
    
        '~~> Open files
        Set oXLWb = oXLApp.Workbooks.Open("C:\Sample.xlsx")
    
        '~~> Set the relevant worksheet
        Set oXLWs = oXLWb.Sheets(1)
    
        '
        '~~> Rest of your code
        '
    End Sub
    

    Late Binding

    Sub LateBindingEx()
        Dim oXLApp As Object
        Dim oXLWb As Object, oXLWs As Object
    
        '~~> Establish an EXCEL application object
        On Error Resume Next
        Set oXLApp = GetObject(, "Excel.Application")
    
        '~~> If not found then create new instance
        If Err.Number <> 0 Then
            Set oXLApp = CreateObject("Excel.Application")
        End If
        Err.Clear
        On Error GoTo 0
    
        '~~> Show Excel
        oXLApp.Visible = True
    
        '~~> Open files
        Set oXLWb = oXLApp.Workbooks.Open("C:\Sample.xlsx")
    
        '~~> Set the relevant worksheet
        Set oXLWs = oXLWb.Sheets(1)
    
        '
        '~~> Rest of your code
        '
    End Sub
    

    HERE is an MSDN link regarding Early Binding Vs Late Binding.

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