Import Data from Word into Excel using VBA

knightofni

New Member
Joined
Sep 22, 2010
Messages
12
Hello all,

I have a series of word documents (25 in all) which are all completed to the same template.

What I would like to do is transfer the data from Word into Excel for each file.

Is there a way to do this using VBA? I've looked around but have just got stuck!

All suggestions greatly appreicated. Many thanks.
 

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
Hopefully, this should get you started. First, place all relevant Word files in a folder. Then specify the path to this folder in the following code, where indicated. Then add the desired code to copy the data from Word and paste it to Excel, where indicated. The following code, as is, creates an instance of Word, loops through each Word file in the specified folder, opens the file, closes the file without saving it, and quits Word.

Code:
[FONT=Verdana][COLOR=#000000][FONT=Verdana][COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR][/FONT][/COLOR][/FONT]
 
[FONT=Verdana][FONT=Verdana][COLOR=#000000][COLOR=darkblue]Sub[/COLOR] test()[/COLOR][/FONT]
 
[FONT=Verdana][COLOR=#000000]  [COLOR=darkblue]Dim[/COLOR] WordApp [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Object[/COLOR][/COLOR][/FONT]
[COLOR=#000000][FONT=Verdana]  [COLOR=darkblue]Dim[/COLOR] Doc [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Object[/COLOR][/FONT]
[FONT=Verdana]  [COLOR=darkblue]Dim[/COLOR] strPath [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR][/FONT]
[FONT=Verdana]  [COLOR=darkblue]Dim[/COLOR] strFile [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR][/FONT]
[FONT=Verdana]  [COLOR=darkblue]Dim[/COLOR] Cnt [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR][/FONT]
 
[FONT=Verdana]  Application.ScreenUpdating = [COLOR=darkblue]False[/COLOR][/FONT]
 
[FONT=Verdana]  [COLOR=darkblue]Set[/COLOR] WordApp = CreateObject("Word.Application")[/FONT]
 
[FONT=Verdana]  [COLOR=green]'Change the path accordingly[/COLOR][/FONT]
[FONT=Verdana]  strPath = "C:\Users\Domenic\Desktop\"[/FONT]
 
[FONT=Verdana]  [COLOR=darkblue]If[/COLOR] Right(strPath, 1) <> "\" [COLOR=darkblue]Then[/COLOR] strPath = strPath & "\"[/FONT]
 
[FONT=Verdana]  strFile = Dir(strPath & "*.doc")[/FONT]
 
[FONT=Verdana]  [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]While[/COLOR] Len(strFile) > 0[/FONT]
 
[FONT=Verdana]      Cnt = Cnt + 1[/FONT]
 
[FONT=Verdana]      [COLOR=darkblue]Set[/COLOR] Doc = WordApp.Documents.Open(strPath & strFile)[/FONT]
 
[FONT=Verdana]      [COLOR=green]'Your code to copy data from Word and paste it to Excel[/COLOR][/FONT]
 
[FONT=Verdana]      Doc.Close savechanges:=[COLOR=darkblue]False[/COLOR][/FONT]
 
[FONT=Verdana]      strFile = Dir[/FONT]
 
[FONT=Verdana]  [COLOR=darkblue]Loop[/COLOR][/FONT]
 
[FONT=Verdana]  WordApp.Quit[/FONT]
 
[FONT=Verdana]  [COLOR=darkblue]Set[/COLOR] WordApp = [COLOR=darkblue]Nothing[/COLOR][/FONT]
 
[FONT=Verdana]  Application.ScreenUpdating = [COLOR=darkblue]True[/COLOR][/FONT]
 
[FONT=Verdana]  [COLOR=darkblue]If[/COLOR] Cnt > 0 [COLOR=darkblue]Then[/COLOR][/FONT]
[FONT=Verdana]      MsgBox "Completed...", vbInformation[/FONT]
[FONT=Verdana]  [COLOR=darkblue]Else[/COLOR][/FONT]
[FONT=Verdana]      MsgBox "No '.doc' files were found...", vbExclamation[/FONT]
[FONT=Verdana]  [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR][/FONT]
 
[FONT=Verdana][COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR][/FONT]
[/COLOR][/FONT]
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,716
Members
452,939
Latest member
WCrawford

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