VBA字词:循环播放

问题描述

我有以下代码,它们能够在Word中打开嵌入式Excel表,将A1单元格更改为字符串“ Testing”,然后关闭嵌入式Excel。

到目前为止,此操作仅对文档中的第一个对象运行。我希望它能遍历它找到的所有对象,如下所示。请参阅“ ActiveDocument.Inlineshapes.Count”。我意识到现在它在第一个For循环中只找到1比1。我尝试了一些不同的方法,但无法正确完成。我猜想我需要在其中执行do while循环...对解决这个问题有帮助吗?

Sub TestOpenEditandSave()

Dim oOleFormat As OLEFormat
Dim lNumShapes As Long
Dim lShapeCnt As Long
Dim xlApp As Object

'ActiveDocument.Inlineshapes.Count

For lShapeCnt = 1 To 1 'ActiveDocument.Inlineshapes.Count
    If ActiveDocument.Inlineshapes(lShapeCnt).Type = wdInlineshapeEmbeddedOLEObject Then
        If ActiveDocument.Inlineshapes(lShapeCnt).OLEFormat.ProgID = "Excel.Sheet.8" Then
            ActiveDocument.Inlineshapes(lShapeCnt).OLEFormat.Edit
            Set xlApp = Getobject(,"Excel.Application")
            xlApp.Workbooks(1).Worksheets(1).Range("A1") = "Testing"

With Selection.Find
    .ClearFormatting
    .Text = "nothingMatch"
    .Execute Forward:=True
End With

        End If
    End If
Next lShapeCnt

End Sub

解决方法

如果您使用Word中的内置集合,这会更简单:

Sub FindShapes()
    Dim oInlineShape As InlineShape
    
    For Each oInLineShape In ActiveDocument.InlineShapes
        If oInlineShape.Type = wdInlineShapeEmbeddedOLEObject Then
            If oInlineShape.OLEFormat.ProgID = "Excel.Sheet.8" Then
                'Do stuff here
            End If
        End If
    Next oInlineShape
End Sub