How to use Word VBA to Find Wild Card Matches in Multiple Word Documents and Extract them
There are many situations where a particular format throughout the document that needs to be extracted. The answer would be
Dim sWildCard As String
Dim sDir
Dim oWD As Word.Document
Dim sPath As String
sWildCard = "\[[!\[\]]{1,}\]"
sPath = "C:\Documents\"
sDir = Dir$(sPath & "*.docx", vbNormal)
Do Until LenB(sDir) = 0
Set oWD = Documents.Open(sPath & sDir)
Open "C:\Match_Output.txt" For Append As #1
Selection.HomeKey wdStory, wdMove
Selection.Find.Execute FindText:=sWildCard, MatchWildcards:=True
Do While Selection.Find.Found
Print #1, ActiveDocument.Name & vbTab & Selection.Range.Text
Selection.Range.Collapse wdCollapseEnd
Selection.Find.Execute
Loop
Close #1
oWD.Close False
sDir = Dir$
Loop
End Sub
This code was very helpful, thanks!
ReplyDeleteThank you this helps
ReplyDeleteThanks this was just what I was looking for although Recording macros did help too. Other optional approaches to scanning text for patterns I found was the VBA Like operator (e.g. http://analystcave.com/vba-like-operator/). Works for Excel too
ReplyDeleteHow would you extend the functionality to include footnotes etc
ReplyDeleteYour blog is very nice and provide me informative content thanks for sharing for more information about Open Close Workbook
ReplyDelete