Import text from MS Word documents into Excel

lonely

Board Regular
Joined
Feb 28, 2003
Messages
230
Hi All,
I have multiple MS Word files, each of which has a paragraph. I need to get the data from each of these word files into a new cell in Excel.

For Example:
1. Open First Word Document... read all the text... and close this document
2. Paste this text in Cell A1 in MyWorkbook.xls
3. Open Second Word Document... read all the text... and close this document
4. Paste this text in Cell A2 in MyWorkbook.xls
and so on............

can someone help me in this?
Thanks in advance...
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
This gets text from all Word documents in c:\test and puts it into column A of the active sheet.

Code:
Option Explicit

Sub GetWordText()
  ' late binding
  Dim bWdRunning As Boolean
  Dim wdApp As Object
  Dim wdDoc As Object
  Dim wdDocName As String
  Dim wdText As String
  Dim rTarget As Range
  
  ' folder containing Word documents
  Const sPATH As String = "C:\test\"
  
  ' find first file
  wdDocName = Dir(sPATH & "*.doc")
  
  ' only continue if file is found
  If Len(wdDocName) > 0 Then
    
    ' reference running Word instance
    Set wdApp = GetObject(, "Word.Application")
    On Error Resume Next
    ' check if Word instance found
    If wdApp Is Nothing Then
      ' open new Word instance
      Set wdApp = CreateObject("Word.Application")
      bWdRunning = False
    Else
      bWdRunning = True
    End If
    On Error GoTo 0
    
    Do
      ' open Word document
      Set wdDoc = wdApp.documents.Open(sPATH & wdDocName)
      
      With wdDoc
        'get document contents
        wdText = .Range(0, .Characters.Count)
      End With
      
      ' determine target cell
      Set rTarget = ActiveSheet.Cells(65536, 1).End(xlUp)
      If Len(rTarget.Text) > 0 Then Set rTarget = rTarget.Offset(1, 0)
      
      ' populate cell
      rTarget.Value = wdText
      
      ' close Word document
      wdDoc.Close False
      
      ' find next file
      wdDocName = Dir
      ' exit if no more files
      If Len(wdDocName) = 0 Then Exit Do
    Loop
    
    ' clean up
    Set wdDoc = Nothing
    If Not bWdRunning Then wdApp.Quit
    Set wdApp = Nothing
  End If
  
End Sub
 
Upvote 0
There's some good building blocks in that one:

- Getting a list of a particular type of files in a directory
- Automating another application (albeit late-binding)
- Opening a Word doc, dealing with Word ranges, closing the Doc
- Populating the next cell in the worksheet
 
Upvote 0

Forum statistics

Threads
1,215,460
Messages
6,124,949
Members
449,198
Latest member
MhammadishaqKhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top