vba 如何在 Word 文档中打开(和保存)所有嵌入的文档?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22584295/
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
How to open (and save) all embedded documents in a Word document?
提问by John
I'm looking to write VBA to save out embedded files in a Word document. But I'm having issues just opening them:
我正在寻找编写 VBA 来保存 Word 文档中的嵌入文件。但是我在打开它们时遇到了问题:
Sub Extract()
Dim num as Integer
Dim numObjects As Integer
numObjects = ActiveDocument.InlineShapes.count
MsgBox numObjects ' prints "11"
For num = 1 To numObjects
If ActiveDocument.InlineShapes(num).Type = 1 Then
'it's an embedded OLE type so open it.
ActiveDocument.InlineShapes(num).OLEFormat.Open
'Works for the first one but errors 5941 (the requested
' member of the collection does not exist)
End If
Next num
End Sub
This code opens the first embedded file if it's not already open. It errors on the next one.
如果第一个嵌入文件尚未打开,此代码将打开它。它在下一个出错。
Or, if the first file is already open, the macro just seems to do nothing.
或者,如果第一个文件已经打开,宏似乎什么都不做。
Any hints? (I'm doing this with Word 2010.)
任何提示?(我正在用 Word 2010 做这个。)
采纳答案by Kazimierz Jawor
The answer seems to be quite easy- after you open first embedded file it becomes active one and next when you try to open the next embedded object you refers to active document, not to desired one. Try with object variable in this way:
答案似乎很简单 - 在您打开第一个嵌入文件后,它会变为活动状态,当您尝试打开下一个嵌入对象时,您指的是活动文档,而不是所需的文件。以这种方式尝试使用对象变量:
Sub Extract()
Dim num as Integer
Dim AD as document
Set AD = activedocument
Dim numObjects As Integer
numObjects = AD.InlineShapes.count
MsgBox numObjects ' prints "11"
For num = 1 To numObjects
If AD.InlineShapes(num).Type = 1 Then
'it's an embedded OLE type so open it.
AD.InlineShapes(num).OLEFormat.Open
End If
Next num
End Sub