excel - 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
我尝试了其他帖子中的几个“修复”但没有成功:
- 将 SpreadsheetType 更改为任何其他值(包括空白、数字和所有版本的 acSpreadsheetTypeExcel)
- 打开文件并在打开时运行宏
- 打开文件并将其重新保存为 .xls
- 使用不同的名称打开文件并将其另存为 .xls
- 在尝试 TransferSpreadsheet 之前将 Attr 设置为 vbNormal
没有一个列名包含任何空格(尽管一个包含下划线并且根本不导入已成功运行的列,但这是一个单独的问题 - 我手动将该列添加到 Access 表中以防万一,但它是没有数据条目)。
所有 .xls 文件都来自相同的来源、相同的格式、相同的列名和数据类型——它们是来自机器源的自动每日报告。前 13 个文件导入得很好,我发现运行的文件和剩余的文件之间没有明显的区别。
谁能帮我理解这个宏发生了什么以及如何修复它?
编辑添加:我在表中添加了一个索引以防止重复条目,这大大减少了导入记录的数量,但它仍然停止在完全相同的文件上工作。在宏将不会处理的文件之一上手动运行导入向导可以正常工作,但是我有大量文件要导入,并且希望避免一个接一个地手动导入它们。
解决方案
我通过更多的实验弄清楚了 -错误 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
推荐阅读
- angular-material - 如何为 Angular Material 拖放 v10 提供混合方向
- android-pay - 如何在 Google Pass 活动门票徽标中使用 base64 字符串
- ios - iOS:如何为 3 个具有相同持续时间和重复的序列设置动画?
- python - 类似于点积的自定义 keras 层
- wpf - 在 xaml 中使用前导空格为标签格式化双精度
- colors - 具有多种颜色的 ifc 对象
- visual-studio-code - 使 VSCode 修剪仅包含空格的行,但不修剪带有尾随空格的文本行
- sql - 即使使用 group by 也会返回重复的行
- apple-push-notifications - Java 1.6 支持向 Apple 的新 HTTP/2 规范发送通知负载
- python - 如何在此裁剪图像中获取 x,y,w,h 的每个值?