Editing text files?

-td-shadow

New member
Local time
Today, 08:21
Joined
Feb 11, 2002
Messages
5
Is it possible for VBA to search a text file for a string of characters and then edit that string, without opening the file?

I have an access database that prints out barcodes. The windows driver for the printer duplicates the block of characters for each label it prints, making the printer extremely slow. By printing the label to a file *.prn and editing the quantity value I can speed the printer up to it's intended speed. I want VBA to handle this without user intervention.

Is this at all possible? :confused:

thanks
 
Unless you're planning on totally bypassing the operating system's file handling routines and doing reads and writes to specific disk clusters (not a good idea unless you REALLY know what you're doing and have a compelling reason to do so), the only way to search or edit a disk file is by opening it. If you're not familiar with reading and writing disk files in VBA, review the help screens for the Open, Print#, Write#, Input#, Get, Put and Close statements.
 
Also note the lack of a rewrite statement. The only way to update a portion of a text file is by reading in and writing out every record.
 
These statements are both true. However, it is possible to do what is being asked. You do actually have to open the file. But the person running Access would not know that it is going on.

I didn't write this to be pretty, just to show what you need to do:

Sub replaceInPRN()
Dim wdFile As Word.Document
Set wdFile = GetObject("C:\My Documents\Hello.PRN")

With wdFile.Content.Find
.Execute FindText:="the stuff you don't like", _ ReplaceWith:="", Replace:=wdReplaceAll
End With

wdFile.SaveAs "C:\My Documents\Hello.Doc"
Set wdFile = Nothing

End Sub

This runs Microsoft Word without actually letting the user know that word is being run.

Of course, this works if all you're doing is finding and replacing a string. You of course could do alot more depending on what it is exactly your trying to do.

I would also like to note that when I do run this file, When I open the file in Word my Word Window shows up above and to the left of my viewing screen (all I see is the resize tool in the bottom corner).

Drev
 
I forgot to change this in my post.
Since your using a .prn file instead of a doc file you'll want this line to look like this:

Set wdFile = _
GetObject("C:\My Documents\Hello.prn", "Word.Document")
 

Users who are viewing this thread

Back
Top Bottom