MS Access 2016 TransferSpreadsheet 导入错误 3274:不是预期的格式

MS Access 2016 TransferSpreadsheet Import Error 3274: Not in the expected format

我正在尝试在 MS Access 2016 中使用 VBA 设置一个宏,以将多个 .xls 文件导入我的 table。

我能够在 13 个文件上 运行 这个宏,但是在第 13 个文件之后,每个剩余的文件都会在 DoCmd.TransferSpreadsheet 行上引发 "Run-time error '3274': External table is not in the expected format." 错误:

Function ImportAllExcel()
Dim myfile
Dim mypath
Dim finpath

mypath = REDACTED
finpath = REDACTED

ChDir (mypath)
myfile = Dir(mypath)

Do While myfile <> ""
  If myfile Like "*.xls" Then
    DoCmd.TransferSpreadsheet acImport, 8, _
        "Table Name", mypath & myfile, True

    ' Move imported files to Imported folder
    FileCopy mypath & "/" & myfile, finpath & "/" & myfile
    SetAttr mypath & "/" & myfile, vbNormal
    Kill mypath & "/" & myfile
  End If
  myfile = Dir()
Loop

MsgBox "Import complete."

End Function

我尝试了几次 "fixes" 其他帖子但没有成功:

None 的列名称包含任何空格(尽管其中一个包含下划线并且在成功 运行 的列中根本不导入该列,但这是一个单独的问题 - 我手动将列添加到 Access table 以防万一,但它没有数据条目)。

所有 .xls 文件都来自相同的来源、相同的格式、相同的列名和数据类型 - 它们是来自机器源的自动每日报告。前 13 个文件导入得很好,我发现 运行 和其余文件之间没有明显区别。

任何人都可以帮助我了解这个宏发生了什么以及如何修复它吗?

编辑添加: 我在 table 中添加了一个索引以防止重复条目,这显着减少了导入记录的数量,但它仍然停止工作完全相同的文件。手动 运行 在宏不会处理的文件之一上启用导入向导工作得很好,但我有大量文件要导入,并且希望避免一个一个地手动导入它们。

我通过更多的实验弄明白了 - 错误 3274 可能是由超过 Access 的单元格数据限制的文件引起的。我相信是一个长文本栏让进口。

手动导入一些文件后,我 运行 在尝试手动导入时遇到另一个错误 - "The wizard is unable to access information in the file ''. Please check that the file exists and is in the correct format."

这导致我 https://support.microsoft.com/en-us/help/2836058/access-errors-during-import-export-to-excel-xls 建议尝试 .xlsx 格式...修复了手动导入。

因为它奏效了,我在我的宏中添加了一些代码以在导入之前将文件转换为 .xlsx 格式,并且它修复了它并且 运行 在所有剩余的文件上都很漂亮。

如果有人感兴趣,这里是结果:

Function ImportAllExcel()

Dim myfile
Dim mypath
Dim endpath
Dim oExcel As Object
Dim oExcelWb As Object
Dim bExcelOpened As Boolean

' Folders to import from/to
mypath = REDACTED
endpath = REDACTED

ChDir (mypath)
myfile = Dir(mypath)

' Suppress confirmation of failed import rows caused by indexing
DoCmd.SetWarnings False

Do While myfile <> ""
  If myfile Like "*.xls" Then

    ' Convert XLS file to XLSX to prevent errors
    On Error Resume Next
        ' Use existing instance of Excel if already open
        Set oExcel = GetObject(, "Excel.Application") 
        If Err.Number <> 0 Then
            'Could not get instance of Excel, so create a new one
            Err.Clear
            Set oExcel = CreateObject("Excel.Application")
            bExcelOpened = False
        Else
            bExcelOpened = True
       End If
    On Error GoTo -1

    oExcel.Visible = False
    Set oExcelWb = oExcel.Workbooks.Open(mypath & myfile)
    oExcelWb.SaveAs Replace(mypath & myfile, ".xls", ".xlsx"), 51, , , , False
    oExcelWb.Close False
    If bExcelOpened = True Then oExcel.Quit

    ' Delete the converted file & select the new one
    Kill mypath & "/" & myfile
    myfile = myfile & "x"

    ' Import the file
    On Error GoTo SkipFile
    SetAttr mypath & "/" & myfile, vbNormal
    DoCmd.TransferSpreadsheet acImport, 8, "TABLE NAME", mypath & myfile, True

    ' Move imported files to Imported folder
    FileCopy mypath & "/" & myfile, endpath & "/" & myfile
    Kill mypath & "/" & myfile

SkipFile:
    ' Clear error handling
    On Error GoTo -1
  End If
  myfile = Dir()
Loop

DoCmd.SetWarnings True

MsgBox "Import complete."

End Function