![]() |
|
|
|
#1
|
||||
|
||||
|
Since you want to get a whole folder full of data, all on different worksheets, this would be better with an Excel macro instead of a Word macro. Simply add the following macro to an Excel workbook, then run it. Note that the code requires a reference to the Word object model. To do this in the VBE, choose Tools|References then scroll down to the Microsoft Word entry and check it.
Code:
Sub GetWordLists()
'Note: this code requires a reference to the Word object model
Application.ScreenUpdating = False
Dim wdApp As New Word.Application
Dim wdDoc As Word.Document
Dim strFolder As String, strFile As String
Dim WkBk As Workbook, WkSht As Worksheet, i As Long
strFolder = GetFolder
If strFolder = "" Then Exit Sub
Set WkBk = ThisWorkbook
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
Set WkSht = WkBk.Sheets.Add
i = 1
WkSht.Cells(i, 1) = strFolder & "\" & strFile
WkSht.Cells(i, 2) = "Page"
WkSht.Name = Split(strFile, ".doc")(0)
Set wdDoc = wdApp.Documents.Open(Filename:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ChrW(&H649)
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
i = i + 1
WkSht.Cells(i, 1) = Trim(.Duplicate.Words.First.Text)
WkSht.Cells(i, 2) = .Duplicate.Information(wdActiveEndAdjustedPageNumber)
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
.Close SaveChanges:=False
End With
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Find specific rows then copy and paste to new doc
|
konopca | Word VBA | 5 | 02-20-2014 02:34 PM |
How to allow specific double words in spellcheck
|
Wyndham | Word | 5 | 02-06-2014 10:44 AM |
Need VBA For Macro On How To Remove Specific Words
|
netchie | Word VBA | 6 | 08-28-2012 03:37 PM |
Macro to copy specific columns in Excel from another spreadsheet
|
KD999 | Excel Programming | 1 | 07-20-2012 08:58 AM |
Macro for highlighting specific number of words
|
icsjohn | Word VBA | 2 | 12-07-2011 06:44 PM |