Multiple sheets with names and data from Word files

knpaddac

New Member
Joined
Feb 11, 2014
Messages
33
I have approximately 1,200 Word files and I want to create a single Excel file that has a sheet titled for each of those Word files. ALSO, I want to copy all of the contents of each Word file into A1 of the sheet that matches it. Is this possible to do with a macro? Otherwise, I have alot of copy/paste to do.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try something based on:
Code:
Sub GetWordDocumentData()
'Note: this code requires a reference to the Word object model, added via Tools|References
Application.ScreenUpdating = False
Dim StrFolder As String, StrFile As String, WkSht As Worksheet
StrFolder = GetFolder
If StrFolder = "" Then Exit Sub
Dim wdApp As New Word.Application, wdDoc As Word.Document
wdApp.DisplayAlerts = wdAlertsNone
StrFile = Dir(StrFolder & "\*.doc")
While StrFile <> ""
  On Error Resume Next
  Set WkSht = Sheets.Add
  MsgBox Split(StrFile, ".doc")(0)
  WkSht.Name = Split(StrFile, ".doc")(0)
  Set wdDoc = wdApp.Documents.Open(Filename:=StrFolder & "\" & StrFile, AddToRecentFiles:=False, Visible:=False)
  With wdDoc
    .Range.Copy
    .Close SaveChanges:=False
  End With
  WkSht.PasteSpecial Format:="Microsoft Word 8.0 Document Object"
  StrFile = Dir()
Wend
wdApp.Quit
Set wdDoc = Nothing: Set wdApp = Nothing: Set WkSht = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
Since you haven't said how you want the data to appear in Excel, I've inserted them as Word objects as that best preserves the original layout.
 
Upvote 0
Wow. Was not expecting anything so quickly. Thanks.
I suppose that I probably could have explained a little better. What I do (if I was doing each manually) is that I highlight and copy all of the text in the Word document and then use the 'match destination formatting' option under the paste command in Excel. This then puts all the data into separate cells, columns and rows, in the Excel file.
It is possible that what you have done already fits this, but I do have to ask, is it possible you could give a little info on what exactly I have to put into the Macro and highlight where I need to put it to work correctly?
 
Upvote 0
You could try changing:
WkSht.PasteSpecial Format:="Microsoft Word 8.0 Document Object"
to:
WkSht.Paste Destination:=WkSht.Range("A1")
or:
WkSht.Range("A1").PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True

PS: you could also delete:
MsgBox Split(StrFile, ".doc")(0)
That line's just left over from testing.
 
Last edited:
Upvote 0
Okay. Do I need to put somewhere the name of the folder that contains all of the Word documents that are being used?
 
Upvote 0
Not unless you want to. The macro already includes a folder browser, which allows you to select any folder.
 
Upvote 0
I have put it in, and made changes as you suggested, but whenever I run it I get an error "Compile error: User-defined type not defined" and the text "wdApp As New Word.Application" is highlighted.
 
Upvote 0
Did you read the first line in the macro? What do you mean by "I have put it in"? As written, the code doesn't require (or provide for) you to put anything in.
 
Upvote 0
I created a new macro in my Excel document and copied your initial code from above into that macro. Then made the changes you mentioned in comment #4. When I click 'run' I am getting the previously mentioned errors.
Maybe I am doing something wrong? Maybe this is beyond my abilities?
 
Upvote 0
The code I posted was an entire stand-alone macro. All you needed to do was to:
• add it to an appropriate Excel workbook
• add the Word references
• maybe, change the line:
WkSht.PasteSpecial Format:="Microsoft Word 8.0 Document Object"
• and delete the line:
MsgBox Split(StrFile, ".doc")(0)
If you don't do the second step, expect errors.
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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