我有一批嵌入了Excel工作表的Word文档 . 用户通过双击工作表图像并打开嵌入的Excel对象,在Excel工作表中输入数据 . 我需要获取用户输入的数据 .

下面是WORD VBA,引用了Microsoft Excel 15库 . (在Office 2010下创建的Word和Excel对象 . )

我可以找到OLE对象,但我无法用它做任何事情 . 在下面的代码中,我尝试将对象分配给Worksheet对象,但是我遇到了类型不匹配错误 .

更复杂的是嵌入式Excel工作表有宏 . 在问题的一些过程中,将打开一个Excel窗口,其中会显示启用宏安全提示的提示 . 我很可能暂时禁用宏检查以通过此操作 .

我需要做的就是获取工作表中的数据,将其复制到其他地方一次 . 如果可能的话,我很乐意将工作表复制到外部文件中 .

我有Office 2010和2013,以及Visual Studio 2010 Pro和2014 Express .

如何获取嵌入的工作表数据?

Sub x()
        Dim oWS As Excel.Worksheet
        Dim oIShape As InlineShape
        For Each oIShape In ActiveDocument.InlineShapes
            If Not oIShape.OLEFormat Is Nothing Then
                 If InStr(1, oIShape.OLEFormat.ProgID, "Excel") Then
                    oIShape.OLEFormat.ActivateAs (oIShape.OLEFormat.ClassType) 'Excel.Sheet.8
                    Set oWS = oIShape  '** type mismatch
                    Debug.Print oWS.Cells(1, 1)
                End If
            End If
        Next oIShape
    End Sub

我使用了建议开始上一次尝试:Modify embedded Excel workbook in Word document via VBA

有适当的引用和代码搞砸了文档的一些问题 .

下面是另一个工作正常,但有一些问题和代码,我不明白 .

1)我不想使用编辑模式,但其他模式不起作用2)完美无瑕的参考Set xlApp = GetObject(,“Excel.Application”)很奇怪 . 某种无证的功能?

Sub TestMacro2()
        Dim lNumShapes As Long
        Dim lShapeCnt As Long
        Dim xlApp As Object
        Dim wrdActDoc As Document
        Dim iRow As Integer
        Dim iCol As Integer

        Set wrdActDoc = ActiveDocument
        For lShapeCnt = 1 To 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")
                    With xlApp.Workbooks(1).Worksheets(2) ' can be multiple sheets, #2 is needed in this case
                        For iCol = 3 To .UsedRange.Columns.Count
                            If .Cells(1, iCol) = "" Then Exit For
                           For iRow = 1 To .UsedRange.Rows.Count
                                    Debug.Print .Cells(iRow, iCol) & "; ";
                            Next iRow
                            Debug.Print 'line feed
                        Next iCol
                    End With
                    xlApp.Workbooks(1).Close
                    xlApp.Quit
                    Set xlApp = Nothing
                End If
            End If
        Next lShapeCnt
    End Sub

代码运行良好,可以完成我的提取任务 - 谢谢!