-
Jul 13th, 2006, 08:41 AM
#1
Thread Starter
Hyperactive Member
Open and Clear a text file.
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
-
Jul 13th, 2006, 09:19 AM
#2
Re: Open and Clear a text file.
Originally Posted by Animelion
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.
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Jul 13th, 2006, 09:42 AM
#3
Re: Open and Clear a text file.
if you want to clear a file's contents:
VB Code:
Open "C:\Folder\Text1.txt" For Output As #1: Close #1
-
Jul 13th, 2006, 09:42 AM
#4
Thread Starter
Hyperactive Member
Re: Open and Clear a text file.
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
-
Jul 15th, 2006, 10:37 PM
#5
Addicted Member
Re: Open and Clear a text file.
Originally Posted by bushmobile
if you want to clear a file's contents:
VB Code:
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 I'd like to understand it more
Thanks
-
Jul 15th, 2006, 11:37 PM
#6
Re: Open and Clear a text file.
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
Jul 16th, 2006, 12:23 AM
#7
Addicted Member
Re: Open and Clear a text file.
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
-
Jul 16th, 2006, 12:42 AM
#8
Re: Open and Clear a text file.
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
Jul 16th, 2006, 12:49 AM
#9
Re: Open and Clear a text file.
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.
-
Jul 16th, 2006, 01:06 AM
#10
Re: Open and Clear a text file.
I would think it opens it in the default, shared file mode.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
Jul 16th, 2006, 07:41 AM
#11
Re: Open and Clear a text file.
ah well, I'll shut up then
-
Jul 16th, 2006, 09:00 AM
#12
Re: Open and Clear a text file.
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
-
Jul 16th, 2006, 09:57 AM
#13
Thread Starter
Hyperactive Member
Re: Open and Clear a text file.
How could I have it automatically copy the contents of the text file into the clipboard ?
-
Jul 16th, 2006, 10:05 AM
#14
Re: Open and Clear a text file.
VB Code:
Open "C:\file.txt" For Input As #1
Clipboard.Clear
Clipboard.SetText Input(LOF(1), #1)
Close #1
-
Jul 16th, 2006, 10:12 AM
#15
Thread Starter
Hyperactive Member
Re: Open and Clear a text file.
Thanks bushmobile. Can I use the semicolon multiple times to make that all in one line of text ?
-
Jul 16th, 2006, 10:18 AM
#16
Re: Open and Clear a text file.
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.
-
Jul 16th, 2006, 04:04 PM
#17
Addicted Member
Re: Open and Clear a text file.
Originally Posted by RobDog888
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
-
Jul 17th, 2006, 09:40 AM
#18
Thread Starter
Hyperactive Member
Re: Open and Clear a text file.
When I user the Clipboard.clear command I get a "Object Required" error. Any more quidance would be helpful.
-
Jul 17th, 2006, 11:11 AM
#19
Re: Open and Clear a text file.
Clipboard is for VB 6. Which Office app are you using?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
Jul 17th, 2006, 11:15 AM
#20
Thread Starter
Hyperactive Member
Re: Open and Clear a text file.
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.
-
Jul 17th, 2006, 11:16 AM
#21
Re: Open and Clear a text file.
Originally Posted by Animelion
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?
-
Jul 17th, 2006, 11:23 AM
#22
Re: Open and Clear a text file.
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
Jul 17th, 2006, 11:41 AM
#23
Thread Starter
Hyperactive Member
Re: Open and Clear a text file.
When I user the Clipboard.clear command I get a "Object Required" error. Any more guidance would be helpful.
-
Jul 17th, 2006, 11:45 AM
#24
Re: Open and Clear a text file.
whoops sorry, that's VB6 yes
As RobDog888 said, you'll need API, something like this:
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 Function
to use:
VB Code:
If CopyFileToClipboard("C:\file.txt") Then
' Copy successful
Else
' Copy failed
End If
-
Jul 17th, 2006, 11:45 AM
#25
Re: Open and Clear a text file.
Yes, use the APIs I posted as the Clipboard obbject is not supported in VBA.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|