【问题标题】:How to open (and save) all embedded documents in a Word document?如何打开(和保存)Word 文档中的所有嵌入文档?
【发布时间】:2014-03-22 22:10:34
【问题描述】:

我正在寻找编写 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

如果第一个嵌入文件尚未打开,此代码将打开它。它在下一个错误。

或者,如果第一个文件已经打开,宏似乎什么都不做。

有什么提示吗? (我正在使用 Word 2010 执行此操作。)

【问题讨论】:

    标签: vba error-handling reference ms-word


    【解决方案1】:

    答案似乎很简单——在您打开第一个嵌入文件后,它会变为活动文件,然后当您尝试打开下一个嵌入对象时,您指的是活动文档,而不是所需的文件。以这种方式尝试使用对象变量:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多