2016-09-15 92 views
0

這裏是我在Excel中控制word文檔的代碼,並將它與一些數據一起發佈。 我想創造一些不同風格的文字,但一直收到運行時錯誤430(類不支持自動化或不支持預期的接口)將VBA中的標題樣式從Excel應用於Word

下面是代碼:

'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 
+0

在錯誤發生在哪一行? –

+0

一些初步的想法:你的myRange變量是一個Excel.Range對象嗎?如果是這樣,你就不能爲物體提供正確的價值。看起來你正在使用'Range'作爲'Cell'。此外,還有一個可以在Excel VBA中引用的Word庫。最後,正如Dirk所說,請指出發生錯誤的位置 – Zac

+0

我使用了Word的庫。唯一的問題是 'objSelection.Style = ActiveDocument.Styles(「___」)'。這不起作用。其餘的工作很好。 – user3016795

回答

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 
+0

@ user3016795,你通過了嗎? – user3598756

+0

謝謝!您在「ActiveDocument」之前添加objWord的意見保存了一天:) – user3016795

+0

不客氣 – user3598756

相關問題