Open wordpad/notepad file

L

Lumpierbritches

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
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (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.
 
L

Lumpierbritches

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
 
S

Sueffel

Lumpierbritches said:
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
 
O

One Handed Man

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
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (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.
 
L

Lumpierbritches

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
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top