Extract MS Excel Data embedded in MS Word

Reply

Join Date: Apr 2006
Posts: 2
Reputation: qualityprocess is an unknown quantity at this point 
Solved Threads: 0
qualityprocess qualityprocess is offline Offline
Newbie Poster

Extract MS Excel Data embedded in MS Word

 
0
  #1
Apr 20th, 2006
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)
  1. 'We launch the code from a form which will later loop through the 1000+ documents.
  2.  
  3. Private Sub CommandButton1_Click()
  4.  
  5.  
  6.  
  7. 'Start a new instance of Microsoft Word with the target document
  8.  
  9. Dim oWordDoc As Word.Document
  10.  
  11. Set oWordDoc = GetObject("C:\SourceDocument.doc", "Word.Document")
  12.  
  13. 'This works, Word opens with the document.
  14.  
  15.  
  16.  
  17. Dim intCount As Integer
  18.  
  19. intCount = oWordDoc.InlineShapes.count
  20.  
  21. ' This works, we see the single OLE object embedded.
  22.  
  23.  
  24.  
  25. Dim oInlineShape As InlineShape
  26.  
  27. Set oInlineShape = oWordDoc.InlineShapes.Item(1)
  28.  
  29. 'This works, we obtain control of the OLE object.
  30.  
  31.  
  32.  
  33. 'Dim oExcelSheet As Excel.Worksheet
  34.  
  35. 'Set oExcelSheet = oInlineShape
  36.  
  37. 'This DOES NOT work.
  38.  
  39. 'Have tried several means to cast to an Excel object.
  40.  
  41. 'Am not sure this type of cast can be done at all, and, if the object is a Worksheet.
  42.  
  43.  
  44.  
  45. 'Once we have it as an object we might manipulate it in place to get the fields
  46.  
  47. ' Or, at minimum save it to disk as an Excel document.
  48.  
  49. 'oExcelSheet.SaveAs ("C:\SourceDocument.xls")
  50.  
  51.  
  52.  
  53. 'Clean up here.
  54.  
  55.  
  56.  
  57. 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)
  1. 'We launch the code from a form which will later loop through the 1000+ documents.
  2.  
  3. Private Sub CommandButton1_Click()
  4.  
  5.  
  6.  
  7. 'Start a new instance of Microsoft Word with the target document
  8.  
  9. Dim oWordDoc As Word.Document
  10.  
  11. Set oWordDoc = GetObject("C:\SourceDocument.doc", "Word.Document")
  12.  
  13. 'This works, Word opens with the document.
  14.  
  15.  
  16.  
  17. Dim intCount As Integer
  18.  
  19. intCount = oWordDoc.InlineShapes.count
  20.  
  21. ' This works, we see the single OLE object embedded.
  22.  
  23.  
  24.  
  25. Dim oInlineShape As InlineShape
  26.  
  27. Set oInlineShape = oWordDoc.InlineShapes.Item(1)
  28.  
  29. 'This works, we obtain control of the OLE object.
  30.  
  31.  
  32.  
  33. oInlineShape.OLEFormat.DoVerb (wdOLEVerbShow)
  34.  
  35. 'This works, opens the OLE Excel object in Excel. Very cool.
  36.  
  37.  
  38.  
  39.  
  40. 'But now we attempt to gain control of the Excel Worksheet which was opened
  41.  
  42. ' and can not seem to find it.
  43.  
  44.  
  45.  
  46. 'The idea here is to find the Active Excel window which was opened by the code above.
  47.  
  48. Dim xlWindow As Excel.Window
  49.  
  50. Set xlWindow = xlApp.Windows.Item(1)
  51.  
  52. 'The Window count is zero and there are no items to grab.
  53.  
  54. xlWindow.Activate
  55.  
  56.  
  57.  
  58. Set xlWorkbook = xlApp.ActiveWorkbook
  59.  
  60. 'This too is empty
  61.  
  62. xlWorkbook.SaveAs ("C:\SourceDocument.xls")
  63.  
  64. xlWorkbook.Close
  65.  
  66.  
  67.  
  68. 'Clean up here.
  69.  
  70.  
  71.  
  72. End Sub
Last edited by Comatose; Apr 20th, 2006 at 6:09 pm. Reason: Code Tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Extract MS Excel Data embedded in MS Word

 
0
  #2
Apr 20th, 2006
*Wipes His Forehead*

I'll get back to you on this one.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Extract MS Excel Data embedded in MS Word

 
0
  #3
Apr 25th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 2
Reputation: qualityprocess is an unknown quantity at this point 
Solved Threads: 0
qualityprocess qualityprocess is offline Offline
Newbie Poster

Re: Extract MS Excel Data embedded in MS Word

 
0
  #4
