【问题标题】:Is there a way to bulk create word docs有没有办法批量创建 word 文档
【发布时间】:2018-08-31 04:20:09
【问题描述】:

我正在尝试从 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 

我正在尝试跳过单独创建和保存数百个新文件的手动步骤。

有没有人有创造性的解决方案?

【问题讨论】:

标签: excel ms-word


【解决方案1】:

假设您的文件名以单元格 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

【讨论】:

    猜你喜欢
    • 2020-04-21
    • 1970-01-01
    • 2020-05-02
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多