问题描述
使用 DoCmd.TransferSpreadsheet acImport 目前对我不起作用。我正在尝试导入 .xlsx 文件,并且我正在使用通配符 (*) 来完成文件名。当我使用 MsgBox 时,FullPath 是正确的,但是当 TransferSpreadsheet 运行时,它说在 C:\Users\Me\Documents(默认位置)中找不到该文件。
Dim FPath As String
Dim FName As String
Dim FullPath As String
FPath = CurrentProject.Path & "\Data\"
FName = "DataTable"
FullPath = Dir(FPath & FName & "*.xlsx")
If FullPath <> "" Then
DoCmd.TransferSpreadsheet acImport,10,TableName,FullPath,1
Else: MsgBox "Error - file not found"
End If
为什么不看我指定的地方?这个错误是不是不正确,它表示其他什么?
解决方法
Dir()
仅返回文件 name 或不返回任何内容(空字符串)。它不会返回完整路径。
Dim FPath As String
Dim FileName As String
FPath = CurrentProject.Path & "\Data\"
FileName = Dir(FPath & "DataTable*.xlsx")
If FileName <> "" Then
DoCmd.TransferSpreadsheet acImport,10,TableName,FPath & FileName,1
Else
MsgBox "Error - file not found"
End If