Expose Excel InlineShapes to edit

我是研究僧i 提交于 2021-01-29 06:01:55

问题


I am trying to automatize filling in a third party form from an Excel Workbook VBA module. They sadly have gone with a Word document with an in-line Excel workbook embedded, which contains cells as named ranges that I want to edit.

How do I assign the InlineShapes object to an Excel.workbook object so that I can expose it to Excel methods and properties?

This is what I have tried so far:

Sub test()
        Dim wdApp As Word.Application
        Set wdApp = CreateObject("word.application")
        wdApp.Visible = true ' for testing
        Dim wdAppendixB As Word.Document
        Set wdAppendixB = wdApp.Documents.Add(ThisWorkbook.Path & "\Templates\form_template.dotx")
        Dim wbAppB As Excel.Workbook
        wdAppendixB.InlineShapes.Item(1).OLEFormat.Edit
        Set wbAppB = wdAppendixB.InlineShapes.Item(1).OLEFormat.Object
        wbAppB.Sheets("Sheet1").Range("date1").Value = "2019-06-02"
Exit Sub

As soon as the script opens the OLE object for editing, the script stops with no errors. Closing the OLE object for editing does not resume the script.

If I omit editing the object and just set the workbook object to the OLEFormat.Object it errors out with Run-time error '430' "Class does not support Automation or does not support expected interface".

Any suggestion is appreciated.


回答1:


Use Activate, instead of Edit (or DoVerb with an appropriate wdOleVerb constant).

Note that this will leave the object in an activated state. There's no elegant way to emulate the user clicking outside the object to de-select it. The workarounds are to either open the object in its own window (instead of in-place) and close that file window OR to try to activate the object as a specific, non-existant class. Since this will trigger an error, this has to be wrapped in `On Error Resume Next' and 'On Error GoTo 0'.

Sub test()
        Dim wdApp As Word.Application
        Set wdApp = CreateObject("word.application")
        wdApp.Visible = True ' for testing
        Dim wdAppendixB As Word.Document
        Set wdAppendixB = wdApp.Documents.Add(ThisWorkbook.Path & "\Templates\form_template.dotx")
        Dim wbAppB As Excel.Workbook
        Dim of As Word.OLEFormat
        Set of = wdAppendixB.InlineShapes.Item(1).OLEFormat
        of.Activate '.Edit
        Set wbAppB = of.Object
        wbAppB.Sheets("Sheet1").Range("B1").Value = "2019-06-02"
        On Error Resume Next
        of.ActivateAs "This.Class.NotExist"
        On Error GoTo 0
End Sub


来源:https://stackoverflow.com/questions/56431672/expose-excel-inlineshapes-to-edit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!