[英]Unable to late bind excel from Access VBA - Workbooks.Open doesn't work
正如芬頓先生所建議的那樣,我想開始刪除我對后期綁定的引用。 但是,我嘗試晚綁定 Excel,就像我查找的所有內容一樣,並且在添加“Microsoft Visual Basic for Applications Extensibility 5.3”之前無法修復它。
我目前的參考資料是:
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.
使用 Windows 10、Access 2013 Runtime 和 accdb 進行了測試。
這是 Function 的頂部,其中包含聲明和錯誤部分:
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
問題:
1) 我錯過了 Open 的常數嗎? 關鍵是要刪除引用,但要完成這項工作,我必須添加一個。 :(
2)接下來我應該嘗試刪除哪些參考? 老實說,在最后一個之后我有點害怕,但我希望程序盡可能穩定。
先感謝您!
這是我從我的個人代碼 stuffz 中復制/粘貼的內容。 它是用於 excel 的通用后期綁定模板。 您可以通過設置具有所有 excle 常量的模塊來提高后期綁定效率。 很漂亮的東西。
Option Compare Database
Public Function getFile() As String
Dim f As Object
Dim i As Long
Set f = Application.FileDialog(3)
f.AllowMultiSelect = False
If f.Show Then
If f.SelectedItems.Count > 0 Then
getFile = f.SelectedItems(1)
End If
End If
End Function
Public Function ConvUNC(filePath As String) As String
Dim fileR As String
fileR = Replace(filePath, "C:", "C$")
ConvUNC = "\\" & Environ$("computername") & "\" & fileR
End Function
Public Sub UploadExcel()
On Error GoTo UpLoadExcel_Err
Dim fileP As String, FileDir As String
Dim oXL As Object, sheet As Object
Dim bringOver As Variant
fileP = getFile
If fileP = "" Then GoTo CleanUp
FileDir = Left(fileP, InStrRev(fileP, "\") - 1) & "\"
Set oXL = CreateObject("Excel.Application")
With oXL
.WorkBooks.Open FileName:=FileDir & Dir$(fileP)
Open NewTextFile For Output As #2
For Each sheet In .Worksheets
bringOver = .ActiveSheet.UsedRange
NextSheet:
Erase bringOver
Next sheet
End With
CleanUp:
On Error Resume Next
DoEvents
oXL.Quit
oXL.Application.Quit
Erase bringOver
Exit Sub
UpLoadExcel_Err:
MsgBox "An error has occured. " & " " & Err.Number & " " & Err.Description & " "
GoTo CleanUp
Resume
End Sub
BigBen 似乎已經找到了答案。 我剛換了工作簿。 我還按照 Krish 的建議將 CreateObject 移出循環,因為它當然應該在循環之外::)
設置 wb =.Workbooks.Open(FileToImport)
.DisplayAlerts = -1
.Visible = -1
.Windows(1).Visible = -1
Set xlsheet1 = wb.Worksheets(1)
Set rng = xlsheet1.UsedRange
道格,我認為你是對的,我應該制作一個可重復使用的 function。 我還沒有完成 ConvUNC .. 可能應該。 我不能按原樣使用您的代碼,因為幾個 excel 使用多個 select,但我想我可以修改它。
謝謝大家的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.