首页 > 解决方案 > MS Access 2016 TransferSpreadsheet 导入错误 3274:不是预期的格式

问题描述

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

我能够在 13 个文件上运行此宏,但在第 13 个文件之后,每个剩余的文件都会抛出“运行时错误'3274':外部表不是预期的格式。” DoCmd.TransferSpreadsheet 行上的错误:

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

我尝试了其他帖子中的几个“修复”但没有成功:

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

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

谁能帮我理解这个宏发生了什么以及如何修复它?

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

标签: excelms-accessvbams-access-2016

解决方案


我通过更多的实验弄清楚了 -错误 3274 可能是由超出 Access 的单元格数据限制的文件引起的。 我相信是一个 Long Text 列影响了导入。

手动导入一些文件后,我在尝试手动导入时遇到了另一个错误 - “向导无法访问文件''中的信息。请检查文件是否存在并且格式正确。”

这导致我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

推荐阅读