Consulting

Results 1 to 4 of 4

Thread: Just find, highlight, and specify the file name, no replace

  1. #1
    VBAX Regular
    Joined
    Aug 2017
    Posts
    10
    Location

    Just find, highlight, and specify the file name, no replace

    Hi, guys
    How to make the following code finds for the String in the folder and then color it and specify the names of the files that contain them:
    Thanks in advance
    Public Sub MassReplace()
    
    Dim strPath As String
     Dim strFile As String
    Dim FType As String
    Dim FName As String
    Dim strFind As String
     Dim strReplace As String
     Dim WordApp As Object
     Dim WordDoc As Object
    'The text above defines your objects
    
     strFind = InputBox("Enter Text to find")
    
     strReplace = InputBox("Enter replacement Text")
    ' user defines text they want to find and replace using input boxes
    
        With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show Then
         strPath = .SelectedItems(1)
          Else
             MsgBox "No folder selected!", vbExclamation
             Exit Sub
            End If
     End With
    
      If Right(strPath, 1) <> "\" Then
         strPath = strPath & "\"
         strFile = Dir(strPath & "*.docx*")
     End If
     Application.ScreenUpdating = False
    'The block of code above allows the user to select the folder file to search
    
     Do While strFile <> "" 'Do this while strFile is not blank
    
        Set WordApp = CreateObject("Word.Application") 'Open MS word
        WordApp.Visible = True 'Make word visible
        Set WordDoc = WordApp.Documents.Open(strPath & strFile) 'open file in folder
        WordApp.ActiveDocument.Range.Select ' select all text
    
        With WordApp.Selection.Find 'Using the find function allows a search of text
        .Text = strFind 'find "strFind"
        .Replacement.Text = strReplace 'replacement text is "strReplace"
        .Wrap = wdFindContinue
        '.Format = False
        '.MatchCase = False
        '.MatchWholeWord = False
        '.MatchWildcards = False
        '.MatchSoundsLike = False
        .Execute Replace:=wdReplaceAll 'replace all text
    
        WordApp.ActiveDocument.Close wdSaveChanges 'Close document and save changes
    
        End With 'End with block
    
        WordApp.Quit 'Close the word application
        strFile = Dir 'Go back to the directory
    
       Loop
    
     Application.ScreenUpdating = True
     End Sub

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Where is this code being run from and where is the filename list supposed to go?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular
    Joined
    Aug 2017
    Posts
    10
    Location
    Quote Originally Posted by macropod View Post
    Where is this code being run from and where is the filename list supposed to go?
    Hi, macropod
    Thanks for reply

    The path of the folder containing the files for which the word will be searched, and the list of file names that the search word is likely to be in is: E:\office

    All appreciation for you

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You haven't actually answered my questions...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Tags for this Thread

Posting Permissions

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