DoCmd.TransferText 方法 (Access)

TransferText 方法執行 Visual Basic 中的 TransferText 巨集動作。

語法

expression.TransferText (TransferType, SpecificationName, TableName, FileName, HasFieldNames, HTMLTableName, CodePage)

expression 代表 DoCmd 物件的變數。

參數

名稱 必要/選用 資料類型 描述
TransferType Optional AcTextTransferType 您想要進行的傳輸類型。 您可以從分隔的或固定寬度文字檔案或 HTML 檔案,匯入、匯出或連結資料。 預設值為 acImportDelim。 Microsoft Access 專案 (.adp) 中僅支援 acImportDelimacImportFixedacExportDelimacExportFixedacExportMerge 傳輸類型。
SpecificationName Optional Variant 字串運算式,代表您已建立並儲存於目前資料庫中的匯入或匯出規格的名稱。 對於固定寬度的文字檔而言,您必須指定引數或使用 schema.ini 檔案,且該檔案必須與匯入、連結或匯出的文字檔儲存於相同的資料夾。

若要建立綱要描述檔案,您可以使用文字匯入/匯出精靈來建立檔案。 對於分隔的文字檔及 Microsoft Word 合併列印資料檔而言,您可以讓此引數保持空白,以選取預設的匯入/匯出規格。
TableName 選用 Variant 字串運算式,代表您的文字資料的匯入目標、匯出來源或連結目標的 Access 資料表名稱,或是您要將其結果匯出至文字檔之 Access 查詢的名稱。
FileName 選用 Variant 為一包含路徑之完整檔案名稱的字串運算式,代表您要匯入、匯出或連結的文字檔。
HasFieldNames 選用 Variant 使用 True (1) 在匯入、匯出或連結時使用文字檔的第一列當做欄位名稱。 使用 False (0) 將文字檔案的第一列視為一般資料。 如果您想讓此引數保持空白,則假設使用預設值 (False)。 系統會對 Microsoft Word 合併列印資料檔案忽略此引數,它必須一律在第一列包含欄位名稱。
HTMLTableName Optional Variant 字串運算式,代表您想要匯入或連結之 HTML 檔案中資料表或清單的名稱。 除非 TransferType 引數是設定為 acImportHTMLacLinkHTML,否則會忽略這個引述。 如果您將此引數保留空白,則會將 HTML 檔案中的第一個資料表或清單匯入或連結。

HTML 檔案中如果有 CAPTION 標籤,則其中的資料表或清單的名稱便由 CAPTION 標籤所指定的文字來判定。 如果沒有 CAPTION 標籤,其名稱便由 TITLE 標籤所指定的文字來判定。 具有相同名稱的資料表或清單如果不止一個,Access 便藉由在每個資料表或清單名稱的結尾加上編號來區別;例如 Employees1 及 Employees2。
字碼頁識別碼 Optional Variant Long 值,指出字碼頁的字元集。

註解

可以使用 TransferText 方法在目前的 Access 資料庫或 Access 專案 (.adp) 以及文字檔之間匯入或匯出文字。 您也可以將文字檔中的資料連結至目前的 Access 資料庫。 有了連結的文字檔之後,就可以使用 Access 來檢視文字資料,並且仍然可以從您的文字處理程式對資料進行完整的存取。 您也可以在 HTML 檔案 (*.html) 的資料表或清單中匯入、匯出和連結資料表或清單。

您可以將 Access 選取查詢中的資料匯出至文字檔。 Access 會將查詢的結果集當成資料表匯出。

範例

下列範例使用標準輸出 (Standard Output) 規格,將資料從 Access 資料表 External Report 匯出至分隔文字檔 April.doc。

DoCmd.TransferText acExportDelim, "Standard Output", _ 
    "External Report", "C:\Txtfiles\April.doc"

下列程式碼會示範如何建立新的 Microsoft Word 文件,然後使用 Customers 資料表中的資料執行合併列印。

Public Sub DoMailMerge(strFileSavePath As String)

    ' Create new Word App, add a document and set it visible
    Dim wdApp As New Word.Application
    wdApp.Documents.Add
    wdApp.Visible = True

    ' Open the data set from this database
    wdApp.ActiveDocument.MailMerge.OpenDataSource _
        Name:=Application.CurrentProject.FullName, _
        OpenExclusive:=False, _
        LinkToSource:=True, _
        Connection:="TABLE Customers", _
        SQLStatement:="SELECT Customers.* FROM Customers;"
              
    ' Add fields to the mail merge document
    Dim oSel As Object
    Set oSel = wdApp.Selection
    With wdApp.ActiveDocument.MailMerge.Fields
    
        oSel.TypeText vbNewLine & vbNewLine
        .Add oSel.range, "First_Name"
        oSel.TypeText " "
        .Add oSel.range, "Last_Name"
        oSel.TypeText vbNewLine
        .Add oSel.range, "Company"
        oSel.TypeText vbNewLine
        .Add oSel.range, "Address"
        oSel.TypeText vbNewLine
        .Add oSel.range, "City"
        oSel.TypeText ", "
        .Add oSel.range, "State"
        oSel.TypeText " "
        .Add oSel.range, "Zip"
        oSel.TypeText vbNewLine
        oSel.TypeParagraph
        oSel.TypeText "Dear "
        .Add oSel.range, "First_Name"
        oSel.TypeText ","
        oSel.TypeText vbNewLine
        oSel.TypeParagraph
        oSel.TypeText "We have created this mail just for you..."
        oSel.TypeText vbNewLine
        oSel.TypeText vbNewLine
        oSel.TypeText "Sincerely," & vbNewLine & "John Q. Public"
        oSel.TypeText vbFormFeed
        
    End With
    
    ' Execute the mail merge and save the document
    wdApp.ActiveDocument.MailMerge.Execute
    wdApp.ActiveDocument.SaveAs strFileSavePath
        
    ' Close everything and Cleanup Variables
    Set oSel = Nothing
    wdApp.ActiveDocument.Close False
    Set wdApp = Nothing

End Sub

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應