Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-09-2016, 07:36 AM
equalizer88 equalizer88 is offline word vba Search files for string and save found filenames to new textfile Windows 7 32bit word vba Search files for string and save found filenames to new textfile Office 2010 32bit
Novice
word vba Search files for string and save found filenames to new textfile
 
Join Date: Jul 2015
Posts: 15
equalizer88 is on a distinguished road
Default

Ok, figured out problem was difference between Select and Range properties. I was missing the wdDoc.Select inside the With wdDoc loop. That is why the search was not looking in external files.



So why does the Range Object With .Range.Find not require specifying the external file? (Such as wdDoc.Range)



Code:
    While strFile <> ""
        Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, ReadOnly:=True, Visible:=False)
        With wdDoc
            Application.ScreenUpdating = False
'  **************************
 wdDoc.Select 'this was missing line 
' ***************************
     Selection.HomeKey Unit:=wdStory     ' It starts the search from the start of the document.
            'With .Range.Find 
' ****************************************
' if .Range.Find is used then no selection of WdDoc is required
'*****************************************
            With Selection.Find
              '.ClearFormatting
              .Forward = True
              .Wrap = wdFindContinue
              .Format = False
              .MatchCase = False
              .MatchWholeWord = False
              .MatchWildcards = False
             '.MatchSoundsLike = False
              .MatchAllWordForms = False
              .Text = "first"
              .Execute 'Replace:=wdReplaceAll
          'If .Found = True Then
            If Selection.Find.Found = True Then
                Print #DestFileNum, wdDoc.Name
            End If
           End With
        .Close SaveChanges:=False
        End With
        strFile = Dir()
    Wend
 ......
Reply With Quote
Reply

Tags
vba word search



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Word - Search Within Files Containing A String - Copy Files to New Folder jc491 Word VBA 0 01-09-2016 12:00 PM
word vba Search files for string and save found filenames to new textfile search for string, wherever found, at end of line, insert 8 <cr>s sbktex Word VBA 2 09-17-2014 04:09 PM
word vba Search files for string and save found filenames to new textfile Way to search for a string in text file, pull out everything until another string? omahadivision Excel Programming 12 11-23-2013 12:10 PM
word vba Search files for string and save found filenames to new textfile Macro to create new word doc and save the file using String found in the document VBNation Word VBA 2 02-08-2013 07:14 AM
word vba Search files for string and save found filenames to new textfile Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:42 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft