944,035 Members | Top Members by Rank

Ad:
Apr 20th, 2006
0

Extract MS Excel Data embedded in MS Word

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
qualityprocess is offline Offline
2 posts
since Apr 2006
Apr 20th, 2006
0

Re: Extract MS Excel Data embedded in MS Word

*Wipes His Forehead*

I'll get back to you on this one.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Apr 25th, 2006
0

Re: Extract MS Excel Data embedded in MS Word

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
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
May 1st, 2006
0

Re: Extract MS Excel Data embedded in MS Word

Thank you! Will test shortly and respond.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
qualityprocess is offline Offline
2 posts
since Apr 2006
Jul 27th, 2006
0

Re: Extract MS Excel Data embedded in MS Word

If you are still interested, I know how!

Robert





Quote 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
RobertB is offline Offline
2 posts
since Jul 2006
Mar 27th, 2007
0

Re: Extract MS Excel Data embedded in MS Word

Click to Expand / Collapse  Quote originally posted by RobertB ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Wiseman is offline Offline
1 posts
since Mar 2007
Mar 27th, 2007
0

Re: Extract MS Excel Data embedded in MS Word

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
RobertB is offline Offline
2 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Visual basic Help please!
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Using Delay or Pause, without the whole thing freezing?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC