【问题标题】:Write to Word Document from Excel VBA从 Excel VBA 写入 Word 文档
【发布时间】:2014-04-20 02:55:19
【问题描述】:

我能够从 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

【问题讨论】:

  • 在 Word 宏记录器中记录您的步骤并阅读 Word 制作的代码。
  • 看不到您的代码有任何问题。它对我来说很好用
  • 它可以工作,但所有内容都被设置为标题 1。
  • 我有Heading 1 第一行样式和Normal 第二行样式:i.stack.imgur.com/TvFZL.png
  • simco - 是的,我简化了示例,现在可以使用了。

标签: vba excel


【解决方案1】:

这就是我想要的。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多