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
Printable View
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.Quote:
Originally Posted by Animelion
if you want to clear a file's contents:VB Code:
Open "C:\Folder\Text1.txt" For Output As #1: Close #1
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
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:Quote:
Originally Posted by bushmobile
Thanks
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.
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:
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?Quote:
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 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:
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
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.
I would think it opens it in the default, shared file mode. :)
ah well, I'll shut up then
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
How could I have it automatically copy the contents of the text file into the clipboard ?
VB Code:
Open "C:\file.txt" For Input As #1 Clipboard.Clear Clipboard.SetText Input(LOF(1), #1) Close #1
Thanks bushmobile. Can I use the semicolon multiple times to make that all in one line of text ?
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.
Thanks for the info and the link Rob. Really usefull info...and relatively easy to understand :thumb:Quote:
Originally Posted by RobDog888
When I user the Clipboard.clear command I get a "Object Required" error. Any more quidance would be helpful.
Clipboard is for VB 6. Which Office app are you using?
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.
You will get that error when trying to use Clipboard.Clear in VBA, but it will work just fine in VB6.Quote:
Originally Posted by Animelion
Which are you using?
Then you will probably need to use some Window APIs to do the trick.
VB Code:
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
When I user the Clipboard.clear command I get a "Object Required" error. Any more guidance would be helpful.
whoops sorry, that's VB6 yes :blush:
As RobDog888 said, you'll need API, something like this:to use:VB Code:
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 FunctionVB Code:
If CopyFileToClipboard("C:\file.txt") Then ' Copy successful Else ' Copy failed End If
Yes, use the APIs I posted as the Clipboard obbject is not supported in VBA.