首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >MS Access 2016 TransferSpreadsheet导入错误3274:格式不正确

MS Access 2016 TransferSpreadsheet导入错误3274:格式不正确
EN

Stack Overflow用户
提问于 2019-03-13 00:23:21
回答 1查看 1.4K关注 0票数 0

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

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

代码语言:javascript
复制
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

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

在尝试TransferSpreadsheet之前,

  • 将SpreadsheetType更改为任何其他值(包括空白、数字和所有版本的文件)并运行宏,同时打开
  • 打开文件并将其重新另存为.xls
  • 打开文件,然后使用不同的名称将文件另存为.xls
  • SetAttr to vbNormal

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

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

有没有人能帮我弄明白这个宏是怎么回事,以及如何修复它?

编辑添加:我在我的表中添加了一个索引以防止重复条目,这显著减少了导入记录的数量,但它仍然停止处理完全相同的文件。在宏无法处理的文件之一上手动运行导入向导可以很好地工作,但我有大量文件要导入,因此不希望逐个手动导入它们。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-13 06:30:23

我通过更多的实验发现了这一点-错误3274可能是由于文件超出了Access的单元格数据限制而导致的。我相信这是一个长文本专栏,借用了重要的。

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

这让我想到了https://support.microsoft.com/en-us/help/2836058/access-errors-during-import-export-to-excel-xls,它建议我尝试.xlsx格式……修复了手动导入的问题。

由于这是有效的,我添加了一些代码到我的宏中,在导入之前将文件转换为.xlsx格式,它修复了它,并在所有剩余的文件上运行得很好。

如果有人感兴趣,下面是结果:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55126283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档