PDA

View Full Version : Solved: Find and replace in text file



austenr
10-04-2006, 11:05 AM
I have no real macro experience in Word, so I am looking for some help. What I want to do is open a .txt file and find a certain string and replace it with a certain string. I would like to have some sort of dialogue box to do this with. For example, the first dialogue box would ask for the file to open. Then after you open it, ask for the date to find and then the date to replace it with. Date format is 20060801 for example. Any help is appreciated. I would do this is a macro recorder but notepad does not support that function.

Norie
10-04-2006, 11:47 AM
I would do this is a macro recorder but notepad does not support that function.
Eh, but Word does.:bug:

fumei
10-04-2006, 01:40 PM
And Word can certainly open txt files.

mvidas
10-05-2006, 05:38 AM
It sure can.

You can also use vba if you really want to, this doesn't have to be in word though:Option Explicit
Private Declare Function GetOpenFileNameB Lib "comdlg32.dll" Alias "GetOpenFileNameA" _
(pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Public Function GetOpenFileName(Optional ByVal vFileFilter As String, Optional ByVal _
vWindowTitle As String, Optional ByVal vInitialDir As String, Optional ByVal _
vInitialFileName As String) As String
Dim OFN As OPENFILENAME, RetVal As Long
OFN.lStructSize = Len(OFN)
OFN.hwndOwner = 0
OFN.hInstance = 0
OFN.lpstrFile = IIf(vInitialDir = "", Space$(254), vInitialDir & Space$(254 - Len(vInitialDir)))
OFN.lpstrInitialDir = IIf(vWindowTitle = "", CurDir, vInitialDir)
OFN.lpstrTitle = IIf(vWindowTitle = "", "Select File", vWindowTitle)
OFN.lpstrFilter = IIf(vFileFilter = "", "All Files (*.*)" & Chr(0) & "*.*", _
Replace(vFileFilter, ",", Chr$(0)))
OFN.nMaxFile = 255
OFN.lpstrFileTitle = Space$(254)
OFN.nMaxFileTitle = 255
OFN.flags = &H81000 'explorer style + filemustexist
RetVal = GetOpenFileNameB(OFN)
If RetVal Then GetOpenFileName = Trim$(OFN.lpstrFile)
End Function
Sub AustenTextFileFindAndReplace()
Dim vFile As String, vFF As Long, tStr As String, FindWhat As String, ReplaceWith As String
vFile = GetOpenFileName()
If Len(vFile) = 0 Then Exit Sub
vFF = FreeFile
Open vFile For Binary As #vFF
tStr = Space$(LOF(vFF))
Get #vFF, , tStr
Close #vFF
FindWhat = InputBox("Please enter string to search for", "Find What")
' Do Until Len(FindWhat) = 0
ReplaceWith = InputBox("Searching for: '" & FindWhat & "'." & vbCrLf & vbCrLf & _
"Please enter string to replace it with", "Replace with")
tStr = Replace(tStr, FindWhat, ReplaceWith)
' FindWhat = InputBox("Please enter string to search for", "Find What")
' Loop
Open vFile For Output As #vFF
Print #vFF, tStr;
Close #vFF
End SubI commented out a section that would let you do multiple find/replaces if you wanted, just comment those out and it will loop until the "Find What" is blank.
Matt

austenr
10-05-2006, 06:00 AM
Thanks Matt

austenr
10-05-2006, 06:07 AM
Having a bit of trouble compiling the code Matt. It errors on the first line.

"Only comments may appear after End Sub, End Function or End Property"

austenr
10-05-2006, 06:10 AM
Never mind. Operator error. :mkay

austenr
10-05-2006, 06:12 AM
Nice job Matt! Thanks. This one is solved.

mvidas
10-05-2006, 06:24 AM
Glad to help! As I said, it isn't word-specific (I actually put it together in outlook vba), but the fact that you're searching for a file, and getting an input for the find and replace, seems kinda odd (when you could search for the file in windows explorer, open notepad, and press control-h for find/replace there). In any case I'm glad it works for you