PDA

View Full Version : Open Notepad and write something onto it



arrun
10-15-2012, 09:15 AM
Hello again, can somebody give me some pointer on how to write the value of a variable (created within VBA) onto a notepad?

Let say I have following VBA code:


dim i as double
i = 99.99999

Now I want to open a notepad (using VBA code) and write the following onto it:

"The value of the variable 'i' is: ", i, "\n"

I know how to open a notepad using VBA however I am struggling to write something onto it.

Your help will be highly appreciated.

Thanks and regards,

Paul_Hossler
10-15-2012, 09:51 AM
I got this from some where on the web awhile ago


Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'Finds a window with the name, returns the handle.
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
'Sends a specified message to the window handle that you give it.
Private 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
'Gets a controls window handle. The form window handle must be specified to get a decent control.
Const WM_SETTEXT As Long = &HC
'Sets the text of this control.

Sub Write2Notepad()
Dim hWnd As Long
Dim chWnd As Long
Dim s As String

s = "The value of i is " & 999.99 & vbCrLf




'start Notepad
Call Shell("NOTEPAD.EXE", vbNormalFocus)
hWnd = FindWindow("Notepad", vbNullString)

'Find a control window handle... in Notepad, there's the main Textbox that you want.
chWnd = FindWindowEx(hWnd, ByVal 0&, vbNullString, vbNullString)
SendMessage chWnd, WM_SETTEXT, ByVal 0&, s
End Sub


Paul

Kenneth Hobs
10-15-2012, 11:04 AM
Why open notepad at all?

http://www.vbaexpress.com/forum/showthread.php?t=41543

snb
10-15-2012, 12:01 PM
sub snb()
i = 99.99999
createobject("scripting.filesystemobject").createtextfile("G:\OF\example.txt").write "The value of the variable 'i' is: " & i
end sub

shrivallabha
10-15-2012, 11:55 PM
One more:
Public Sub WriteToNotePad()
Dim i As Double
i = 99.9999
Open "C:\TestFile.txt" For Output As #1
Print #1, "The value of the variable 'i' is: " & i & " \n"
Close #1
Shell "Notepad " & """C:\TestFile.txt""", vbNormalFocus
End Sub