返回列表 上一主題 發帖

請問用VBA執行WORD合併列印及其應用

本帖最後由 linyancheng 於 2020-4-1 14:24 編輯

回復 1# iceandy6150


在Excel中:

Shell "rundll32.exe url.dll,FileProtocolHandler " & ThisWorkbook.Path & "\○○○○○.doc", 1 '「○○○○○.doc」改成Word檔名。假設Word檔在同一資料夾中。

在Word中:

早期版本是相對路徑,Office 2007以後是絕對路徑。
為避免麻煩,可在完成編輯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
Word呈現極致的靜態之美,Excel展現極致的動態之美。

TOP

        靜思自在 : 不要小看自己,因為人有無限的可能。
返回列表 上一主題