有没有一种方法可以批量创建Word文档

时间:2018-08-31 04:20:10

标签: excel ms-word

我正在尝试从excel列表中批量创建单个Word文档。

示例列表:

  **Excel Column A**(Document Titles)
  **Row 1:** MAIN 08-30-18 - ECOM
  **Row 2:** SF 08-31-18 - DRM
  **Row 3:** MAIN 09-01-18 - SPONSORED 

我试图跳过手动创建和保存数百个新文件的手动步骤。

有人有创意的解决方案吗?

1 个答案:

答案 0 :(得分:0)

假设文件名以单元格A1,A2,A3 .....开头,下面的代码将创建带有示例文本的Word文件

Sub TEST1()
    Dim lastrow As Variant
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Dim i, j As Integer
    lastrow = Range("A" & Rows.Count).End(xlUp).Row
    For j = 1 To lastrow
        Set wrdApp = CreateObject("Word.Application")
        wrdApp.Visible = True
        Set wrdDoc = wrdApp.Documents.Add
        With wrdDoc
            For i = 1 To 5
                .Content.InsertAfter "Sample Test Line #" & i
                .Content.InsertParagraphAfter
            Next i
            'change the below location
            If Dir("C:\work\MyNewWordDoc" & j & ".doc") <> "" Then ' Check if file exists already
                Kill "C:\work\MyNewWordDoc.doc"
            End If
            .SaveAs ("C:\work\MyNewWordDoc" & j & ".doc")
            .Close ' close the document
        End With
        wrdApp.Quit ' close the Word application
        Set wrdDoc = Nothing
        Set wrdApp = Nothing
    Next j
End Sub