从Excel VBA写入Word文档

时间:2014-04-19 18:00:35

标签: excel vba excel-vba

我可以从VBA宏写入Word文档,但是我在编写标题,段落,表格等语法方面遇到了麻烦......

此示例将两行写入Word文档,但两者都作为标题样式出现。我想要一个行标题和段落中的一行或“正常”样式...

   Dim wdApp As Word.Application
   Set wdApp = New Word.Application

   With wdApp
    .Visible = True
    .Activate
    .Documents.Add

    With .Selection        
        .Style = "Heading 1"
        .TypeText ("My Heading")
        .TypeParagraph

        .Style = "Normal"
        .TypeText ("Some regular paragraph text")

    End With

1 个答案:

答案 0 :(得分:0)

这就是我想要的。

With .Selection
    .Style = "Heading 1"
    .TypeText "Header 1"
    .TypeParagraph

    .Style = "Heading 2"
    .TypeText "Header 1.1"
    .TypeParagraph

    .Style = "No Spacing"
    .TypeText "some text"
    .TypeParagraph

    .Style = "Heading 1"
    .TypeText "Header 2"
    .TypeParagraph
End With