May 1st, 2006
Thank you! Will test shortly and respond.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 2
Reputation: RobertB is an unknown quantity at this point 
Solved Threads: 0
RobertB RobertB is offline Offline
Newbie Poster

Re: Extract MS Excel Data embedded in MS Word

 
0
  #5
Jul 27th, 2006
If you are still interested, I know how!

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)
  1. 'We launch the code from a form which will later loop through the 1000+ documents.
  2.  
  3. Private Sub CommandButton1_Click()
  4.  
  5.  
  6.  
  7. 'Start a new instance of Microsoft Word with the target document
  8.  
  9. Dim oWordDoc As Word.Document
  10.  
  11. Set oWordDoc = GetObject("C:\SourceDocument.doc", "Word.Document")
  12.  
  13. 'This works, Word opens with the document.
  14.  
  15.  
  16.  
  17. Dim intCount As Integer
  18.  
  19. intCount = oWordDoc.InlineShapes.count
  20.  
  21. ' This works, we see the single OLE object embedded.
  22.  
  23.  
  24.  
  25. Dim oInlineShape As InlineShape
  26.  
  27. Set oInlineShape = oWordDoc.InlineShapes.Item(1)
  28.  
  29. 'This works, we obtain control of the OLE object.
  30.  
  31.  
  32.  
  33. 'Dim oExcelSheet As Excel.Worksheet
  34.  
  35. 'Set oExcelSheet = oInlineShape
  36.  
  37. 'This DOES NOT work.
  38.  
  39. 'Have tried several means to cast to an Excel object.
  40.  
  41. 'Am not sure this type of cast can be done at all, and, if the object is a Worksheet.
  42.  
  43.  
  44.  
  45. 'Once we have it as an object we might manipulate it in place to get the fields
  46.  
  47. ' Or, at minimum save it to disk as an Excel document.
  48.  
  49. 'oExcelSheet.SaveAs ("C:\SourceDocument.xls")
  50.  
  51.  
  52.  
  53. 'Clean up here.
  54.  
  55.  
  56.  
  57. 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)
  1. 'We launch the code from a form which will later loop through the 1000+ documents.
  2.  
  3. Private Sub CommandButton1_Click()
  4.  
  5.  
  6.  
  7. 'Start a new instance of Microsoft Word with the target document
  8.  
  9. Dim oWordDoc As Word.Document
  10.  
  11. Set oWordDoc = GetObject("C:\SourceDocument.doc", "Word.Document")
  12.  
  13. 'This works, Word opens with the document.
  14.  
  15.  
  16.  
  17. Dim intCount As Integer
  18.  
  19. intCount = oWordDoc.InlineShapes.count
  20.  
  21. ' This works, we see the single OLE object embedded.
  22.  
  23.  
  24.  
  25. Dim oInlineShape As InlineShape
  26.  
  27. Set oInlineShape = oWordDoc.InlineShapes.Item(1)
  28.  
  29. 'This works, we obtain control of the OLE object.
  30.  
  31.  
  32.  
  33. oInlineShape.OLEFormat.DoVerb (wdOLEVerbShow)
  34.  
  35. 'This works, opens the OLE Excel object in Excel. Very cool.
  36.  
  37.  
  38.  
  39.  
  40. 'But now we attempt to gain control of the Excel Worksheet which was opened
  41.  
  42. ' and can not seem to find it.
  43.  
  44.  
  45.  
  46. 'The idea here is to find the Active Excel window which was opened by the code above.
  47.  
  48. Dim xlWindow As Excel.Window
  49.  
  50. Set xlWindow = xlApp.Windows.Item(1)
  51.  
  52. 'The Window count is zero and there are no items to grab.
  53.  
  54. xlWindow.Activate
  55.  
  56.  
  57.  
  58. Set xlWorkbook = xlApp.ActiveWorkbook
  59.  
  60. 'This too is empty
  61.  
  62. xlWorkbook.SaveAs ("C:\SourceDocument.xls")
  63.  
  64. xlWorkbook.Close
  65.  
  66.  
  67.  
  68. 'Clean up here.
  69.  
  70.  
  71.  
  72. End Sub
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1
Reputation: Wiseman is an unknown quantity at this point 
Solved Threads: 0
Wiseman Wiseman is offline Offline
Newbie Poster

Re: Extract MS Excel Data embedded in MS Word

 
0
  #6
Mar 27th, 2007
Originally Posted by RobertB View Post
If you are still interested, I know how!

Robert
I would like to know. I have 5000+ Word files in 13 directories that I need to extract the embedded Excel data from.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 2
Reputation: RobertB is an unknown quantity at this point 
Solved Threads: 0
RobertB RobertB is offline Offline
Newbie Poster

Re: Extract MS Excel Data embedded in MS Word

 
0
  #7
Mar 27th, 2007
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC