Can I use Word as a programmable text editor?

G

Guest

Hi all

I use a flat file text editor - TextPad (Textpad.com) - to edit my text
files. But I've come to a situation where I need to programmatically load
some text files, search for a value and globally replace to a new value, then
save them out. Unfortunately, TextPad does not have a built-in fullly
functionable programming language. So I can't automate this with TextPad.

Can I use Word to load TXT files, do the edits, and save as TXT files in VBA?

By the way, anyone know of a text editor that has VBA as its built-in
programming language? Just curious.

Thanks
 
G

Guest

The following SaveAs statement save the current file as an unformatted text
file

ActiveDocument.SaveAs FileName:="TextFile.txt", Fileformat:=wdFormatText
 
G

Guest

Sure you can. This code is written by one of MVP's I just made a few minor
change.

'**************
Public Sub BatchReplaceAll()

Dim FirstLoop As Boolean
Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim Response As Long

PathToUse = "C:\" <-- write the correct path for the folder with .txt files

On Error Resume Next

FirstLoop = True

myFile = Dir$(PathToUse & "*.txt")

While myFile <> ""

'Open document
Set myDoc = Documents.Open(PathToUse & myFile)

If FirstLoop Then

Dialogs(wdDialogEditReplace).Show

FirstLoop = False

Response = MsgBox("Do you want to process " & _
"the rest of the files in this folder", vbYesNo)
If Response = vbNo Then Exit Sub

Else


With Dialogs(wdDialogEditReplace)
.ReplaceAll = 1
.Execute
End With

End If


myDoc.Close SaveChanges:=wdSaveChanges

'Next file in folder

myFile = Dir$()

Wend

End Sub

'************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top