Vba DoCmd.TransferSpreadsheet导入错误“;外部表格不是预期的格式";
嗨,我得到这个错误 “外部表不是预期的格式” …当我尝试将excel数据传输到access表时。我在文本文件中有一个描述字段。为了避免在250个字符的限制下进行截断,我将文本文件转换为EXCEL,然后将EXCEL传输到access数据库。但是我得到了这个错误。。。。我的机器上安装了excel 2010和2016。提前谢谢大家Vba DoCmd.TransferSpreadsheet导入错误“;外部表格不是预期的格式";,vba,excel,ms-access,Vba,Excel,Ms Access,嗨,我得到这个错误 “外部表不是预期的格式” …当我尝试将excel数据传输到access表时。我在文本文件中有一个描述字段。为了避免在250个字符的限制下进行截断,我将文本文件转换为EXCEL,然后将EXCEL传输到access数据库。但是我得到了这个错误。。。。我的机器上安装了excel 2010和2016。提前谢谢大家 Sub ImportPPDE_v2() Dim fDialog As Office.FileDialog Dim strNewPath As String On Err
Sub ImportPPDE_v2()
Dim fDialog As Office.FileDialog
Dim strNewPath As String
On Error GoTo GameOver
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
'Set up the fDialog variable
'Set the Title, add a filter for text files, and set the initial filepath we want to look at, we are defaulting to the Clarity Extract folder
fDialog.Filters.Clear
fDialog.Title = "Select Latest Project Portfolio Data Extract File"
fDialog.Filters.Add "*.txt", "*.txt"
fDialog.InitialFileName = "G:\Clarity EPPM Extracts\"
fDialog.AllowMultiSelect = False
fDialog.Show
'turn off warnings, we don't need to see this
DoCmd.SetWarnings False
'Check to make sure a file has been selected, and if so that the Project Portfolio Data Extract file has been selected
If fDialog.SelectedItems.Count = 0 Then
MsgBox "No File has been selected. Load actions have been cancelled.", , "No File Selected"
GoTo GameOver
ElseIf InStr(1, fDialog.SelectedItems(1), "Project Portfolio Data extract_") = 0 Then
MsgBox "The file selected appears to be incorrect. It should be the Data Extract file. Load actions have been cancelled.", , "ERROR OCCURRED IN DATA LOAD"
GoTo GameOver
End If
'First delete everything currently in the table
DoCmd.RunSQL "Delete * from tbl_Project_Portfolio_Data_Load"
'DoCmd.TransferText , "Spec_PPDE", "tbl_Project_Portfolio_Data_Load", fDialog.SelectedItems(1)
'First isolate the file name from the selected path, then change the file extension to .xls
strNewPath = Right(fDialog.SelectedItems(1), Len(fDialog.SelectedItems(1)) - InStrRev(fDialog.SelectedItems(1), "\"))
strNewPath = "C:\" & Left(strNewPath, Len(strNewPath) - 4) & ".xlsx"
'Copy the Portfolio data extract file to the user's C: drive as a .xls file
Call SaveAsFile(CStr(fDialog.SelectedItems(1)), strNewPath)
'Now import the new data as selected above
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "tbl_Project_Portfolio_Data_Load", strNewPath, True
'Now update the Load_Date to be today
DoCmd.RunSQL "Update tbl_Project_Portfolio_Data_Load set [Load Date] = #" & Date & "# Where [Load Date] IS NULL"
'Let the user know the process finished successfully
MsgBox "Project Portfolio Data Extract Data has been uploaded", , "Victory!"
GameOver:
'Turn our warnings back on
DoCmd.SetWarnings True
'Set this back to nothing
Set fDialog = Nothing
'Check if an error occurred that would prevent the expected data from being loaded
If Err.Number <> 0 Then
MsgBox Err.Description
End If
End Sub
如果您确实无法在不截断的情况下链接到文本文件(我可以),并且由于simple Open无法工作,请将文本文件导入Excel,然后另存为Excel工作簿。我使用Excel宏记录器生成了一些代码,并适应了Access过程
Sub TextToExcel1(currpath As String, newpath As String)
Dim xlx As Excel.Application, xlw As Excel.Workbook, xls As Excel.Worksheet
Dim blnEXCEL As Boolean
If Dir(newpath) <> "" Then Kill newPath
blnEXCEL = False
On Error Resume Next
Set xlx = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set xlx = CreateObject("Excel.Application")
blnEXCEL = True
End If
Err.Clear
xlx.Visible = False
Set xlw = xlx.Workbooks.Add
Set xls = xlw.Worksheets("Sheet1")
With xls.QueryTables.Add("TEXT;" & currPath, Range("$A$1"))
.Name = "Test"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
xlw.SaveAs newPath
Set xls = Nothing
xlw.Close False
Set xlw = Nothing
If blnEXCEL = True Then xlx.Quit
Set xlx = Nothing
End Sub
子文本ToExcel1(currpath作为字符串,newpath作为字符串)
Dim xlx作为Excel.Application,xlw作为Excel.工作簿,xls作为Excel.Workbench
Dim blnEXCEL作为布尔值
如果Dir(newpath)“,则杀死newpath
blnEXCEL=False
出错时继续下一步
Set xlx=GetObject(,“Excel.Application”)
如果错误号为0,则
Set xlx=CreateObject(“Excel.Application”)
blnEXCEL=True
如果结束
呃,明白了
xlx.Visible=False
设置xlw=xlx.Workbooks.Add
设置xls=xlw。工作表(“表1”)
使用xls.QueryTables.Add(“TEXT;”和currPath,范围(“$A$1”))
.Name=“测试”
.FieldNames=True
.rowNumber=False
.FillAdjacentFormulas=False
.PreserveFormatting=True
.refreshinfoleopen=False
.RefreshStyle=xlInsertDeleteCells
.SavePassword=False
.SaveData=True
.AdjustColumnWidth=True
.RefreshPeriod=0
.TextFilePromptOnRefresh=False
.TextFilePlatform=437
.TextFileStartRow=1
.TextFileParseType=xlDelimited
.TextFileTextQualifier=xlTextQualifierDoubleQuote
.textfileconsutivedelimiter=True
.TextFileTabDelimiter=False
.TextFileSemicolonDelimiter=False
.textfilecommadelimitor=False
.TextFileSpaceDelimiter=True
.TextFileColumnDataTypes=数组(1,1)
.TextFileTrailingMinusNumbers=True
.Refresh BackgroundQuery:=False
以
xlw.SaveAs新路径
设置xls=无
xlw.关闭错误
设置xlw=Nothing
如果blnEXCEL=True,则xlx.Quit
设置xlx=无
端接头
然后这个版本:
Sub TextToExcel2(currpath As String, newpath As String)
Dim xlx As Excel.Application
Dim blnEXCEL As Boolean
If Dir(newpath) <> "" Then Kill newPath
blnEXCEL = False
On Error Resume Next
Set xlx = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set xlx = CreateObject("Excel.Application")
blnEXCEL = True
End If
Err.Clear
xlx.Visible = False
xlx.Workbooks.OpenText filename:=currPath, Origin:=437, StartRow:=1, _
DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False,
Comma:=False, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), _
Array(2, 1)), TrailingMinusNumbers:=True
xlx.ActiveWorkbook.SaveAs filename:=newPath, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
If blnEXCEL = True Then xlx.Quit
Set xlx = Nothing
End Sub
子文本ToExcel2(currpath作为字符串,newpath作为字符串)
Dim-xlx作为Excel.Application
Dim blnEXCEL作为布尔值
如果Dir(newpath)“,则杀死newpath
blnEXCEL=False
出错时继续下一步
Set xlx=GetObject(,“Excel.Application”)
如果错误号为0,则
Set xlx=CreateObject(“Excel.Application”)
blnEXCEL=True
如果结束
呃,明白了
xlx.Visible=False
xlx.Workbooks.OpenText文件名:=currPath,原点:=437,StartRow:=1_
数据类型:=xlDelimited,文本限定符:=xlDoubleQuote_
连续delimiter:=True,制表符:=False,分号:=False,
逗号:=假,空格:=真,其他:=假,字段信息:=数组(数组(1,1)_
数组(2,1)),trailingMinusNumber:=True
xlx.active工作簿.SaveAs文件名:=newPath,文件格式:=xlOpenXMLWorkbook,CreateBackup:=False
如果blnEXCEL=True,则xlx.Quit
设置xlx=无
端接头
甚至:
Sub TextToExcel3(currpath As String, newpath As String)
Dim xlx As Excel.Application
Dim blnEXCEL As Boolean
If Dir(newpath) <> "" Then Kill newPath
blnEXCEL = False
On Error Resume Next
Set xlx = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set xlx = CreateObject("Excel.Application")
blnEXCEL = True
End If
Err.Clear
xlx.Workbooks.Open (currpath)
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
xlx.Visible = False
xlx.ActiveWorkbook.SaveAs filename:=newPath, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
If blnEXCEL = True Then xlx.Quit
Set xlx = Nothing
End Sub
子文本ToExcel3(currpath作为字符串,newpath作为字符串)
Dim-xlx作为Excel.Application
Dim blnEXCEL作为布尔值
如果Dir(newpath)“,则杀死newpath
blnEXCEL=False
出错时继续下一步
Set xlx=GetObject(,“Excel.Application”)
如果错误号为0,则
Set xlx=CreateObject(“Excel.Application”)
blnEXCEL=True
如果结束
呃,明白了
xlx.Workbooks.Open(currpath)
列(“A:A”)。选择
Selection.TextToColumns Destination:=范围(“A1”),数据类型:=xlDelimited_
TextQualifier:=xlDoubleQuote,continuedDelimiter:=True,Tab:=False_
分号:=False,逗号:=False,空格:=True,其他:=False,字段信息_
:=数组(数组(1,1),数组(2,1)),TrailingMinusNumbers:=True
xlx.Visible=False
xlx.active工作簿.SaveAs文件名:=newPath,文件格式:=xlOpenXMLWorkbook,CreateBackup:=False
如果blnEXCEL=True,则xlx.Quit
设置xlx=无
端接头
我在让版本1一致地复制时遇到问题。版本2和版本3看起来都很可靠。Brad可以为您提供另一种导入Excel文件的方法,这使您能够更好地控制导入Excel文件的方式。谢谢,但我不知道如何使用它,如果您能帮我修改我的Excel文件,我将不胜感激。对不起,但你没有提供必要的信息让我这么做。我需要知道Access表中所有列的名称。请添加
SaveAsFile()
@Erik von Asmuth;列名称为:尺寸标识、项目标识、项目名称、项目代码、项目描述
Sub TextToExcel3(currpath As String, newpath As String)
Dim xlx As Excel.Application
Dim blnEXCEL As Boolean
If Dir(newpath) <> "" Then Kill newPath
blnEXCEL = False
On Error Resume Next
Set xlx = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set xlx = CreateObject("Excel.Application")
blnEXCEL = True
End If
Err.Clear
xlx.Workbooks.Open (currpath)
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
xlx.Visible = False
xlx.ActiveWorkbook.SaveAs filename:=newPath, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
If blnEXCEL = True Then xlx.Quit
Set xlx = Nothing
End Sub