View Single Post
 
Old 05-08-2016, 12:22 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Your original code seems to work for the example, though you have commented out one line that should remain. There will never be bold text in a text file. Text files don't support text formatting?

Code:
Sub Find_files_with_string_save_filenames()
    Application.ScreenUpdating = False
    Dim strFolder As String, strFile As String
    Dim wdDoc As Document
    Dim DestFileNum As Long
    Dim sDestFile As String

    strFolder = GetFolder
    If strFolder = "" Then Exit Sub
    strFile = Dir(strFolder & "\*.txt", vbNormal)
    sDestFile = strFolder & "\f9.txt"

    DestFileNum = FreeFile()
    Open sDestFile For Append As DestFileNum

    While strFile <> ""
        Set wdDoc = Documents.Open(Filename:=strFolder & "\" & strFile, AddToRecentFiles:=False, ReadOnly:=True, Visible:=False)
        With wdDoc
            Application.ScreenUpdating = False
            Selection.HomeKey Unit:=wdStory 'what does this do? It starts the search from the start of the document.
            With Selection.Find
                .ClearFormatting
                .Text = "first"
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
            End With
            If Selection.Find.Execute Then
                Print #DestFileNum, wdDoc.Name
            End If
            .Close SaveChanges:=False
        End With
        strFile = Dir()
    Wend
    Close #DestFileNum

    Set wdDoc = Nothing
    Application.ScreenUpdating = True
    MsgBox "Completed...", vbInformation
End Sub

Function GetFolder() As String
Dim oFolder As Object
    GetFolder = ""
    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
    If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
    Set oFolder = Nothing
End Function
If you were working with document files rather than text files then

Code:
While strFile <> ""
        Set wdDoc = Documents.Open(Filename:=strFolder & "\" & strFile, AddToRecentFiles:=False, ReadOnly:=True, Visible:=False)
        With wdDoc
            Application.ScreenUpdating = False
            Selection.HomeKey Unit:=wdStory    'what does this do? It starts the search from the start of the document.
            With Selection.Find
                .ClearFormatting
                'find formatting
                .Font.Name = "Times New Roman"
                .Font.Bold = True
                .Font.Size = "12"
                
                .Text = "first"
                .Forward = True
                .Wrap = wdFindContinue
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
            End With
            If Selection.Find.Execute Then
                Print #DestFileNum, wdDoc.Name
            End If
            .Close SaveChanges:=False
        End With
        strFile = Dir()
    Wend
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote