将VBA中的标题样式从Excel应用到Word

时间:2016-09-15 06:47:34

标签: vba ms-word

以下是我在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

1 个答案:

答案 0 :(得分:0)

你必须:

  • 将所需文档的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