Vba 如何在Word文档中打开(和保存)所有嵌入的文档?
我想写VBA来保存Word文档中的嵌入文件。但我在打开它们时遇到了一些问题:Vba 如何在Word文档中打开(和保存)所有嵌入的文档?,vba,error-handling,reference,ms-word,Vba,Error Handling,Reference,Ms Word,我想写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 ActiveD
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
如果第一个嵌入文件尚未打开,此代码将打开该文件。它在下一个错误
或者,如果第一个文件已经打开,宏似乎什么也不做
有什么提示吗?(我是用Word 2010来做这件事的。)答案似乎很简单-打开第一个嵌入文件后,它会变为活动文件,然后当您尝试打开下一个嵌入对象时,您会引用活动文档,而不是所需的文档。请按以下方式尝试使用对象变量:
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