Write text to a new notebook

Can someone help me write the cell value to a new instance of Notepad?

Here is the code I tried:

Sub a()
    Dim nt As String
    nt = Shell("notepad.exe", vbNormalFocus)
    Print #1, ActiveSheet.Cells(1, 1).Value
    Close #1
End Sub
+4
source share
1 answer

I answered a similar question many years ago on vbforums.com, but could not find it, so I quickly rewrote it for you. I commented on the code so that you do not have problems understanding it.

Like you and I, we have names, similarly, windows have descriptors (hWnd), a class, etc. Once you know what hWnd is, it’s easier to interact with it. The Findwindow API finds the hWnd of a particular window using the class name. Read the rest of the API here.

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

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

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const WM_SETTEXT = &HC

Private Sub Command1_Click()
    Dim Ret As Long, ChildRet As Long
    Dim sString As String

    '~~> This is the value from the cell which
    '~~> you want to send to notepad
    sString = Range("A1").Value

    '~~> Start Notepad
    Ret = Shell("notepad.exe", vbNormalFocus)

    '~~> Wait for it to load
    DoEvents

    '~~> Find notepad
    Ret = FindWindow(vbNullString, "Untitled - Notepad")

    '~~> Check if found
    If Ret = 0 Then
        MsgBox "Cannot find Notepad Window"
        Exit Sub
    End If

    '~~> Find the "Edit Window" which is a child window of Notepad window
    ChildRet = FindWindowEx(Ret, ByVal 0&, "Edit", vbNullString)

    '~~> Send the message
    SendMessage ChildRet, WM_SETTEXT, 0, ByVal sString
End Sub

, Spy ++ uuSpy. , "" Spy ++

enter image description here

+5

Source: https://habr.com/ru/post/1615342/


All Articles