Results 1 to 20 of 24

Thread: Finding text strings in Word from an excel list of text strings

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Yes, it seems more difficult than it needs to be...
    If you apply Find to a range (like the whole content of a doc), it returns a boolean but modifies the selection to the found string.
    If you use the selection object to get the position, it needs to be referred to from the document window.
    Well, this seems to work:
    (I've used the filesystemobject to go through the files in the folder so you'll need a reference to the Microsoft Sctipting Runtime as well as Microsoft Word. And I've named the range of cells "partnumbers")
    [vba]Sub Main()

    Const TARGET_FOLDER_PATH As String = "C:\TEMP\"

    Dim fso As FileSystemObject
    Dim oTargetFolder As Folder
    Dim f As File

    Dim appWD As Word.Application
    Dim docSource As Word.Document
    Dim oSearchRange As Word.Range

    Dim rngPartnumber As Range

    Set fso = New FileSystemObject
    Set oTargetFolder = fso.GetFolder(TARGET_FOLDER_PATH)

    Set appWD = New Word.Application
    For Each rngPartnumber In Range("partnumbers")
    For Each f In oTargetFolder.Files
    Set docSource = appWD.Documents.Open(TARGET_FOLDER_PATH & f.Name)
    Set oSearchRange = docSource.Content
    With oSearchRange.Find
    .ClearFormatting
    .MatchWholeWord = True
    .Text = rngPartnumber.Text
    If .Execute Then
    docSource.Range(docSource.Paragraphs(1).Range.Start, _
    oSearchRange.End).Select
    rngPartnumber.Offset(0, 1).Value = f.Name & " Page: " & _
    appWD.ActiveWindow.Selection.Information(wdActiveEndPageNumber) _
    & " Para: " & appWD.ActiveWindow.Selection.Paragraphs.Count

    End If
    End With
    docSource.Close False
    Next f
    Next rngPartnumber
    appWD.Quit False

    End Sub[/vba]
    Last edited by Killian; 03-01-2006 at 05:12 AM. Reason: took out an unused variable
    K :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •