Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to control Notepad from my VB program?

Status
Not open for further replies.

GPerk

Programmer
Jul 6, 2002
161
US
I am trying to control an instance of a Notepad program which has some data in it - I want to save the data into a file.
I am trying this code:

Dim AllProcess() As Process = GetProcessesByName("notepad")
Dim MyProcess As Process = AllProcess(0) ' use the first one found
MyProcess.WaitForInputIdle(1000)
If MyProcess.Responding Then
System.Windows.Forms.SendKeys.SendWait("^s) ' Ctrl-S; File/Save
System.Windows.Forms.SendKeys.SendWait("C:\Test\T1.csv") ' enter filename
System.Windows.Forms.SendKeys.SendWait(vbCr) ' hit the Save button
End If

But it seems these commands are not getting to the Notepad form unless the focus is on that form when the SendKeys is being executed.
1. How can I set the focus to the Notepad?
2. Is there a better way to control the Notepad
 
Control Notepad? Notepad is about the easiest Windows program to recreate in .Net. Why don't you just create your own form with a multiline textbox and open/save functionality?
 
for that matter, why don't you just write the data to a file directly rather than playing with Notepad or text boxes all together? For Example:
Code:
        Dim Wrt As New IO.StreamWriter("C:\Test\T1.csv", False)
        Wrt.WriteLine("Line of text goes here or you can use a " & Variable)
        Wrt.Close()

--------------------------------------------------
"...and did we give up when the Germans bombed Pearl Harbor? NO!"

"Don't stop him. He's roll'n."
--------------------------------------------------
 
The problem is that the website insists on providing the data by spawning a Notepad which contains the data.
I can save the data by manually clicking and typing on the Notepad, but I would like to automate this process.
Sometimes dozens of files are involved each day.

It seems the alternative is to do screen scraping. Yuk!
 
Can you explain the entire process? I'm not sure how a website would cause Notepad.exe to be started on an end-user's machine.
 
OK, I think I have it worked out.
The first part of the process causes the data to be downloaded into a Notepad screen.

Then the following sub is activated.
(I discovered the AppActivate on another website.)

Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim AllProcess() As Process = GetProcessesByName("notepad")
Dim MyProcess As Process = AllProcess(0)
MyProcess.WaitForInputIdle(1000)
Application.DoEvents()
If MyProcess.Responding Then
AppActivate(MyProcess.ProcessName) ' give notepad the focus
System.Windows.Forms.SendKeys.SendWait("^a") ' Ctrl-A; select all text
System.Windows.Forms.SendKeys.SendWait("^c") ' Ctrl-C; copy to Clipboard
Dim Gdata As IDataObject = Clipboard.GetDataObject() ' get the data from clipboard
txtData.Text = Gdata.GetData(DataFormats.Text).ToString()
MyProcess.Kill()
Else
MsgBox("Save failed.")
End If
End Sub

So now we have the data within our program where we can process it.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top