【问题标题】:Apply heading style in VBA from Excel to Word将 VBA 中的标题样式从 Excel 应用到 Word
【发布时间】:2016-09-15 16:18:28
【问题描述】:

这是我在 Excel 中用于控制 Word 文档并与一些数据一起发布的代码。 我想创建一些不同样式的文本,但不断收到运行时错误 430(类不支持自动化或不支持预期接口)

代码如下:

'Create the word document
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add
    objWord.Visible = True
    Set objSelection = objWord.Selection


    For i = 2 To 94
        'Heading 1
        If myRange(i - 1, 1) <> myRange(i, 1) Then
            objSelection.TypeParagraph
            objSelection.Style = ActiveDocument.Styles("Heading 2")
            objSelection.TypeText Text:=myRange(i, 1)
        End If
        objSelection.TypeParagraph
        objSelection.Style = ActiveDocument.Styles("Heading 3")
        objSelection.TypeText Text:=myRange(i, 2)
        For k = 3 To 12
            objSelection.TypeParagraph
            objSelection.Style = ActiveDocument.Styles("Heading 4")
            objSelection.TypeText Text:=myRange(1, k)
            objSelection.TypeParagraph
            objSelection.Style = ActiveDocument.Styles("Normal")
            objSelection.TypeText Text:=myRange(i, k)
        Next
    Next

【问题讨论】:

  • 错误出现在哪一行?
  • 一些初步想法:你的myRange 变量,是一个Excel.Range 对象吗?如果是,则您没有为对象提供正确的值。看起来您正在使用Range 作为Cell。此外,还有一个您可以在 Excel VBA 中引用的 Word 库。最后,正如Dirk所说,请说明错误发生在哪里
  • 我使用了 Word 库。唯一的问题是objSelection.Style = ActiveDocument.Styles("___")。这是行不通的。其余的工作都很好。

标签: vba ms-word


【解决方案1】:

你必须:

  • 在任意窗口设置Selection所需文档的对象

    Set objSelection = objDoc.ActiveWindow.Selection
    
  • 明确引用Word应用活动文档:

    objSelection.Style = objWord.ActiveDocument.Styles("Heading 2")
    

您可能还想使用With - End With 语法来清理您的代码,使其更具可读性、健壮性和更快

Option Explicit

Sub main()    
'' "early binding" case
'' requires adding Microsoft Word XX.Y Object Library" reference to your project
'''    Dim objWord As Word.Application '<--| "early binding" requires referencing 'Word' application explicitly
'''    Dim objDoc As Word.document '<--| "early binding" requires referencing 'Word' application explicitly
'''    Dim objSelection As Word.Selection '<--| "early binding" requires referencing 'Word' application explicitly

' "late binding" case
    Dim objWord As Object
    Dim objDoc As Object
    Dim objSelection As Object

    Dim myRange As Range '<--| for Excel objects, referencing 'Excel' explicitly is optional

    Dim i As Long, k As Long '<--| VBA variables

    Set myRange = ActiveSheet.Range("myRange") '<-- set myRange range object to your active worksheet named range "myRange"

    Set objWord = CreateObject("Word.Application") '<--| get a new instance of Word
    Set objDoc = objWord.Documents.Add '<--| add a new Word document
    objWord.Visible = True
    Set objSelection = objDoc.ActiveWindow.Selection '<--| get new Word document 'Selection' object

    With objSelection '<--| reference 'Selection' object

        For i = 2 To 94
            'Heading 1
            If myRange(i - 1, 1) <> myRange(i, 1) Then
                .TypeParagraph
                .Style = objWord.ActiveDocument.Styles("Heading 2")
                .TypeText Text:=myRange(i, 1).Text
            End If
            .TypeParagraph
            .Style = objWord.ActiveDocument.Styles("Heading 3")
            .TypeText Text:=myRange(i, 2).Text
            For k = 3 To 12
                .TypeParagraph
                .Style = objWord.ActiveDocument.Styles("Heading 4")
                .TypeText Text:=myRange(1, k).Text
                .TypeParagraph
                .Style = objWord.ActiveDocument.Styles("Normal")
                .TypeText Text:=myRange(i, k).Text
            Next
        Next
    End With

    objDoc.SaveAs "C:\Users\...\Desktop\Doc1.docx" '<--| save your word document

    objWord.Quit '<--| quit Word
    Set objWord = Nothing '<--| release object variable        
End Sub

【讨论】:

  • @user3016795,你通过了吗?
  • 谢谢!您对在“ActiveDocument”保存一天之前添加 objWord 的评论:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多