PDA

View Full Version : Using data from an Excel file that's embedded in Word



holdemfoldem
10-02-2018, 01:49 PM
Hello all, I'm new to this thread.

I have written VB code in an Excel file that successfully reads a Word file and stores each line and stores it in a cell.

But I need to write something that can locate and read data, cell by cell, from any Excel files that are embedded in the Word doc and stores that data in the cells of the currently open Excel file.

I searched through posts but couldn't find any relevant posts/replies.

Thanks ahead for any help.

FWIW, here's the code that successfully reads lines from the Word file and stores them in the cells of the currently open Excel file:

Option Explicit
Sub GetWordata()

Dim singleLine As Paragraph
Dim lineText As String
Dim word_app As word.Application
Dim wDoc As word.Document
Dim r As Integer
Dim i As Integer

' word_app.Application.Templates.LoadBuildingBlocks
Set word_app = CreateObject("word.application")
Set wDoc = word_app.Documents.Open("U:/ExcelApps/MergeExcelFiles/test/test.docx")
word_app.Visible = True
i = 1

For Each singleLine In activeDocument.Paragraphs
lineText = singleLine.Range.Text
' MsgBox lineText
Sheets(1).Cells(1, i) = lineText
' Sheets("Data").Cells(1, i) = lineText
i = i + 1
Next singleLine
wDoc.Close

End Sub

macropod
10-02-2018, 03:17 PM
For code to process embedded Excel workbooks in a document, see: https://social.msdn.microsoft.com/Forums/office/en-US/5955da06-725d-45f2-aa1b-5eb37c0646c6/how-to-convert-all-embeded-excel-sheets-in-word-into-words-tables?forum=worddev. The code there is for converting such workbooks to Word tables, but the principles for data extraction are the same.

holdemfoldem
10-03-2018, 09:50 AM
For code to process embedded Excel workbooks in a document, see: https://social.msdn.microsoft.com/Forums/office/en-US/5955da06-725d-45f2-aa1b-5eb37c0646c6/how-to-convert-all-embeded-excel-sheets-in-word-into-words-tables?forum=worddev. The code there is for converting such workbooks to Word tables, but the principles for data extraction are the same.

Thanks. Looks promising. Will research code further. :)