Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88868

Unable to late bind excel from Access VBA - Workbooks.Open doesn't work

$
0
0

I would like to start removing my references to late bind as Mr. Fenton suggests. However, I tried to late bind Excel, exactly as everything I have looked up, and could not fix it until I added "Microsoft Visual Basic for Applications Extensibility 5.3".

My references currently are:

Visual Basic for Applications
Microsoft Access 15.0 Object Library
Microsoft Data Access Components Installed Version
Microsoft ActiveX Data Objects 6.1 Library
Microsoft DAO 3.6 Object Library
Microsoft Windows Common Controls 6.0 (SP6)
Microsoft Scripting Runtime
Microsoft XML,v6.0
Microsoft Visual Basic for Applications Extensibility 5.3.

Testing was done with Windows 10, Access 2013 Runtime and accdb.

Here is the top part of the Function with the declarations and section with the error:

On Error GoTo errHandle:

Dim FileToImport As Variant
Dim FilesLoaded As String
Dim csvStr As String
DoCmd.Hourglass False
Dim Loc As Integer

Loc = Forms!StartPage.LocationID

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim ofile As Object
Dim fdialog As Object
Dim rs As ADODB.Recordset
Dim uid As String
uid = Forms!AppLoginFrm!FullName
Dim stp As String
Dim thestr As String
Dim inboundrs As ADODB.Recordset
Dim ChkInbound As String
Dim repprg As Long
Dim ExcelApp As Object
Dim fname As String
Dim rng As Object
Dim wb As Object
Dim xlsheet1 As Object
Dim skiphead As Integer
stp = format(Now, "yyyy-mm-dd hh:nn:ss")
Dim tmpfile As String

tmpfile = TempPath()

tmpfile = tmpfile & "tmpLoad.txt"

skiphead = 2
Set fdialog = Application.FileDialog(3)
fdialog.Filters.Clear
fdialog.Filters.Add "Excel Files", "*.xls,*.xlsx"

    fdialog.AllowMultiSelect = True
    If fdialog.Show = 0 Then
    Exit Function
    End If


Set ofile = fso.CreateTextFile(tmpfile, True, False)


     ofile.WriteLine """Location"""& ","& """YardTrack"""& ","& """Dir"""& ","& """Seq"""...

        For Each FileToImport In fdialog.SelectedItems

    Set ExcelApp = CreateObject("Excel.Application")

    With ExcelApp
    .Workbooks.Open FileToImport

        .DisplayAlerts = -1
        .Visible = -1
        .Windows(1).Visible = -1

        Set xlsheet1 = .Worksheets(1)
        Set rng = xlsheet1.UsedRange

            If xlsheet1.cells(1, 1).Value = "TOT-CARS" Then
                skiphead = 3
            End If
        For i = skiphead To rng.rows.count
            csvStr = csvStr & Chr(34) & rng(i, 1) & Chr(34) & ","& Chr(34) & rng(i, 2) & Chr...

            ofile.Write csvStr
            csvStr = ""
            Next

              ofile.Write Chr(34) & csvStr & Chr(34) & vbCrLf
            ExcelApp.Workbooks.Close
            Set rng = Nothing
            Set xlsheet1 = Nothing
      End With
            FilesLoaded = FilesLoaded & vbCrLf & fileName(FileToImport)

        Next FileToImport


ExcelApp.Quit
ofile.Close
Set fdialog = Nothing

Questions:

1) Did I miss a constant for Open?? The point was to remove references, but to make this work I had to add one. :(

2) What reference should I try to remove next? Honestly I'm a little scared after the last one, but I want the program as stable as possible.

Thank you in advance!


Viewing all articles
Browse latest Browse all 88868