Write Text In Notepad

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
10 posts Page 1 of 1
Contributors
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

Write Text In Notepad
XTechVB
how can i write some text from my application to notepad?
You can find me on Facebook or on Skype mihai_92b
User avatar
Agust1337
Coding God
Coding God
Posts: 2456
Joined: Fri Feb 19, 2010 8:18 pm

Re: Write Text In Notepad
Agust1337
What do you mean? Do you want to be able to write a text document or write a text from you application to notepad(Sending information)?
Top-notch casual Dating
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

Re: Write Text In Notepad
XTechVB
Agust1337 wrote:
What do you mean? Do you want to be able to write a text document or write a text from you application to notepad(Sending information)?
Sending Information
You can find me on Facebook or on Skype mihai_92b
User avatar
Napster1488
VIP - Donator
VIP - Donator
Posts: 524
Joined: Fri Jan 07, 2011 8:41 pm

Re: Write Text In Notepad
Napster1488
U could do it with sendkeys.send("blabla")
YouTube Downloader v3.0
Image
Image
Image
User avatar
Agust1337
Coding God
Coding God
Posts: 2456
Joined: Fri Feb 19, 2010 8:18 pm

Re: Write Text In Notepad
Agust1337
Try this but im not sure if it will work
Code: Select all
'under public class
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Public Const WM_SETTEXT = &HC 

'the sending information:
Dim notepad As Long
Dim editx As Long

notepad = FindWindow("notepad", vbNullString)
editx = FindWindowEx(notepad, 0&, "edit", vbNullString)
Call SendMessageByString(editx, WM_SETTEXT, 0&, "The New Text")

If editx = 0 Then
    Msgbox "Cannot Locate Notepad Window "
    Exit Sub
End If 
Original thread: http://www.vbforums.com/showthread.php?t=295769
Top-notch casual Dating
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

Re: Write Text In Notepad
XTechVB
Agust1337 wrote:
Try this but im not sure if it will work
Code: Select all
'under public class
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Public Const WM_SETTEXT = &HC 

'the sending information:
Dim notepad As Long
Dim editx As Long

notepad = FindWindow("notepad", vbNullString)
editx = FindWindowEx(notepad, 0&, "edit", vbNullString)
Call SendMessageByString(editx, WM_SETTEXT, 0&, "The New Text")

If editx = 0 Then
    Msgbox "Cannot Locate Notepad Window "
    Exit Sub
End If 
Original thread: http://www.vbforums.com/showthread.php?t=295769
it doesn't work
You can find me on Facebook or on Skype mihai_92b
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Write Text In Notepad
mandai
This will set the text in a Notepad window:
Code: Select all
    <DllImport("user32.dll")> Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function

    <DllImport("user32.dll")> Shared Function FindWindowEx(ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
    End Function

    <DllImport("user32.dll")> Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As String) As IntPtr
    End Function

    Const WM_SETTEXT As UInteger = &HC

    Private Sub btnSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSet.Click
        Dim notepad As IntPtr = FindWindow("notepad", Nothing)
        notepad = FindWindowEx(notepad, IntPtr.Zero, "edit", Nothing)

        If notepad = IntPtr.Zero Then
            MsgBox("Cannot Locate Notepad Window")
        Else
            SendMessage(notepad, WM_SETTEXT, IntPtr.Zero, "The New Text")
        End If
    End Sub
Last edited by mandai on Thu Mar 22, 2012 5:02 pm, edited 1 time in total.
User avatar
2cool4cereal2
VIP - Donator
VIP - Donator
Posts: 151
Joined: Thu Oct 14, 2010 3:26 am

Re: Write Text In Notepad
2cool4cereal2
viewtopic.php?f=38&t=3456&p=24103#p24103

That's a tutorial on how to use the sendkeys command.
"I try to be modest at all times, and that's what makes me better than everyone else."
Image
Image
Image
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Re: Write Text In Notepad
smashapps
This is the code I use:
Code: Select all
SendKeys.Send(TextBox1.Text & "{enter}") 
That will send Textbox1's text and then press Enter
Code: Select all
SendKeys.Send("some text")
That one will just type some text
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Write Text In Notepad
mandai
By using SendKeys you risk not having the window in focus and the keys not being typed.

SendMessage would be the more reliable method.
10 posts Page 1 of 1
Return to “Coding Help & Support”