Excel到Word:未定义用户定义的类型,缺少引用中的Word库

时间:2014-10-09 14:22:38

标签: excel excel-vba ms-word export vba

我想将Excel中的一些单元格内容导出到Word,我想这样做

Sub CreateNewWordDoc()
     ' to test this code, paste it into an Excel module
     ' add a reference to the Word-library
     ' create a new folder named C:\Foldername or edit the filnames in the code
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Dim i As Integer
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
    Set wrdDoc = wrdApp.Documents.Add
     ' or
     'Set wrdDoc = wrdApp.Documents.Open("C:\Foldername\Filename.doc")
     ' sample word operations
    With wrdDoc
        For i = 1 To 100
            .Content.InsertAfter "Here is a sample test line #" & i
            .Content.InsertParagraphAfter
        Next i
        If Dir("C:\Foldername\MyNewWordDoc.doc") <> "" Then
            Kill "C:\Foldername\MyNewWordDoc.doc"
        End If
        .SaveAs ("C:\Foldername\MyNewWordDoc.doc")
        .Close ' close the document
    End With
    wrdApp.Quit ' close the Word application
    Set wrdDoc = Nothing
    Set wrdApp = Nothing
End Sub

但是当测试Excel函数时说&#34;编译错误:用户定义的类型未定义&#34; 我想这是一个图书馆遗失,但我找不到任何合适的工具 - &gt;参考(VBA), 比如Microsoft Office World或Microsft Word ......我在哪里可以找到它们?

1 个答案:

答案 0 :(得分:0)

refence取决于您使用的MSOffice版本。 对我来说,它是&#34; Microsoft Word 12.0对象库&#34;

希望有所帮助。