使用 VBA 在 Excel 中打开嵌入对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34479378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open an Embedded Object in Excel using VBA
提问by Dev.K.
In an ms office document I've embedded / inserted an external document (object) (PDF in my case).
在 ms office 文档中,我嵌入/插入了一个外部文档(对象)(在我的情况下为 PDF)。
After opening the document, when I click on the PDF object icon, It opens up the PDF file embedded in it.
打开文档后,当我单击 PDF 对象图标时,它会打开嵌入其中的 PDF 文件。
Using VBA / Macro I want to do the same thing, Where I'll have to run a macro and it will open up the embedded PDF file(Without clicking on the PDF ICON).
使用 VBA / 宏我想做同样的事情,在那里我必须运行一个宏,它会打开嵌入的 PDF 文件(不点击 PDF 图标)。
Is it possible?
是否可以?
Thanks,
谢谢,
回答by Axel Richter
Excel:
电子表格:
You can get the OLEObjectform the OLEObjectsof the Worksheet. See OLEObjects- https://msdn.microsoft.com/en-us/library/office/ff840244.aspx, OLEObject- https://msdn.microsoft.com/en-us/library/office/ff838421.aspx, OLEObjectmembers - https://msdn.microsoft.com/EN-US/library/office/ff841208.aspx.
你可以得到OLEObject的形式OLEObjects的Worksheet。请参阅OLEObjects- https://msdn.microsoft.com/en-us/library/office/ff840244.aspx,OLEObject- https://msdn.microsoft.com/en-us/library/office/ff838421.aspx,OLEObject成员 - https ://msdn.microsoft.com/EN-US/library/office/ff841208.aspx。
There is a method Verbwhich has a verb for opening the object. See https://msdn.microsoft.com/EN-US/library/office/ff838827.aspx- Verbs - https://msdn.microsoft.com/EN-US/library/office/ff820926.aspx
有一种方法Verb有一个动词来打开对象。请参阅https://msdn.microsoft.com/EN-US/library/office/ff838827.aspx- Verbs - https://msdn.microsoft.com/EN-US/library/office/ff820926.aspx
Example:
例子:
Sub test()
With ActiveSheet
Set o = .OLEObjects("Objekt 1")
o.Verb xlVerbOpen
End With
End Sub
"Objekt 1" is the name of the object in the Excel worksheet. The object must be in the active sheet.
“对象 1”是 Excel 工作表中对象的名称。对象必须在活动工作表中。
Word:
单词:
In Word it depends on if the embedded object is in an InlineShapeor an Shape. And there is no OLEObjectscollection. So you must handle with Shape.OLEFormat. See InlineShapes- https://msdn.microsoft.com/en-us/library/office/ff822592.aspx, Shapes- https://msdn.microsoft.com/en-us/library/office/ff845240.aspx, Shape- https://msdn.microsoft.com/en-us/library/office/ff196943.aspx, OLEFormat- https://msdn.microsoft.com/EN-US/library/office/ff197153.aspx.
在Word这取决于如果嵌入对象处于InlineShape或Shape。而且没有OLEObjects收藏。所以你必须处理Shape.OLEFormat. 请参阅InlineShapes- https://msdn.microsoft.com/en-us/library/office/ff822592.aspx,Shapes- https://msdn.microsoft.com/en-us/library/office/ff845240.aspx,Shape- https: //msdn.microsoft.com/en-us/library/office/ff196943.aspx, OLEFormat- https://msdn.microsoft.com/EN-US/library/office/ff197153.aspx。
Example:
例子:
Sub test()
With ActiveDocument
Set oShape = .InlineShapes(1) 'The embedded object is the first InlineShape.
'Set oShape = .Shapes(1) 'The embedded object is the first Shape.
Set oOLEFormat = oShape.OLEFormat
oOLEFormat.Open
End With
End Sub
回答by Andrew L.
In short, when you already know which object you are referring to:
简而言之,当您已经知道您指的是哪个对象时:
Excel
电子表格
Sheets("Sheet1").OLEObjects("Object 1").Activate
Word
单词
ActiveDocument.InlineShapes(1).OLEFormat.Open

