Hello Fire_d,
I made the following changes to the code. There are 2 macros: FindNotepad and ReadNotepad. FindNotepad searches for an open instance of Notepad. You can use the file name like "Book1.xls" or a partial name like "Report", or no name at all. If no name is supplied then the first open instance of Notepad is used. The ReadNotepad takes the Window handle returned by FindNotepad and returns all of the text as a string. Delete the old code you have and paste this code in it is place. A sample of using the macros is included.
'Written: May 21, 2010
'Author: Leith Ross
'Summary: Looks for an open Notepad file either by file name (no path), partial file name,
' or no name. If no name is specified the first Notepad file found is used.
Option Explicit
'GetWindow Constants
Const GW_CHILD = 5
Const GW_HWNDFIRST = 0
Const GW_HWNDLAST = 1
Const GW_HWNDNEXT = 2
Const GW_HWNDPREV = 3
Const GW_OWNER = 4
'Window Message Constants
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE
'You can use the GetDlgItem function with any parent-child window pair, not just with
'dialog boxes. As long as the hDlg (hWnd) parameter specifies a parent window and the
'child window has a unique identifier (as specified by the hMenu parameter in the
'CreateWindow or CreateWindowEx function that created the child window),
'GetDlgItem returns a valid handle to the child window.
Private Declare Function GetDlgItem _
Lib "User32.dll" _
(ByVal hDlg As Long, _
ByVal nIDDlgItem As Long) As Long
'Send messages to windows
Private Declare Function SendMessage _
Lib "User32.dll" _
Alias "SendMessageA" _
(ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any) As Long
'Get the length of a Window's caption
Private Declare Function GetWindowTextLength _
Lib "User32.dll" _
Alias "GetWindowTextLengthA" _
(ByVal hWnd As Long) As Long
'Get the caption of a Window as a string
Private Declare Function GetWindowText _
Lib "User32.dll" _
Alias "GetWindowTextA" _
(ByVal hWnd As Long, _
ByVal lpString As String, _
ByVal nMaxCount As Long) As Long
'Return the length of a null terminated string
Private Declare Function StrLen _
Lib "kernel32.dll" _
Alias "lstrlenA" _
(ByVal lpszString As String) As Long
Private Declare Function GetWindow _
Lib "User32.dll" _
(ByVal hWnd As Long, _
ByVal wCmd As Long) As Long
Private Declare Function GetDesktopWindow _
Lib "User32.dll" () As Long
Private Declare Function GetClassName _
Lib "User32.dll" _
Alias "GetClassNameA" _
(ByVal hWnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long
Public Function FindNotepad(Optional FileName As String) As Long
Dim Caption As String
Dim ClassName As String
Dim L As Long
Dim TopWnd As Long
'Find any open Notepad file or one whose file name matches (whole or partial name)
If FileName = "" Then
FileName = "*"
Else
FileName = "*" & FileName & "*"
End If
'Start with the Top most window that has the focus
TopWnd = GetWindow(GetDesktopWindow, GW_CHILD)
'Loop while the hWnd returned by GetWindow is valid.
While TopWnd <> 0
'Get Window caption
L = GetWindowTextLength(TopWnd) + 1
Caption = String(L, Chr$(0))
L = GetWindowText(TopWnd, Caption, L)
Caption = IIf(L > 0, Left(Caption, L), "")
'Get the Window Class name
L = GetWindowTextLength(TopWnd) + 1
ClassName = String(L, Chr$(0))
L = GetClassName(TopWnd, ClassName, L)
ClassName = IIf(L > 0, Left(ClassName, L), "")
If Caption Like FileName And ClassName = "Notepad" Then
FindNotepad = TopWnd
Exit Function
End If
'Get the next Window
TopWnd = GetWindow(TopWnd, GW_HWNDNEXT)
'Process Windows events.
DoEvents
Wend
End Function
Public Function ReadNotepad(hWnd As Long) As String
Dim Buffer As String
Dim BuffSize As Long
Dim chWnd As Long
Dim nEditID As Long
Dim RetVal As Long
'Get the window handle of the Edit Control which is a child window of Notepad:
nEditID = 15
chWnd = GetDlgItem(hWnd, nEditID)
'Get the character count of the text and setup a buffer to hold it:
BuffSize = SendMessage(chWnd, WM_GETTEXTLENGTH, 0, 0) + 1
Buffer = String(BuffSize, Chr$(0))
'Read the text from Notepad into the buffer:
RetVal = SendMessage(chWnd, WM_GETTEXT, BuffSize, ByVal Buffer)
If RetVal <> 0 Then
ReadNotepad = Left(Buffer, StrLen(Buffer))
End If
End Function
Example of Using the Macros
Public Sub ReadNotepadTest()
Dim Text As String
'Read the text from any open Notepad file
Text = ReadNotepad(FindNotepad)
Range("A1") = Text
End Sub
Bookmarks