输入类型不匹配的设置文件等于,打开Word文件,

时间:2019-05-19 19:05:07

标签: excel vba ms-word

尝试打开W​​ord文件时Excel宏崩溃

我尝试过打开和关闭目标文件。这两个文件都在文件夹中。
我已经在项目中添加了以下对象模型:Visual Basic for Applications,Microsoft Excel 16.0对象库,Microsoft Forms 2.0对象库,Microsoft Word 16.0对象库。

    Sub CopyCheckBoxesToWord()
        CopyCheckBoxesToWord Macro
        'http://learnexcelmacro.com/wp/2012/01/copy-from-excel-and-paste-in-word/
        Dim objWord As Word.Application
        Set objWord = "Example.docx" 'Error Type mismatch here
        'Word file is in the same folder as the excel file.  I have it 
        'open.
        Range("G5").Copy
        With objWord
           .Documents.Add
           .Selection.PasteAndFormat wdFormatPlainText
           .Visible = True
        End With
        End Sub

我要访问该文件。它可以打开或关闭,但打开效果更好。 我想将G5的内容以无格式的unicode文本写入Word文件。

1 个答案:

答案 0 :(得分:0)

类型不匹配是因为您已将objWord声明为类型Word.Application,并且试图将其设置为String

也许是这样的:

    Dim objWord As Word.Application
    Dim objDoc As Word.Document


Set objWord = New Word.Application
With objWord
    Set objDoc = .Documents.Open("Example.docx")
    … *your code*

End With