2014-04-28 92 views
7

因此,我多次使用doCmd.TransferText來使用保存的文本導入規範,因爲您可以輕鬆地保存從Application.FileDialog(msoFileDialogFilePicker)返回的文件路徑以找到選擇所需文件用保存的規範導入。如何爲已保存的Excel導入指定不同的文件路徑

但是我很難找到一種方法來處理excel文件,它很容易保存excel導入規範,但使用DoCmd.TransferSpreadSheet方法沒有辦法使用保存的導入,以及使用doCmd.RunSavedImportExport沒有選擇指定文件路徑。

對此有其他任何工作比周圍使用不同的文件類型(如.CSV)

回答

8

「保存導入」和訪問「已保存的出口」被存儲在形成CurrentProject.ImportExportSpecifications集合ImportExportSpecification對象。已保存的Excel導入的詳細信息如下所示,它是通過手動導入Excel電子表格並在導入嚮導的最後一頁勾選「保存導入步驟」複選框創建的。

<?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。現在,如果我從「xlsxTest.xlsx」重命名的Excel文件「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對象的信息,請參閱

ImportExportSpecification Object (Access)

+0

一如既往地堅持和樂於助人Gord,謝謝! – pegicity

+0

非常感謝Gord! – ArtiBucco

+1

顯然這應該很好,並提供良好的背景信息,但我發現http://stackoverflow.com/a/14443025/6099655第二個子()更直接 – CWilson

0

在我的情況

vbCrLf沒有工作 - 但vbLF呢!

我使用Access 2010(32位)。

斯特凡的問候

2

我研究了同樣的問題。 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。如果你有一個名爲「導出-表1」出口規範,那麼您可以在即時窗口中粘貼此查看XML:

? CurrentProject.ImportExportSpecifications.Item("Export-Table1").XML 
2

看到這個我想我會分享一些我的工作了,而回解決這個問題。給出了什麼,你可以在技術規格改變更多的控制:

' 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 
0

爲了增加@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 

所以下次運行時,它會選擇正確的當前文件來替換。

相關問題