Option Explicit _____________________________________________________________________________________ Private Sub EditFile() ' button on FindReplace sheet EditFileSub Range("FindPath") End Sub _____________________________________________________________________________________ Private Sub ViewFile() ' button on FindReplace sheet EditFileSub Range("FindPath"), True End Sub _____________________________________________________________________________________ Sub EditFileOnCursorRow() ' button on FindReplace sheet. sheet must be ActiveSheet to run ' also run with Ctrl-R EditFileSub Range("B" & GetCursorRow) End Sub _____________________________________________________________________________________ Private Sub ViewHtmOnCursorRow() ' button on FindReplace sheet EditFileSub Range("B" & GetCursorRow), True End Sub _____________________________________________________________________________________ Function GetCursorRow() As Integer ' returns row number of cursor, if valid cursor row and input exists in Filename column Dim Row As Integer Row = ActiveCell.Row ' cursor is in the input section If Row < Range("FindUL").Row Then MsgBox "Your cursor is not in the Output Section.", vbCritical, "Cursor Too Far Up" End End If ' is cursor on row with filename in Column B? If Cells(Row, 2).Value = "" Then MsgBox "There is no filename in Column (B) for that row.", vbCritical, "No Filename Found" End End If GetCursorRow = Row End Function _____________________________________________________________________________________ Private Sub EditFileSub(InputLoc As Range, Optional Viewflag As Boolean) ' sub to many buttons on FindReplace sheet Dim FileFlag As Boolean Dim i As Integer Dim FileName As String, IndName As String FileName = InputLoc.Value ' start by checking that something was entered If Len(FileName) = 0 Then Range("FindPath").Select MsgBox "Must enter Filename!", vbCritical, "No File to Edit/View!" End End If ' then check if file extension is html editable FileFlag = IsPathFile(FileName, Viewflag) If Not FileFlag Then 'if a path, check if index file exists within the folder If FolderExists(FileName) Then IndName = GetIndexFileName(FileName) If Len(IndName) = 0 Then GoTo NoIndexFile Else FileName = FileName & "\" & IndName GoTo EditingIndex End If Else NoIndexFile: Range("FindPath").Select If Viewflag Then MsgBox "Filename is not usually a browser viewable file.", vbCritical, "Not Valid File!" Else MsgBox "Filename is not a text editable file.", vbCritical, "Not Valid Filename!" End If End End If End If ' finally check if file exists If Not FileExists(FileName) Then Range("FindPath").Select MsgBox "File does not exist.", vbCritical, "Not Valid Filename!" End End If ' now we can edit EditingIndex: If Viewflag Then If Right(FileName, 3) = "htm" Or Right(FileName, 4) = "html" Or Right(FileName, 5) = "shtml" Then ActiveWorkbook.FollowHyperlink FileName Else MsgBox "You can only view HTML files. Use the Edit Button.", vbExclamation, "Sorry" End If Else CallNotepad FileName End If End Sub _____________________________________________________________________________________ Private Sub RunFind() ' button on FindReplace sheet Dim bool As Boolean Dim Ext As String Ext = Range("FindSearchExt").Value bool = IsPathFile(Range("FindPath").Value) If bool And Ext = "filenames" Then MsgBox "You can not search for filenames when you have entered a File Name.", vbCritical, "Aborted!" End ElseIf bool Then FindReplace "" Else If Ext = "htm+txt" Then FindReplace "txt" FindReplace "htm", , True Else FindReplace Ext End If End If End Sub _____________________________________________________________________________________ Private Sub RunReplace() ' button on FindReplace sheet Dim i As Integer Dim Ext As String, FindReplType As String, ReplString As String Ext = Range("FindSearchExt").Value If Ext = "filenames" Then MsgBox "There is no replace yet for filenames. However, on the ImageUtils tab there is a facility to change the beginning of filenames.", vbCritical, "Aborted!" Exit Sub End If ReplString = Range("FindReplaceString").Value FindReplType = Range("FindReplType").Value If Len(ReplString) = 0 Then If FindReplType = "Replace" Then Range("FindReplaceString").Select i = MsgBox("There is no Replace String. Continuing will delete the Find String. Are you sure?", vbYesNo, "Confirmation Wanted") If i <> vbYes Then Exit Sub Else Range("FindReplaceString").Select MsgBox "When inserting text, you can't insert nothing!", vbCritical, "Nothing to Insert" Exit Sub End If End If If IsPathFile(Range("FindPath").Value) Then FindReplace "", ReplString Else If Ext = "htm+txt" Then FindReplace "txt", ReplString FindReplace "htm", ReplString, True Else FindReplace Ext, ReplString End If End If End Sub _____________________________________________________________________________________ Private Sub FindReplace(ByVal Ext As String, Optional ReplString As Variant, Optional NoClearFlag As Boolean) ' sub routine to Find and Replace routines ' -> ideally this uses Controls, so labels and colors of checkboxes change based on selections Dim CaseInsensFlag As Boolean, NotClearFlag As Boolean, RecurFlag As Boolean, ReplFlag As Boolean Dim Ctr(1) As Integer, i As Integer, j As Integer, t As Integer Dim PrintRow As Long Dim f As Object, fls As Object, P As Object Dim D As String, f2 As String, Path As String, FindReplType As String, FindString As String, RP As String, SP As String Dim FileList, FL, Locs As Variant ' we restore the yellow background color of the input fields, as pasting from the web clears it RestoreInputCells "FindPath" RestoreInputCells "FindFindString" RestoreInputCells "FindReplaceString" Ctr(0) = 0 Ctr(1) = 0 PrintRow = Range("FindUL").Row NotClearFlag = Range("FindNotClearFlag").Value If NoClearFlag Or NotClearFlag Then PrintRow = WorksheetFunction.Max(Sheets("FindReplace").Range("A65536").End(xlUp).Row + 1, PrintRow) Else ClearFindReplace End If Application.ScreenUpdating = False Path = Range("FindPath").Value ' we test for special feature If UCase(Left(Path, 8)) = "TEMPLIST" Then f2 = ReturnTopPath & "\TempList.txt" If FileExists(f2) Then FileList = ReturnFileAsArrayOfRows(f2) Else MsgBox "No TempList.txt found.", vbCritical, "Aborted" End End If ElseIf Len(Path) = 0 Then Range("FindPath").Select MsgBox "Must enter Path or Filename!", vbCritical, "No Path!" End ElseIf Not FileExists(Path) Then Range("FindPath").Select MsgBox "Path or Filename does not exist.", vbCritical, "Not Valid Path!" End End If FindString = Range("FindFindString").Value If Len(FindString) = 0 Then Range("FindFindString").Select MsgBox "Must enter Find String.", vbCritical, "No Find String!" End End If RecurFlag = Range("FindRecursiveFlag").Value CaseInsensFlag = Range("FindCaseInsens").Value If IsMissing(ReplString) Then ' --> this s/b FindReplType="" ' -> replace Variant in header ReplFlag = False ReplString = "" Else ' FindReplType = Replace, Insert After, Insert Before FindReplType = Range("FindReplType").Value ReplFlag = True End If If FindString = ReplString And FindReplType = "Replace" Then Range("FindFindString").Select MsgBox "It doesn't make sense for the Replace string to be the same as the Find string.", vbCritical, "Can't Be Same!" End End If If CaseInsensFlag And FindReplType = "Replace" Then MsgBox "It doesn't make sense to Replace with case insensitive.", vbCritical, "Must Uncheck!" End End If ' if paths were passed in we get the text If Range("InpTypeFind").Value = 2 Then SP = UCase(FindString) If FileExists(FindString) Then FindString = ReadInFile(FindString) Else Range("FindFindString").Select MsgBox "File not found. Did you mean input type to be String?", vbCritical, "Aborted!" End End If Else SP = "" End If RP = "" If ReplFlag Then RP = UCase(ReplString) If Range("InpTypeRepl").Value = 2 Then If FileExists(ReplString) Then ReplString = ReadInFile(ReplString) Else Range("FindReplaceString").Select MsgBox "File not found. Change input type to String?", vbCritical, "Aborted!" End End If End If End If ' capitalize when non-case search If CaseInsensFlag Then FindString = UCase(FindString) Application.ScreenUpdating = False ' ~~~~~ search through filenames ~~~~~ ' --> could have option to also search paths ' --> could do file renaming here If Ext = "filenames" Then If RecurFlag Then FileList = FindPaths(Path) Else FileList = Array(Path) End If For Each P In FileList Set fls = CreateObject("Scripting.FileSystemObject").GetFolder(P).Files For Each f In fls If CaseInsensFlag Then f2 = UCase(f) Else f2 = f End If If 0 < InStr(ExtractFilename(f2), FindString) Then Sheets("FindReplace").Cells(PrintRow, 1).Value = "Found:" Sheets("FindReplace").Cells(PrintRow, 2).Value = f PrintRow = PrintRow + 1 Ctr(1) = Ctr(1) + 1 End If Ctr(0) = Ctr(0) + 1 Next f Next P Sheets("FindReplace").Cells(PrintRow, 1).Value = Ctr(1) & " files out of " & Ctr(0) Exit Sub End If ' ~~~~~ processing inside files ~~~~~ ' get list of files that we will load and look through If IsEmpty(FileList) Then If IsPathFile(Path) Then FileList = Array(Path) Else DoEvents Application.StatusBar = "Getting all filenames..." FileList = ListFiles(Path, Ext, RecurFlag, True) If IsEmpty(FileList) Then Application.StatusBar = False Sheets("FindReplace").Cells(PrintRow, 1).Value = "No " & Ext & " files found to process" Exit Sub End If End If End If ' ~~ loop for files For Each FL In FileList If SP = UCase(FL) Then Sheets("FindReplace").Cells(PrintRow, 1).Value = "Skipped source:" Sheets("FindReplace").Cells(PrintRow, 2).Value = FL PrintRow = PrintRow + 1 ElseIf RP = UCase(FL) Then Sheets("FindReplace").Cells(PrintRow, 1).Value = "Skipped replace:" Sheets("FindReplace").Cells(PrintRow, 2).Value = FL PrintRow = PrintRow + 1 Else DoEvents Application.StatusBar = "Getting " & FL & "..." D = ReadInFile(FL) If CaseInsensFlag Then D = UCase(D) ' ~~ first handle the cases where we are replacing If ReplFlag Then If 0 < InStr(D, FindString) Then Locs = AllInStr(D, FindString) ' insert in front of, or insert after If FindReplType = "Insert After" Or FindReplType = "Insert Before" Then For j = 0 To UBound(Locs) t = Locs(UBound(Locs) - j) - 1 If FindReplType = "Insert After" Then t = t + Len(FindString) D = Left(D, t) & ReplString & DropStr(D, t) Next j ' find and replace Else D = Replace(D, FindString, ReplString) End If ' save and document SaveFile FL, D Sheets("FindReplace").Cells(PrintRow, 1).Value = "Processed: " Sheets("FindReplace").Cells(PrintRow, 2).Value = FL PrintRow = PrintRow + 1 Ctr(0) = Ctr(0) + 1 End If ' ~~ find string and display filename and count Else If Len(D) = 0 Then Sheets("FindReplace").Cells(PrintRow, 1).Value = "Empty File: " Sheets("FindReplace").Cells(PrintRow, 2).Value = FL PrintRow = PrintRow + 1 Else If 0 < InStr(D, FindString) Then t = UBound(AllInStr(D, FindString)) + 1 Sheets("FindReplace").Cells(PrintRow, 1).Value = t & " times" Sheets("FindReplace").Cells(PrintRow, 2).Value = FL PrintRow = PrintRow + 1 Ctr(0) = Ctr(0) + 1 Ctr(1) = Ctr(1) + t End If End If End If End If Next FL ' ~~~ print summary If ReplFlag Then f2 = Ctr(0) & " " & Ext & " files processed" Else If Ext = "" Then f2 = "the file" Else f2 = Ctr(0) & " of the " & Ext & " files" End If f2 = Ctr(1) & " hits in " & f2 & " searched" End If If Ext <> "" Then f2 = f2 & " - Total " & Ext & " files checked: " & Format(UBound(FileList) + 1, "#,##0") End If Sheets("FindReplace").Cells(PrintRow, 1).Value = "-- " & f2 If ActiveCell.Row > 20 Then Application.Goto Range("FindUL"), True End If Application.StatusBar = False End Sub _____________________________________________________________________________________ Sub ClearFindReplace() ' is button on FindReplace sheet Sheets("FindReplace").Range("A" & Range("FindUL").Row & ":B65536").Clear Application.Goto Range("FindUL"), True Application.ScreenUpdating = False End Sub _____________________________________________________________________________________ Private Sub ClearFindReplaceAll() ' is button on FindReplace sheet ClearFindReplace Range("FindPath").Value = "" Range("FindFindString").Value = "" Range("FindReplaceString").Value = "" Application.Goto Range("A1"), True Range("FindPath").Select End Sub _____________________________________________________________________________________ Private Sub RemoveLinkBlankRowsFind() ' is button on FindReplace sheet RemoveLinkBlankRows "FindUL", 2 End Sub _____________________________________________________________________________________ Private Sub PutFilesIntoTempList() ' is button on FindReplace sheet Dim EndRow As Integer, i As Integer, StartRow As Integer Dim f As String, H As String f = ReturnTopPath & "\TempList.txt" StartRow = Range("FindUL").Row With Sheets("FindReplace") EndRow = .Range("B65536").End(xlUp).Row If EndRow >= StartRow Then For i = StartRow To EndRow H = H & .Cells(i, 2).Value & vbCrLf Next i SaveIfChanged f, H End If End With End Sub _____________________________________________________________________________________ Private Sub ViewTempList() ' is button on FindReplace sheet Dim f As String f = ReturnTopPath & "\TempList.txt" If FileExists(f) Then CallNotepad f Else MsgBox "The TempList.txt file does not exist.", vbCritical, "No File to View" End If End Sub _____________________________________________________________________________________ Private Sub KillTempList() ' is button on FindReplace sheet Kill ReturnTopPath & "\TempList.txt" End Sub _____________________________________________________________________________________