By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,474 Members | 966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,474 IT Pros & Developers. It's quick & easy.

Open wordpad/notepad file

P: n/a
Thank you in advance for any and all assistance. It is greatly appreciated.

I would like a command button in VB.Net 2002 to open a text file, then to allow
text from a textbox on the current form to be appended to that text file. Is
there a place where I can see code to do this?

I have the data/text on the clipboard using the clipboard class, now I want to
append it to the text file either in Notepad or Wordpad. I have tried using the
StreamWriter and get that it's undefined under the Private_Sub Function of the
command button. What am I doing wrong?

Michael
Nov 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
* lu*************@aol.com (Lumpierbritches) scripsit:
I would like a command button in VB.Net 2002 to open a text file, then to allow
text from a textbox on the current form to be appended to that text file. Is
there a place where I can see code to do this?
\\\
Dim p As Process = Process.Start("notepad")
Try
AppActivate(p.Id)
Catch
End Try
Clipboard.SetDataObject("Hallo Welt") ' Text in Clipboard setzen.
SendKeys.Send("^v") ' Strg+V auslösen.
///
I have the data/text on the clipboard using the clipboard class, now I want to
append it to the text file either in Notepad or Wordpad. I have tried using the
StreamWriter and get that it's undefined under the Private_Sub Function of the
command button. What am I doing wrong?


You will have to import the 'System.IO' namespace ('Imports System.IO'
on top of the file). Notice that this won't make any sense if you want
to append the text using Notepad or Wordpad.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2

P: n/a
How do you consider this is a solution. I have no idea what you're talking
about. I just wanted to know, in code, how to open a text file and append text
from a field on a form, using the clipboard paste, from a command
button>?>>>>>????

Michael
Nov 20 '05 #3

P: n/a

"Lumpierbritches" <lu*************@aol.com> wrote in message
news:20***************************@mb-m21.aol.com...
How do you consider this is a solution. I have no idea what you're talking about. I just wanted to know, in code, how to open a text file and append text from a field on a form, using the clipboard paste, from a command
button>?>>>>>????

Michael


It is the colution you asked for. You asked how to open a notepad/wordpad
file, then vaguel amended it to open a text file. I believe what you are
looking foris reading/writing to a text file. You are correct in using a
StreamReader/StreamWriter, but, if it's not defined, look at where you
defined it in code. You must always be aware of the scope of your object
declerations. Consider the following:

Private Sub Main
Dim SR As StreamReader
End Sub

Private Sub WriteFile(ByVal Input As String)
SR = New StreamReader("C:\somefile.txt")
MyText = SR.ReadToEnd
SR.Close
End Sub

You will get your Undefined Error because WriteFile sub has no clue what
SR is because it's declared in Main sub. This has never changed in any
language I know of, but, under VB6, it would have been made a varient unless
you had Option Explicit on, which is default under .NET, then you'd get an
Unefined Error. The two solutions are to either define SR as a FormLevel
object, by this I mean outside of any procedure, or as a Procedure level
object. Now, something to consider, FormLevel objects always exist in
memory as long as the form does, so, if you only need it once or twice, make
it procedure level and the GarbageCollector will gett ride of it when you
don't need it and thus free up resources for other objects. NOTE: my
discription of the Garbage Collector is not entirely accurate, but the gist
is correct.
So, IMHO, try tthis code:

Private Sub WriteFile(ByVal Input As String)
Dim MyText as String
Dim SR As StreamReader = New StreamReader("C:\somefile.txt")
MyText = SR.ReadToEnd
SR.Close
Dim SW As StreamWriter = New StreamWriter("C:\somefile.txt")
SW.Write( MyText & Input)
SW.Close
End Sub

This will read in the textfile, and write it right back out. Now, another
nifty trick of the StreamWriter, open it and Append to the file, eliminating
the StreamReader all together. Check into it. They are both in the
System.IO namespace.

HTH
Sueffel
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.580 / Virus Database: 367 - Release Date: 2/6/2004
Nov 20 '05 #4

P: n/a
Mr Lumpier Britches,

Mr Wagner gave you an accuracte answer for your questions. The code he
posted will paste the current clipboard content into the notepad.

What more do you want ?
OHM

Lumpierbritches wrote:
How do you consider this is a solution. I have no idea what you're
talking about. I just wanted to know, in code, how to open a text
file and append text from a field on a form, using the clipboard
paste, from a command button>?>>>>>????

Michael


--
Best Regards - OHM
one.handed.man{at}BTInternet{dot}com
Nov 20 '05 #5

P: n/a
* lu*************@aol.com (Lumpierbritches) scripsit:
How do you consider this is a solution. I have no idea what you're talking
about. I just wanted to know, in code, how to open a text file and append text
from a field on a form, using the clipboard paste, from a command
button>?>>>>>????


Mhm... That's exactly what the code I posted is doing. It starts
Notepad and pastes some text into it using the clipboard.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #6

P: n/a
Thank you everyone. In VB 6, you could just use Shell and open a word / notepad
file and then using Keystrokes go to the end of the file and paste the
clipboard.

I'm just trying to take a command button and have code open a word/notepad file
and append some text in a form textbox, that's on the clipboard to a text file.

I'm not a fulltime coder, just playing with it for some personal use. When I
put any code in the button click event, I get undefined on Streamwriter or
reader. I'm going to try new things, but I was looking for a simple:
shell:(c:\program file\myfile.txt)
^{end}
^V
or something similar.

But, again, thank you for all your help. I'm just frustrated with .Net. It's
not anything like VB6, which took me awhile to get coding down in it.

Michael
Nov 20 '05 #7

P: n/a
* lu*************@aol.com (Lumpierbritches) scripsit:
reader. I'm going to try new things, but I was looking for a simple:
shell:(c:\program file\myfile.txt)


Use 'System.Diagnostics.Process.Start(<filename>)' instead.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.