因此,我多次使用doCmd.TransferText
来使用保存的文本导入规范,因为您可以轻松保存从Application.FileDialog(msoFileDialogFilePicker)
返回的文件路径,以查找要使用保存的规范导入的文件。
但是我很难找到一种方法来对excel文件做同样的事情,保存excel导入规范很简单,但使用DoCmd.TransferSpreadSheet
方法无法使用保存的导入,以及使用doCmd.RunSavedImportExport
没有选项指定文件路径。
除了使用不同的文件类型(例如.csv)之外,还有其他解决方法吗?
Access中的“Saved Imports”和“Saved Exports”存储在构成ImportExportSpecification
集合的CurrentProject.ImportExportSpecifications
对象中。保存的Excel导入的详细信息类似于以下XML,我通过手动导入Excel电子表格并勾选导入向导最后一页上的“保存导入步骤”复选框创建了该XML。
<?xml version="1.0" encoding="utf-8" ?>
<ImportExportSpecification Path = "C:\Users\Gord\Desktop\xlsxTest.xlsx" xmlns="urn:www.microsoft.com/office/access/imexspec">
<ImportExcel FirstRowHasNames="true" Destination="xlsxTest" Range="Sheet1$" >
<Columns PrimaryKey="ID">
<Column Name="Col1" FieldName="ID" Indexed="YESNODUPLICATES" SkipColumn="false" DataType="Long" />
<Column Name="Col2" FieldName="TextField" Indexed="NO" SkipColumn="false" DataType="Text" />
<Column Name="Col3" FieldName="DateField" Indexed="NO" SkipColumn="false" DataType="DateTime" />
</Columns>
</ImportExcel>
</ImportExportSpecification>
ImportExportSpecification以名称Import-xlsxTest
保存。现在,如果我将Excel文件从“xlsxTest.xlsx”重命名为“anotherTest.xlsx”,我可以使用以下VBA代码更改ImportExportSpecification的XML中的文件名,然后执行导入:
Option Compare Database
Option Explicit
Sub DoExcelImport()
Dim ies As ImportExportSpecification, i As Long, oldXML() As String, newXML As String
Const newXlsxFileSpec = "C:\Users\Gord\Desktop\anotherTest.xlsx" ' for testing
Set ies = CurrentProject.ImportExportSpecifications("Import-xlsxTest")
oldXML = Split(ies.XML, vbCrLf, -1, vbBinaryCompare)
newXML = ""
For i = 0 To UBound(oldXML)
If i = 1 Then
' re-write the second line of the existing XML
newXML = newXML & _
"<ImportExportSpecification Path = """ & _
newXlsxFileSpec & _
""" xmlns=""urn:www.microsoft.com/office/access/imexspec"">" & _
vbCrLf
Else
newXML = newXML & oldXML(i) & vbCrLf
End If
Next
ies.XML = newXML
ies.Execute
Set ies = Nothing
End Sub
有关ImportExportSpecification
对象的更多信息,请参阅
看到这个并认为我会分享一些我曾经努力解决问题的东西。更多地控制您可以在规范中更改的内容:
' MSXML2 requires reference to "Microsoft XML, v6.0"
' earlier versions are probably compatible, remember to use the appropriate DOMDocument object version.
Sub importExcelFile(ImportSpecName As String, Filename As String, SheetName As String, OutputTableName As String)
Dim XMLData As MSXML2.DOMDocument60
Dim ImportSpec As ImportExportSpecification
Dim XMLNode As IXMLDOMNode
' Get XML object to manage the spec data
Set XMLData = New MSXML2.DOMDocument60
XMLData.async = False
XMLData.SetProperty "SelectionLanguage", "XPath"
XMLData.SetProperty "SelectionNamespaces", "xmlns:imex='urn:www.microsoft.com/office/access/imexspec'"
' need to rename the default namespace, so that we can XPath to it. New name = 'imex'
' existing Import Specification (should be set up manually with relevant name)
Set ImportSpec = CurrentProject.ImportExportSpecifications(ImportSpecName)
XMLData.LoadXML ImportSpec.XML
' change it's path to the one specified
With XMLData.DocumentElement
.setAttribute "Path", Filename
' Destination attribute of the ImportExcel node
Set XMLNode = .SelectSingleNode("//imex:ImportExcel/@Destination") ' XPath to the Destination attribute
XMLNode.Text = OutputTableName
' Range attribute of the ImportExcel node
Set XMLNode = .SelectSingleNode("//imex:ImportExcel/@Range") ' XPath to the range attribute
XMLNode.Text = SheetName & "$"
End With
ImportSpec.XML = XMLData.XML
' run the updated import
ImportSpec.Execute
End Sub
我研究了同样的问题。 Gord发布的解决方案给了我一个XML解释错误。 Cosmichighway发布了这个解决方案:http://www.utteraccess.com/forum/index.php?showtopic=1981212。
此解决方案适用于Access 2010和Access 2013,也适用于Access 2007。
With CurrentProject.ImportExportSpecifications("nameOfSpecification")
debug.print .XML
.XML = Replace(.XML, varSavedPathName, varNewPathName)
debug.print .XML
End With
我为每个导出生成一个唯一的文件名,所以一旦完成该过程,我就会恢复原始的文件名路径。 WorkHoursTransactions是一个常量。例:
CONST ConstExportSavedPathName="c:\temp\Name Of File To Use.xls"
tmpFileName = WorkHoursTransactions & ";" & Format(Now(), "YYYYMMDD-HHMMSS") & ".xls"
With CurrentProject.ImportExportSpecifications(WorkHoursTransactions)
.XML = Replace(.XML, ConstExportSavedPathName, tmpFileName)
'Debug.Print .XML
End With
DoCmd.OpenReport WorkHoursTransactions, acViewReport, , , acWindowNormal
DoCmd.RunSavedImportExport WorkHoursTransactions
' return to original filename
With CurrentProject.ImportExportSpecifications(WorkHoursTransactions)
.XML = Replace(.XML, tmpFileName, ConstExportSavedPathName)
'Debug.Print .XML
End With
我也遇到了这个很好的提示,使用立即窗口来显示XML。如果您有一个名为“Export-Table1”的导出规范,那么您可以将其粘贴到即时窗口中以查看XML:
? CurrentProject.ImportExportSpecifications.Item("Export-Table1").XML
就我而言
vbCrLf不起作用 - 但vbLF确实有效!
我正在使用Access 2010(32位)。
斯特凡的问候
要添加到@Alberts答案,如果我们将当前文件路径作为常量,那么,当我们下次运行代码时(例如,用户决定在一段时间后将excel文件存储在不同的文件夹中),'替换'功能将找不到搜索文本,因为路径在第一次运行中已更改。因此,为了使其成为动态的,我们只需要在将其替换为新路径时将当前文件路径写入表中。在“替换”功能中,我们只是引用此值。文件路径没有硬编码。
Let Current File Path = DLookup("[Current file path]", "File Path Table")
Let New File Path = DLookup("[New file path]", "File Path Table")
With CurrentProject.ImportExportSpecifications("Saved-Export")
.XML = Replace(.XML, Current File Path, New File Path)
End With
DoCmd.RunSavedImportExport Saved-Export
'Now you write the 'new file path' to the 'current file path' field in the table
Set mydb = DBEngine.Workspaces(0).Databases(0)
Set myset = mydb.OpenRecordset("File Path Table")
myset.Edit
Let myset![Current file path] = New File Path
myset.Update
myset.Close
Set myset = Nothing
Set mydb = Nothing
所以下次运行时,它会选择要替换的正确当前文件。