| | |
Extract MS Excel Data embedded in MS Word
![]() |
•
•
Join Date: Apr 2006
Posts: 2
Reputation:
Solved Threads: 0
We have several thousand historical MS Word documents with embedded MS Excel Worksheets.
This data predates a relational database system which now manages all this data. Sound familiar!
We need to create a spreadsheet which has a row for each embedded MS Excel Worksheet with columns for specified values. We have no problem aggregating multiple spreadsheets into a single spreadsheet.
We have been unable to automatically extract The MS Excel worksheets from the MS Word documents.
We have tried two general approaches:
Approach 1, Obtain Control of the Excel object from VB running inside Word
Approach 2, Select the Excel object inside Word and save as a separate file
Sample code for both approaches below.
Comments indicating which lines of code are working and not working.
Approach 1, Obtain Control of the Excel object from VB running from Word
Approach 2, Select the Excel object inside Word and save as a separate file
This data predates a relational database system which now manages all this data. Sound familiar!
We need to create a spreadsheet which has a row for each embedded MS Excel Worksheet with columns for specified values. We have no problem aggregating multiple spreadsheets into a single spreadsheet.
We have been unable to automatically extract The MS Excel worksheets from the MS Word documents.
We have tried two general approaches:
Approach 1, Obtain Control of the Excel object from VB running inside Word
Approach 2, Select the Excel object inside Word and save as a separate file
Sample code for both approaches below.
Comments indicating which lines of code are working and not working.
Approach 1, Obtain Control of the Excel object from VB running from Word
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
'We launch the code from a form which will later loop through the 1000+ documents. Private Sub CommandButton1_Click() 'Start a new instance of Microsoft Word with the target document Dim oWordDoc As Word.Document Set oWordDoc = GetObject("C:\SourceDocument.doc", "Word.Document") 'This works, Word opens with the document. Dim intCount As Integer intCount = oWordDoc.InlineShapes.count ' This works, we see the single OLE object embedded. Dim oInlineShape As InlineShape Set oInlineShape = oWordDoc.InlineShapes.Item(1) 'This works, we obtain control of the OLE object. 'Dim oExcelSheet As Excel.Worksheet 'Set oExcelSheet = oInlineShape 'This DOES NOT work. 'Have tried several means to cast to an Excel object. 'Am not sure this type of cast can be done at all, and, if the object is a Worksheet. 'Once we have it as an object we might manipulate it in place to get the fields ' Or, at minimum save it to disk as an Excel document. 'oExcelSheet.SaveAs ("C:\SourceDocument.xls") 'Clean up here. End Sub
Approach 2, Select the Excel object inside Word and save as a separate file
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
'We launch the code from a form which will later loop through the 1000+ documents. Private Sub CommandButton1_Click() 'Start a new instance of Microsoft Word with the target document Dim oWordDoc As Word.Document Set oWordDoc = GetObject("C:\SourceDocument.doc", "Word.Document") 'This works, Word opens with the document. Dim intCount As Integer intCount = oWordDoc.InlineShapes.count ' This works, we see the single OLE object embedded. Dim oInlineShape As InlineShape Set oInlineShape = oWordDoc.InlineShapes.Item(1) 'This works, we obtain control of the OLE object. oInlineShape.OLEFormat.DoVerb (wdOLEVerbShow) 'This works, opens the OLE Excel object in Excel. Very cool. 'But now we attempt to gain control of the Excel Worksheet which was opened ' and can not seem to find it. 'The idea here is to find the Active Excel window which was opened by the code above. Dim xlWindow As Excel.Window Set xlWindow = xlApp.Windows.Item(1) 'The Window count is zero and there are no items to grab. xlWindow.Activate Set xlWorkbook = xlApp.ActiveWorkbook 'This too is empty xlWorkbook.SaveAs ("C:\SourceDocument.xls") xlWorkbook.Close 'Clean up here. End Sub
Last edited by Comatose; Apr 20th, 2006 at 6:09 pm. Reason: Code Tags
Hmn, I found this, but I'm not sure if it can be modified to work with excel (should): http://skp.mvps.org/pptxp010.htm
•
•
Join Date: Jul 2006
Posts: 2
Reputation:
Solved Threads: 0
If you are still interested, I know how!
Robert
Robert
•
•
•
•
Originally Posted by qualityprocess
We have several thousand historical MS Word documents with embedded MS Excel Worksheets.
This data predates a relational database system which now manages all this data. Sound familiar!
We need to create a spreadsheet which has a row for each embedded MS Excel Worksheet with columns for specified values. We have no problem aggregating multiple spreadsheets into a single spreadsheet.
We have been unable to automatically extract The MS Excel worksheets from the MS Word documents.
We have tried two general approaches:
Approach 1, Obtain Control of the Excel object from VB running inside Word
Approach 2, Select the Excel object inside Word and save as a separate file
Sample code for both approaches below.
Comments indicating which lines of code are working and not working.
Approach 1, Obtain Control of the Excel object from VB running from Word
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
'We launch the code from a form which will later loop through the 1000+ documents. Private Sub CommandButton1_Click() 'Start a new instance of Microsoft Word with the target document Dim oWordDoc As Word.Document Set oWordDoc = GetObject("C:\SourceDocument.doc", "Word.Document") 'This works, Word opens with the document. Dim intCount As Integer intCount = oWordDoc.InlineShapes.count ' This works, we see the single OLE object embedded. Dim oInlineShape As InlineShape Set oInlineShape = oWordDoc.InlineShapes.Item(1) 'This works, we obtain control of the OLE object. 'Dim oExcelSheet As Excel.Worksheet 'Set oExcelSheet = oInlineShape 'This DOES NOT work. 'Have tried several means to cast to an Excel object. 'Am not sure this type of cast can be done at all, and, if the object is a Worksheet. 'Once we have it as an object we might manipulate it in place to get the fields ' Or, at minimum save it to disk as an Excel document. 'oExcelSheet.SaveAs ("C:\SourceDocument.xls") 'Clean up here. End Sub
Approach 2, Select the Excel object inside Word and save as a separate file
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
'We launch the code from a form which will later loop through the 1000+ documents. Private Sub CommandButton1_Click() 'Start a new instance of Microsoft Word with the target document Dim oWordDoc As Word.Document Set oWordDoc = GetObject("C:\SourceDocument.doc", "Word.Document") 'This works, Word opens with the document. Dim intCount As Integer intCount = oWordDoc.InlineShapes.count ' This works, we see the single OLE object embedded. Dim oInlineShape As InlineShape Set oInlineShape = oWordDoc.InlineShapes.Item(1) 'This works, we obtain control of the OLE object. oInlineShape.OLEFormat.DoVerb (wdOLEVerbShow) 'This works, opens the OLE Excel object in Excel. Very cool. 'But now we attempt to gain control of the Excel Worksheet which was opened ' and can not seem to find it. 'The idea here is to find the Active Excel window which was opened by the code above. Dim xlWindow As Excel.Window Set xlWindow = xlApp.Windows.Item(1) 'The Window count is zero and there are no items to grab. xlWindow.Activate Set xlWorkbook = xlApp.ActiveWorkbook 'This too is empty xlWorkbook.SaveAs ("C:\SourceDocument.xls") xlWorkbook.Close 'Clean up here. End Sub
•
•
Join Date: Jul 2006
Posts: 2
Reputation:
Solved Threads: 0
Hi,
I would be happy to help you out. This is a domain that I know very well for I have developed a Word VBA macro for a customer that automatically converts and replaces all linked and embedded Excel sheets in a Word document by Word tables.
There are four types of linked / embedded Excel sheets in a Word document. Each type requires special code to extract the Excel sheet. The difficult part is extracting information about the Excel range that is visible in the Word document.
Let me know more about what you need, and we could work something out.
Here is my email address: robert.baron@videotron.ca
Regards,
Robert Baron
Chambly, Canada
I would be happy to help you out. This is a domain that I know very well for I have developed a Word VBA macro for a customer that automatically converts and replaces all linked and embedded Excel sheets in a Word document by Word tables.
There are four types of linked / embedded Excel sheets in a Word document. Each type requires special code to extract the Excel sheet. The difficult part is extracting information about the Excel range that is visible in the Word document.
Let me know more about what you need, and we could work something out.
Here is my email address: robert.baron@videotron.ca
Regards,
Robert Baron
Chambly, Canada
![]() |
Similar Threads
- Converting Excel Data to MSAccess .MDB in VB.NET (VB.NET)
- Reading excel data using vb 6.0 (Visual Basic 4 / 5 / 6)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Visual basic Help please!
- Next Thread: Using Delay or Pause, without the whole thing freezing?
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age application basic beginner birth bmp calculator cd cells.find click client code college component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report save search sendbyte sites sort sql sql2008 sqlserver subroutine tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows






