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

时间:2012-01-16 14:32:21

标签: vba excel-vba xls excel

如何在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

1 个答案:

答案 0 :(得分:1)

您使用计数器变量而不是单元格值,因此您假设需要"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