[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]
『外部アプリとの連携(API)』(あおの)
業務に使用しているアプリとエクセルを連携したマクロを作成しようとしています。
アプリに表示された内容をエクセルのワークシートに転記するにはどうすればいいのでしょうか。
アプリのウィンドウハンドルやキャプションを取得するところまではできたのですが、その先がわからず(すべての子ウィンドウを取得してそれぞれのキャプションを書き出せば良いのかな?というような想像はつくのですが、VBA以外の言語を触ったことがないこともあって、本やネットで調べてみてもいまいち具体的なやり方がわかりませんでした)、ご教示いただけませんでしょうか。
< 使用 Excel:Excel2016、使用 OS:Windows7 >
冷静に考えて見れば、質問があまりにも漠然としすぎていました。
にも関わらず回答していただきありがとうございます。
アプリの仕様によってはやはり不可能ですよね。
自宅のPCでいろいろ試していたのですが、原始的な(?)設計のアプリであれば上記の方法でそれっぽい結果が得られたので、後日試してみようと思います。
お騒がせして申し訳ありませんでした。
(あおの) 2020/12/12(土) 12:40
Option Explicit
Const APP_CAPTION As String = "外部アプリの親ウィンドウのキャプション名"
Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _ ByVal lpClassName As String, _ ByVal lpWindowName As String _ ) As LongPtr
Declare PtrSafe Function EnumChildWindows Lib "user32.dll" ( _ ByVal hWndParent As LongPtr, _ ByVal lpEnumFunc As LongPtr, _ ByVal lParam As VBA.Collection _ ) As LongPtr
Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( _ ByVal hWnd As Long, _ ByVal lpString As String, _ ByVal cch As Long _ ) As LongPtr
Function hWndApp() As LongPtr hWndApp = FindWindow(vbNullString, APP_CAPTION) End Function
Function EnumChildProc( _ ByVal hWnd As LongPtr, _ ByVal lParam As VBA.Collection _ ) As Boolean
lParam.Add hWnd Let EnumChildProc = True End Function
Function GetChildWindows(Optional inParentHwnd As LongPtr = 0) As VBA.Collection Dim c As VBA.Collection Set c = New VBA.Collection Set GetChildWindows = c
Call EnumChildWindows(inParentHwnd, AddressOf EnumChildProc, c)
End Function
Sub 子ウィンドウのハンドルを取得()
Dim colls As New Collection Set colls = GetChildWindows(hWndApp)
Dim i As Long For i = 1 To colls.Count Cells(i, 1) = colls(i) Next
End Sub
Sub 子ウィンドウのキャプション名を取得()
Dim i As Long i = 1 Do While Cells(i, 1) <> "" Dim strCaption As String * 80 GetWindowText Cells(i, 1), strCaption, Len(strCaption)
Dim wndCap wndCap = Left(strCaption, InStr(strCaption, vbNullChar) - 1)
Cells(i, 2) = wndCap i = i + 1 Loop
End Sub
(あおの) 2020/12/12(土) 13:08
[ 一覧(最新更新順) ]
YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki.
Modified by kazu.