![]() |
|
|
|
#1
|
||||
|
||||
|
The following macro runs a Word session so that underlined content in Word documents in the selected folder can be extracted from all documents in that folder. Code:
Sub GetWordData()
'Note: this code requires a reference to the Word object model,
'added via Tools|References in the Excel VBE
Application.ScreenUpdating = False
Dim wdApp As New Word.Application
Dim wdDoc As Word.Document
Dim StrFolder As String, StrFile As String
Dim WkSht As Worksheet, i As Long, j As Long
StrFolder = GetFolder
If StrFolder = "" Then Exit Sub
Set WkSht = ActiveSheet
i = WkSht.Cells(WkSht.Rows.Count, 1).End(xlUp).Row
StrFile = Dir(StrFolder & "\*.docx", vbNormal)
While StrFile <> ""
Set wdDoc = wdApp.Documents.Open(Filename:=StrFolder & "\" & StrFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Wrap = wdFindStop
.Forward = True
.Format = True
.Font.Underline = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
i = i + 1
WkSht.Cells(i, 1).Value = StrFile
WkSht.Cells(i, 2).Value = .Text
If .End = wdDoc.Range.End Then Exit Sub
.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 |
Copy/paste from Excel to Word problems!
|
mross127 | Word | 10 | 08-16-2017 04:41 PM |
| Copy Text Twice to Paste into word | Albundy | Word | 2 | 09-02-2016 12:59 PM |
Copy/Paste EXCEL cells as pic in WORD
|
A_Lau | Drawing and Graphics | 3 | 12-19-2014 06:57 AM |
Copy Paste Serial No to Excel in Text format
|
linan123 | Excel | 1 | 05-02-2014 07:50 PM |
| copy/paste charts from excel to word | bielak01 | Excel | 0 | 04-16-2009 02:27 AM |