Ms access 如何为保存的Excel导入指定不同的文件路径
因此,我多次使用Ms access 如何为保存的Excel导入指定不同的文件路径,ms-access,vba,Ms Access,Vba,因此,我多次使用doCmd.TransferText来使用保存的文本导入规范,因为您可以轻松地保存从应用程序.FileDialog(msoFileDialogFilePicker)返回的文件路径,以查找一个文件,并选择要使用保存的规范导入的文件 然而,我很难找到一种方法来处理excel文件,保存excel导入规范很简单,但是使用DoCmd.TransferSpreadSheet方法无法使用保存的导入,也无法使用DoCmd.RunSavedImportExport指定文件路径 除了使用不同的文件类
doCmd.TransferText
来使用保存的文本导入规范,因为您可以轻松地保存从应用程序.FileDialog(msoFileDialogFilePicker)
返回的文件路径,以查找一个文件,并选择要使用保存的规范导入的文件
然而,我很难找到一种方法来处理excel文件,保存excel导入规范很简单,但是使用DoCmd.TransferSpreadSheet
方法无法使用保存的导入,也无法使用DoCmd.RunSavedImportExport
指定文件路径
除了使用不同的文件类型(例如,csv)之外,是否还有其他解决方法?Access中的“保存的导入”和“保存的导出”存储在构成CurrentProject.ImportExportSpecifications
集合的ImportExportSpecification
对象中。保存的Excel导入的详细信息类似于以下XML,我通过手动导入Excel电子表格并勾选导入向导最后一页上的“保存导入步骤”复选框创建了该XML
ImportExportSpecification以名称Import xlsxTest
保存。现在,如果我将Excel文件从“xlsxTest.xlsx”重命名为“anotherTest.xlsx”,我可以使用以下VBA代码更改ImportExportSpecification的XML中的文件名,然后执行导入:
选项比较数据库
选项显式
子DoExcelImport()
作为ImportExportSpecification,i作为Long,oldXML()作为String,newXML作为String
Const newXlsxFileSpec=“C:\Users\Gord\Desktop\anotherTest.xlsx”用于测试
设置ies=CurrentProject.ImportExportSpecifications(“导入xlsxTest”)
oldXML=Split(ies.XML,vbCrLf,-1,vbBinaryCompare)
newXML=“”
对于i=0到UBound(oldXML)
如果i=1,那么
'重新写入现有XML的第二行
newXML=newXML&_
"" & _
换行字符
其他的
newXML=newXML&oldXML(i)&vbCrLf
如果结束
下一个
ies.XML=newXML
执行
设置ies=无
端接头
有关ImportExportSpecification
对象的详细信息,请参阅
就我而言
vbCrLf不起作用-但vbLF起作用
我使用的是Access 2010(32位)
Stefan的问候我研究了同样的问题。Gord发布的解决方案给了我一个XML解释错误。Cosmichighway发布了此解决方案: 此解决方案适用于Access 2010和Access 2013,也适用于Access 2007
With CurrentProject.ImportExportSpecifications("nameOfSpecification")
debug.print .XML
.XML = Replace(.XML, varSavedPathName, varNewPathName)
debug.print .XML
End With
每次导出时,我都会生成一个唯一的文件名,因此在完成该过程后,我会恢复到原始文件名路径。工时事务是一个常量。例如:
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
看到这一点,我想我应该和大家分享一下我前一段时间为解决这个问题所做的工作。提供对规范中可更改内容的更多控制:
' 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
要添加到@Alberts answer,如果我们将当前文件路径作为常量,那么,当我们下次运行代码时(例如,用户决定在某个时间后将excel文件存储到其他文件夹中),“Replace”函数将找不到搜索文本,因为路径在第一次运行时已更改。因此,为了使其动态化,我们只需要在当前文件路径被新路径替换时将其写入表中。在“Replace”函数中,我们只引用此值。文件路径没有硬编码
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
因此,下次运行时,它将选择正确的当前文件进行替换。显然,这应该非常有效,并提供了良好的背景信息,但我发现第二个子()更直截了当,我更喜欢这种方法,因为它更健壮;需要添加的另一件事是,您需要在“添加引用”中添加“MicrosoftXML,vX.Y”