| 
    
     |  | ▼kim さん: 解決後みたいですが失礼します。もう見ないかな?
 
 昨日Textの読み込み部分で躓いていたところが昨日解決しました。
 ・・・・出来るはずなのに出来ないのは悔しいからやってしまった。
 APIゴリゴリと書きましたが、そんな事は無かったですね。ゴリぐらい?
 
 サンプル程度ですが、UPしておきます。
 解るようになったらおいしい所をとって利用して下さい。
 CommandButton1_Clickを改造する事で当初ご希望の事は
 出来ると思います。・・・多分です。検証はしてません。
 
 準備:
 ・新規UserFormを追加
 ・UserFormにTextBoxを1個、CommandButtonを1こ追加
 ・以下をUserFormにコピペ
 ※適当なテキストをメモ帳で開いておく。
 ・CommandButtonクリックでメモ帳の内容を取得する。
 一応動作確認してます。
 
 Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
 (ByVal lpClassName As String, _
 ByVal lpWindowName 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 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 Const WM_GETTEXT = &HD
 Private Const WM_GETTEXTLENGTH = &HE
 Private Const classname As String = "Notepad"
 Private Const EditClassName As String = "Edit"
 
 
 Private Sub CommandButton1_Click()
 Dim hOwner As Long
 Dim sBuf As String
 'メモ帳のHWND取得
 hOwner = FindWindow(classname, vbNullString)
 sBuf = GetEditData(hOwner)
 Me.TextBox1.Text = sBuf
 End Sub
 
 '任意のメモ帳のTextを取得する
 'このFunctionを適当に改造するとExcelがお亡くなりになりますから
 '理解しないうちは改造しないように。
 Private Function GetEditData(phWnd As Long)
 Dim hChild As Long
 Dim ret As Long, lLen As Long
 Dim sBuf As String
 Dim bytBuf() As Byte
 
 'EditのHWND取得
 hChild = FindWindowEx(phWnd, 0&, EditClassName, vbNullString)
 lLen = SendMessage(hChild, WM_GETTEXTLENGTH, 0, 0)
 lLen = lLen + 1
 ReDim bytBuf(lLen)
 'byteで取得するのが味噌だった。???
 ret = SendMessage(hChild, WM_GETTEXT, lLen, bytBuf(0))
 If ret <> 0 Then
 sBuf = StrConv(bytBuf, vbUnicode)
 Else
 sBuf = ""
 End If
 GetEditData = sBuf
 End Function
 
 Private Sub UserForm_Initialize()
 Me.TextBox1.MultiLine = True
 Me.TextBox1.ScrollBars = fmScrollBarsBoth
 Me.CommandButton1.Caption = "任意のメモ帳のTextを取得する"
 End Sub
 
 |  |