从Word中提取embedded的Excel工作表数据

我有一批embeddedExcel工作表的Word文档。 用户通过双击工作表图像并打开embedded的Excel对象,可以在Excel工作表中input数据。 我需要去用户input的数据。

下面是WORD VBA引用Microsoft Excel 15库。 (在Office 2010下创build的Word和Excel对象)

我可以findOLE对象,但我无法做任何事情。 在下面的代码中,我尝试将对象分配给一个Worksheet对象,但是我得到一个types不匹配的错误。

为了使事情进一步复杂化,embedded的Excel工作表具有macros。 在问题的某些过程中,将打开一个Excel窗口,提示是否启用macros安全提示。 我很可能暂时禁用macros检查,以通过这个。

我所需要做的就是获取工作表中的数据,将其复制到别处一次。 如果可能的话,我会很高兴将工作表复制到外部文件。

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

我怎样才能到达embedded式工作表数据?

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 

我使用build议的先前的尝试开始: 通过VBA在Word文档中修改embedded的Excel工作簿

有适当的参考和代码糟糕的文件的一些问题。

下面是另一个通行证,可以正常工作,但有一些问题和代码我不明白。

1)我不想使用编辑模式,但其他模式不起作用2)完美的引用设置xlApp = GetObject(,“Excel.Application”)是奇怪的。 某种无证的function?

  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 

代码工作得很好,完成我的提取任务 – 谢谢!