【问题标题】:MS Access 2016 TransferSpreadsheet Import Error 3274: Not in the expected formatMS Access 2016 TransferSpreadsheet 导入错误 3274:不是预期的格式
【发布时间】:2019-08-03 05:00:51
【问题描述】:

我正在尝试在 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

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

  • 将 SpreadsheetType 更改为任何其他值(包括空白、数字和所有版本的 acSpreadsheetTypeExcel)
  • 打开文件并在打开时运行宏
  • 打开文件并将其重新保存为 .xls
  • 使用其他名称打开文件并将其另存为 .xls
  • 在尝试 TransferSpreadsheet 之前将 Attr 设置为 vbNormal

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

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

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

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

【问题讨论】:

  • 工作表是否包含隐藏列?除了简单的值表之外,其中是否还有其他内容或功能?
  • 如果在运行例程之前删除导入的 13,例程是否会再导入 13 并失败?如果不是,那么第 14 个文件中肯定有与第一个文件不同的内容。检查电子表格的使用范围是否过多(控制端应该带您到工作表的最后一个填充单元格)
  • @AndyG 没有隐藏列,它是从现有数据库导出的基本原始数据。所有字段的数据类型都是短文本(一个长文本列除外)。
  • @HarassedDad 删除前 13 个文件后,其他文件都不会运行。每个文件包含大约 3500 行,给或取。初始13导入成功后,表中有48225条记录。

标签: excel ms-access vba ms-access-2016


【解决方案1】:

我通过更多的实验发现了它 - 错误 3274 可能是由超出 Access 的单元格数据限制的文件引起的。 我相信是一个长文本列阻碍了导入。

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

这让我找到了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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多