Loop through Word documents from Excel
Dear Raj
Please try the below and feedback. Set reference to Microsoft Word Object
Library from VBE Tools|References Dialog Box.
Sub SearchStringinWord()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim strFolder As String
Dim strFile As String
Dim strSearchString As String
Dim lngRow As Long
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = False
strFolder = "c:\"
strSearchString = "jacob"
strFile = Dir("c:\*.doc", vbNormal)
Do While strFile < ""
Set wrdDoc = wrdApp.Documents.Open(strFolder & strFile)
If InStr(1, wrdDoc.Range.Text, strSearchString, vbTextCompare) 0 Then
lngRow = lngRow + 1
ActiveSheet.Range("A" & lngRow).Formula = "=HYPERLINK(""" & strFolder &
strFile & """,""" & strFile & """)"
End If
wrdDoc.Close
strFile = Dir
Loop
Set wrdDoc = Nothing
wrdApp.Quit
Set wrdApp = Nothing
End Sub
If this post helps click Yes
---------------
Jacob Skaria
"Raj" wrote:
Hi,
A folder has several word documents. These need to be opened from
Excel and checked for existence of a string. If the string exists, the
name of the file needs to be written in the Excel workbook with a
hyperlink to the word document.
Please help with sample code/pointers.
Thanks in Advance for the help.
Regards,
Raj
|