Modify embedded Excel workbook in Word document via VBA

后端 未结 3 908
無奈伤痛
無奈伤痛 2020-12-10 06:21

I have a Word document with two embedded Excel files (added using Insert -> Object -> Create From File) which I wish to modify using Word VBA. I have got to the point where

3条回答
  •  鱼传尺愫
    2020-12-10 07:15

    I have a solution to my own problem. Any further comments will be appreciated -

    Sub TestMacro()
    
        Dim lNumShapes As Long
        Dim lShapeCnt As Long
        Dim xlApp As Object
        Dim wrdActDoc As Document
    
        Set wrdActDoc = ActiveDocument
    
        For lShapeCnt = 1 To 1 'wrdActDoc.InlineShapes.Count
            If wrdActDoc.InlineShapes(lShapeCnt).Type = wdInlineShapeEmbeddedOLEObject Then
                If wrdActDoc.InlineShapes(lShapeCnt).OLEFormat.ProgID = "Excel.Sheet.8" Then
                    wrdActDoc.InlineShapes(lShapeCnt).OLEFormat.Edit
                    Set xlApp = GetObject(, "Excel.Application")
                    xlApp.Workbooks(1).Worksheets(1).Range("A1") = "This is A modified"
                    xlApp.Workbooks(1).Save
                    xlApp.Workbooks(1).Close
                    xlApp.Quit
                End If
            End If
        Next lShapeCnt
    
    End Sub
    

提交回复
热议问题