如何使用 Excel 宏生成或创建一系列 Word 文件?

发布于 2024-12-27 09:33:12 字数 865 浏览 2 评论 0原文

如何创建文件名作为 B(2-9999) 中单元格的 Word 文件序列?

我能够生成文件,但它被保存为 1,2,3,4...

例如

A       B
Title   Titletopic
11111   Fantasy Golf Resort
222     Golden Palms Resort & Spa
3333    Guest Line Hotels & Resorts
4444    Parkfield Resotel
555     Shreyas Retreat
666     Patels Inn
777     Plantation Trails
888     giri
9999    neil

我的代码是

Sub ControlWord()

    Dim appWD As Word.Application

    Set appWD = CreateObject("Word.Application.12")
    appWD.Visible = True

    Sheets("Sheet1").Select

    FinalRow = Range("A9999").End(xlUp).Row
    For i = 2 To FinalRow
        Sheets("Sheet1").Select

        Range("A" & i & ":B" & i).Copy


        appWD.Documents.Add

        appWD.Selection.Paste

        appWD.ActiveDocument.SaveAs Filename:="" & i

    Next i

End Sub

How can i create a sequence of word files with file name as cells in B(2-9999) ?

I am able to generate files but it gets saved as 1,2,3,4....

Eg

A       B
Title   Titletopic
11111   Fantasy Golf Resort
222     Golden Palms Resort & Spa
3333    Guest Line Hotels & Resorts
4444    Parkfield Resotel
555     Shreyas Retreat
666     Patels Inn
777     Plantation Trails
888     giri
9999    neil

my code is

Sub ControlWord()

    Dim appWD As Word.Application

    Set appWD = CreateObject("Word.Application.12")
    appWD.Visible = True

    Sheets("Sheet1").Select

    FinalRow = Range("A9999").End(xlUp).Row
    For i = 2 To FinalRow
        Sheets("Sheet1").Select

        Range("A" & i & ":B" & i).Copy


        appWD.Documents.Add

        appWD.Selection.Paste

        appWD.ActiveDocument.SaveAs Filename:="" & i

    Next i

End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雄赳赳气昂昂 2025-01-03 09:33:12

您使用计数器变量而不是单元格值,因此您假设想要 "11111 Fantasy Golf Resort.docx"

appWD.ActiveDocument.SaveAs Filename:= Range("A" & i).Value & " " & Range("B" & i).Value

或者假设第一个感兴趣的单元格是“A2”;

dim cell as range
for each cell in range("A2", range("A2").end(xldown))
    ...
    ...
    appWD.ActiveDocument.SaveAs Filename:= cell.value & " " & cell.offset(0, 1).value
    ...
next

Your using the counter variable not the cell value, so you assuming want "11111 Fantasy Golf Resort.docx"

appWD.ActiveDocument.SaveAs Filename:= Range("A" & i).Value & " " & Range("B" & i).Value

Or alternatively assuming the 1st cell of interest is "A2";

dim cell as range
for each cell in range("A2", range("A2").end(xldown))
    ...
    ...
    appWD.ActiveDocument.SaveAs Filename:= cell.value & " " & cell.offset(0, 1).value
    ...
next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文