我正在使用MSO Access 2013,并编写一些代码来打开文件对话框,供用户选择Excel文件以将信息导入数据库中的表。我得到运行时错误,并不完全确定原因。有人会帮助解决这种情况吗?代码如下
Dim xlApp As Excel.Application
Dim xlWrksht As Excel.Worksheet
Dim xl As Excel.Application
Dim xlsht As Excel.Worksheet
Dim xlWrkBk As Excel.Workbook
If MsgBox("This will create a new job by importing a Start Up worksheet.
Do you want to continue?", vbYesNo + vbQuestion, "Well Startup - Import
File") = vbYes Then
'prompt user for file
Const msoFileDialogFilePicker As Long = 3
Dim objDialog As Object
Dim fPath As String
Set objDialog = Application.FileDialog(msoFileDialogFilePicker)
With objDialog
.AllowMultiSelect = False
.Show
If .SelectedItems.Count = 0 Then
MsgBox "No file selected."
Exit Sub
Else
fPath = .InitialFileName
End If
End With
'open db and store data in tables
Set myRec = CurrentDb.openrecordset("WellProject")
Set xlApp = CreateObject("Excel.Application")
Set xlWrksht = xlApp.Open(fPath).Worksheets("1") '<----where I get the error
Set xl = CreateObject("Excel.Application")
Set xlWrkBk = GetObject(fPath)
Set xlsht = xlWrkBk.Worksheets(1)
'1st table
myRec.AddNew
myRec.Fields("Job") = xlWrksht.Cells(2, "F")
myRec.Fields("spud_date") = xlWrksht.Cells(10, "B")
myRec.Update
Else: Exit Sub
End If
目录路径存储在变量fPath中。这很好用。我相信这个问题会出现在Excel文件的某个地方,但不确定。有任何想法吗?谢谢大家!