excel - VBA:使用 Excel 编辑 Word 文档会出现运行时错误 438:对象不支持此属性或方法

标签 excel vba ms-word

尽管进行了大量的谷歌搜索,但我无法弄清楚这有什么问题。我仍然错过引用或其他什么吗?如果您能看到错误所在,我将永远感激不尽!

引用:

  • Visual Basic 应用程序
  • Microsoft Excel 16.0 对象库
  • OLE 自动化
  • Microsoft Office 16.0 对象库
  • 引用编辑控件
  • Microsoft Word 16.0 对象库

  • 变量:
    Public appWord As Word.Application
    Public sapmWord As Word.Document
    
    Dim asNimi As String 'in this current sub
    

    代码:
    On Error Resume Next
    
    Set appWord = GetObject(, "Word.Application")
    If Err <> 0 Then
        Set appWord = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    appWord.Visible = True
    Set sapmWord = appWord.documents.Open("C:\ThisIsWorking\andDocOpens.docx")
    'sapmWord.Activate 'doesn't make a difference
    
    With sapmWord
        Selection.EndKey Unit = wdStory 'this line is first line to give an error. With or without a dot in the beginning of line.
        Selection.TypeText Text:=asNimi 'this line too, if previous is commented
    '...and so on!
    End With
    
    sapmWord.Close savechanges:=True
    Set appWord = Nothing
    Set sapmWord = Nothing
    

    最佳答案

    sapmWord是word文档。 Word 文档没有 selection方法。 Word 应用程序对象有它,所以可能你的意思是(是的,你需要'.')

    With appWord 
        .Selection.EndKey Unit:= wdStory 
        .Selection.TypeText Text:=asNimi 
        '...and so on!
    End With
    

    关于excel - VBA:使用 Excel 编辑 Word 文档会出现运行时错误 438:对象不支持此属性或方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51037697/

    相关文章:

    python - 使用 openpyxl 写入行?

    excel - VLOOKUP 返回空白为 1/0/1990,而不是没有任何可见的内容

    excel - 删除所有工作表中除指定列外的所有列

    vba - 如何通过 VBA 以编程方式关闭 Microsoft Word 文档中的红色波浪线?

    vba - 在 VBA 中使用平滑线编写散点图

    EXCEL IF FORMULA 有多个条件

    vba - 如何将一系列行排序到一定数量的(未知)列?

    vba - ADODB 连接的记录集不完整

    r - 在 Rmarkdown 中更改 knitr 的单词模板

    PHP:发送WORD文档文件下载