麻辣家族討論版版's Archiver

linyancheng 發表於 2020-4-2 10:58

自動化合併列印技術

[i=s] 本帖最後由 linyancheng 於 2020-4-18 10:59 編輯 [/i]

Word合併Excel列印,早期版本是採相對路徑,Office 2007以後是採絕對路徑。
為避免因絕對路徑而失去Excel資料連結的麻煩,可在完成編輯Word主文件後,將其還原為標準的Word文件,然後在ThisDocument中加入以下代碼:

Private Sub Document_Open()

    On Error Resume Next
   
    With ThisDocument.MailMerge
        .MainDocumentType = wdCatalog '主文件型式設為型錄(目錄),也可設為信封wdEnvelopes、信件wdFormLetters或標籤wdMailingLabels。
        .OpenDataSource Name:=ThisDocument.Path & "\○○○○○.xls" '「○○○○○.xls」改成資料來源檔名。假設來源檔在同一資料夾中。
        
        '.Destination = wdSendToNewDocument
        '.SuppressBlankLines = True
        
        'With .DataSource
            '.FirstRecord = wdDefaultFirstRecord
            '.LastRecord = wdDefaultLastRecord
        'End With
        
        '.Execute True
    End With
   
    'ThisDocument.Close wdDoNotSaveChanges

End Sub

Private Sub Document_Close()

    On Error Resume Next
   
    ThisDocument.MailMerge.MainDocumentType = wdNotAMergeDocument

End Sub

月曆設計系統v5.0

VBA源碼公開
函數公式公開
功能變數公開

頁: [1]

麻辣家族討論版版為 麻辣學園 網站成員  由 昱得資訊工作室 © Since 1993 所提供