Click to See Complete Forum and Search --> : Open and Clear a text file.
Animelion
Jul 13th, 2006, 08:41 AM
How can I open C:\Folder1\Text1.txt and erase all it's contents and then save it. It does not need to visually open, but I just want to erase all of it's contents. ~Bryan
Shuja Ali
Jul 13th, 2006, 09:19 AM
How can I open C:\Folder1\Text1.txt and erase all it's contents and then save it. It does not need to visually open, but I just want to erase all of it's contents. ~Bryan
What are you trying to do? Just Delete the File.
bushmobile
Jul 13th, 2006, 09:42 AM
if you want to clear a file's contents:Open "C:\Folder\Text1.txt" For Output As #1: Close #1
Animelion
Jul 13th, 2006, 09:42 AM
I have a text file that I am recording information into, however I want the user to be able to click a button to wipe the text file clean. So I am not deleating the text file, but just trying to clear all the text in it and then have it save. ~Thanks Bryan
malik641
Jul 15th, 2006, 10:37 PM
if you want to clear a file's contents:Open "C:\Folder\Text1.txt" For Output As #1: Close #1
Hey bushmobile, could you walk me through this single line of code please? I'm amazed at how simplified this is :eek: I'd like to understand it more :rolleyes:
Thanks
RobDog888
Jul 15th, 2006, 11:37 PM
Its actually 2 lines of code. The colon is a line break/separater so to speak.
The first part
"Open "C:\Folder\Text1.txt" For Output As #1"
Opens the file in Output mode which will clear the file contents if it already exists.
"Close #1"
Closes the file and releases.
malik641
Jul 16th, 2006, 12:23 AM
Thanks RobDog
Well....it's 2 instructions on a single line, right? :)
When does it save the contents of the empty text file? At the close portion? Is that standard with the Close statement?
And I was doing some testing with this....and reading the help file about this...and it said:
If the file is already opened by another process and the specified type of access is not allowed, the Open operation fails and an error occurs.
And I'm not sure I understand this correctly. I had the text file open with text in it (saved, by the way) and I ran the code to try to get an error, but couldn't. So I assume that it is because of the type of access that it has makes the error not occur, correct? And what type of access is this exactly? What kind of access would cause an error?
And I also noticed that if I ran the code with the text file already opened I can open the text file again and a new instance of the text file opens with blank text. And if I save the text file instance that has text in it and close both instances and re-open, the text is (as expected) still there....but why didn't it ask me to replace the file? And why am I aloud to open multiple instances of notepad?
....maybe I just don't understand notepad correctly :rolleyes:
RobDog888
Jul 16th, 2006, 12:42 AM
Yes, 2 lines of code (instructions) on a single line. There are several Modes for files to be opened with. You can specify Shared or Exclusive etc. If you dont specify it then its shared and opening the file in Notepad and via code will not generate an error. Yes, when you close the file its an implied save operation.
Here is one tutorial on File I/O - http://vbforums.com/showthread.php?t=405051
bushmobile
Jul 16th, 2006, 12:49 AM
notepad doesn't open files directly - it opens copies of them - that's why you can open a file in VB while it's open in notepad (and why you can open multiple copies of the same file in notepad - they're all actually separate files) - at least i think that's what it does.
RobDog888
Jul 16th, 2006, 01:06 AM
I would think it opens it in the default, shared file mode. :)
bushmobile
Jul 16th, 2006, 07:41 AM
ah well, I'll shut up then
westconn1
Jul 16th, 2006, 09:00 AM
i just assumed it opened the file read into memory (text box) and closed it, reopen to save etc. unlike word that holds the file open until it is the document is closed.
pete
Animelion
Jul 16th, 2006, 09:57 AM
How could I have it automatically copy the contents of the text file into the clipboard ?
bushmobile
Jul 16th, 2006, 10:05 AM
Open "C:\file.txt" For Input As #1
Clipboard.Clear
Clipboard.SetText Input(LOF(1), #1)
Close #1
Animelion
Jul 16th, 2006, 10:12 AM
Thanks bushmobile. Can I use the semicolon multiple times to make that all in one line of text ?
bushmobile
Jul 16th, 2006, 10:18 AM
you can, but don't - it'll make your code virtually impossible to read / debug easily.
the only reason I did it like that in post #3 is because the Close #1 bit could almost be considered a byproduct of the Opening (and it didn't effect the readability). If you're opening a file and then doing stuff with it then you should put everything on separate lines - it doesn't change the number of lines of code that are actually executed.
malik641
Jul 16th, 2006, 04:04 PM
Yes, 2 lines of code (instructions) on a single line. There are several Modes for files to be opened with. You can specify Shared or Exclusive etc. If you dont specify it then its shared and opening the file in Notepad and via code will not generate an error. Yes, when you close the file its an implied save operation.
Here is one tutorial on File I/O - http://vbforums.com/showthread.php?t=405051
Thanks for the info and the link Rob. Really usefull info...and relatively easy to understand :thumb:
Animelion
Jul 17th, 2006, 09:40 AM
When I user the Clipboard.clear command I get a "Object Required" error. Any more quidance would be helpful.
RobDog888
Jul 17th, 2006, 11:11 AM
Clipboard is for VB 6. Which Office app are you using?
Animelion
Jul 17th, 2006, 11:15 AM
I am running office 2003 on the computer, but it's not an office app, it's a macro recorder in IBM's WRQ reflections. The macro runs on VBA. Hope that helps you.
Hack
Jul 17th, 2006, 11:16 AM
When I user the Clipboard.clear command I get a "Object Required" error. Any more quidance would be helpful.You will get that error when trying to use Clipboard.Clear in VBA, but it will work just fine in VB6.
Which are you using?
RobDog888
Jul 17th, 2006, 11:23 AM
Then you will probably need to use some Window APIs to do the trick.
Private Declare Function EmptyClipboard Lib "user32.dll" () As Long
Private Declare Function GetClipboardData Lib "user32.dll" (ByVal wFormat As Long) As Long
Private Declare Function SetClipboardData Lib "user32.dll" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Animelion
Jul 17th, 2006, 11:41 AM
When I user the Clipboard.clear command I get a "Object Required" error. Any more guidance would be helpful.
bushmobile
Jul 17th, 2006, 11:45 AM
whoops sorry, that's VB6 yes :blush:
As RobDog888 said, you'll need API, something like this:Option Explicit
Private Declare Function GlobalLock Lib "kernel32" ( _
ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" ( _
ByVal hMem As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32" ( _
ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function OpenClipboard Lib "User32" ( _
ByVal hwnd As Long) As Long
Private Declare Function SetClipboardData Lib "User32" ( _
ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function CloseClipboard Lib "User32" () As Long
Private Declare Function EmptyClipboard Lib "User32" () As Long
Private Declare Function lstrcpy Lib "kernel32" ( _
ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
Private Const GHND = &H42
Private Const CF_TEXT = 1
Private Function CopyFileToClipboard(ByVal sPath As String) As Boolean
Dim hGlobalMemory As Long, lpGlobalMemory As Long
Dim sText As String
Open sPath For Input As #1
sText = Input(LOF(1), #1)
Close #1
hGlobalMemory = GlobalAlloc(GHND, Len(sText) + 1)
lpGlobalMemory = GlobalLock(hGlobalMemory)
lpGlobalMemory = lstrcpy(lpGlobalMemory, sText)
If GlobalUnlock(hGlobalMemory) = 0 Then
If OpenClipboard(0&) Then
EmptyClipboard
SetClipboardData CF_TEXT, hGlobalMemory
If CloseClipboard Then CopyFileToClipboard = True
End If
End If
End Functionto use:If CopyFileToClipboard("C:\file.txt") Then
' Copy successful
Else
' Copy failed
End If
RobDog888
Jul 17th, 2006, 11:45 AM
Yes, use the APIs I posted as the Clipboard obbject is not supported in VBA.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.