teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

まともな回答が得られなかった

2018/10/02 08:27

投稿

dewdtm
dewdtm

スコア8

title CHANGED
File without changes
body CHANGED
@@ -1,188 +1,1 @@
1
- いつもお世話になっております。
2
-
3
- 以前にこちらのサイト様で、ExcelVBAにおける外部アプリの操作(主にハンドル関連)についてご質問させていただき、おかげさまで外部データベース入力の自動化を実現させることが出来ました。
4
-
5
- 【以前の質問ページ】
6
- https://teratail.com/questions/135393
7
- https://teratail.com/questions/135546
8
-
9
- 今回は、ExcelVBAでコーディングしたそのプログラムを、VB.NETでも使用できるよう、
10
- 変換したい思い、再度ご質問に上がりました。
11
-
12
- システムの根幹となるハンドル取得のコードは、
13
- 下記のサイト様からコピーしたものです。
14
- 【参考にしたコード】
15
- http://nonsoft.la.coocan.jp/SoftSample/SampleEnumWindows.html
16
-
17
- こちらにはご親切にもVB6とVB.NET両方のコードが載せられているのですが、
18
- VB.NETの方は、肝心のハンドルが取得できず、Class名とTitleしか取得できません。
19
- まずこのハンドル+Class名+titleをTreeView1に列挙すること、
20
- Class名とtitleを除外したハンドルのみの情報をTreeView2に列挙すること、
21
- そして、ハンドル取得⇒送信すること、
22
- この三点のコードを変換したい思っております。
23
-
24
- ここからコードーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
25
-
26
- 【ハンドル+Class名+title情報をTreeView1に列挙するコード】
27
- Private Sub NodesW()
28
- Dim i As Long
29
- Dim J As Long
30
- Dim strClass As String
31
- Dim strTitle As String
32
- Dim strSpace As String
33
-
34
- ' ウィンドウとコントロールの全ての情報を取得
35
- Dim colWindows As Collection
36
- Set colWindows = GetAllWindows()
37
-
38
- ' 親ウィンドウ毎のコレクションループ
39
- For i = 1 To colWindows.Count
40
- ' 子コントロール毎のコレクション取得
41
- Dim colChilds As Collection
42
- Set colChilds = colWindows.Item(i)
43
-
44
- 'If colChilds.Count >= 1 Then ' 全ての親ウィンドウを対象
45
- If colChilds.Count > 1 Then ' 子コントロールを持つ物のみ対象
46
- ' 子コントロール毎のコレクションループ
47
-
48
- For J = 1 To colChilds.Count
49
- strSpace = StrConv(String(50, " "), vbFromUnicode)
50
-
51
- ' コレクションからクラス名取得
52
- strClass = StrConv(colChilds.Item(J)(1), vbFromUnicode)
53
- If LenB(strClass) < 30 Then
54
- strClass = LeftB$(strClass & strSpace, 30)
55
- End If
56
- strClass = StrConv(strClass, vbUnicode)
57
-
58
- ' コレクションから文字列取得
59
- strTitle = StrConv(colChilds.Item(J)(2), vbFromUnicode)
60
- If LenB(strTitle) < 50 Then
61
- strTitle = LeftB$(strTitle & strSpace, 50)
62
- End If
63
- strTitle = StrConv(strTitle, vbUnicode)
64
-
65
- If J = 1 Then
66
- ' 親ウィンドウの情報をツリービューへ追加
67
- Call TreeView1.Nodes.Add( _
68
- , , "R" & _
69
- Right$("00000000" & Hex(colChilds.Item(J)(0)), 8), _
70
- Right$("00000000" & Hex(colChilds.Item(J)(0)), 8) & " - " & _
71
- strClass & " - " & strTitle)
72
- Else
73
- ' 子コントロールの情報をツリービューへ追加
74
- Call TreeView1.Nodes.Add( _
75
- "R" & Right$("00000000" & Hex(colChilds.Item(1)(0)), 8), _
76
- tvwChild, "C" & _
77
- Right$("00000000" & Hex(colChilds.Item(J)(0)), 8), _
78
- Right$("00000000" & Hex(colChilds.Item(J)(0)), 8) & " - " & _
79
- strClass & " - " & strTitle)
80
- End If
81
- Next J
82
- End If
83
- Next i
84
- End Sub
85
-
86
-
87
-
88
-
89
-
90
- 【ハンドル情報のみをTreeView2に列挙するコード】
91
-
92
- Private Sub NodesS()
93
- Dim i As Long
94
- Dim J As Long
95
- Dim strClass As String
96
- Dim strTitle As String
97
- Dim strSpace As String
98
-
99
- ' ウィンドウとコントロールの全ての情報を取得
100
- Dim colWindows As Collection
101
- Set colWindows = GetAllWindows()
102
-
103
- ' 親ウィンドウ毎のコレクションループ
104
- For i = 1 To colWindows.Count
105
- ' 子コントロール毎のコレクション取得
106
- Dim colChilds As Collection
107
- Set colChilds = colWindows.Item(i)
108
-
109
- 'If colChilds.Count >= 1 Then ' 全ての親ウィンドウを対象
110
- If colChilds.Count > 1 Then ' 子コントロールを持つ物のみ対象
111
- ' 子コントロール毎のコレクションループ
112
- For J = 1 To colChilds.Count
113
- If J = 1 Then
114
- ' 親ウィンドウの情報をツリービューへ追加
115
- Call TreeView2.Nodes.Add(, , _
116
- "R" & Right$("00000000" & Hex(colChilds.Item(J)(0)), 8), _
117
- "&H" & Right$("00000000" & Hex(colChilds.Item(J)(0)), 8))
118
- Else
119
- ' 子コントロールの情報をツリービューへ追加
120
- Call TreeView2.Nodes.Add("R" & Right$("00000000" & Hex(colChilds.Item(1)(0)), 8), _
121
- tvwChild, _
122
- "C" & Right$("00000000" & Hex(colChilds.Item(J)(0)), 8), _
123
- "&H" & Right$("00000000" & Hex(colChilds.Item(J)(0)), 8))
124
- End If
125
- Next J
126
- End If
127
- Next i
128
- End Sub
129
-
130
-
131
-
132
-
133
-
134
- 【外部ハンドルを取得して文字列とクリック処理を送信するコード】
135
- 'Login画面の親子ハンドルの取得とLoginID・Password・OKボタンを送信
136
- hWndChild1 = 0
137
- hWndChild2 = 0
138
- hWndChild3 = 0
139
- Do
140
- hwndParent = FindWindow("Class名", "title")
141
- Call NodesS
142
- Call NodesW
143
- If hwndParent <> 0 Then
144
- Call NodesS
145
- Call NodesW
146
- For idx = 1 To TreeView2.Nodes.Count
147
- If CLng(TreeView2.Nodes(idx)) = hwndParent Then
148
- hWndChild1 = TreeView2.Nodes(idx + 9)
149
- hWndChild2 = TreeView2.Nodes(idx + 10)
150
- hWndChild3 = TreeView2.Nodes(idx + 11)
151
- Exit Do
152
- End If
153
- Next
154
- End If
155
- DoEvents
156
- Loop
157
-
158
- st = Now
159
- Do
160
- DoEvents
161
- Ret = IsWindowEnabled(hWndChild1)
162
- If DateDiff("s", st, Now) >= 10 Then Exit Do
163
- Loop While Ret = 0
164
-
165
- If Ret = 0 Then
166
- Debug.Print "time out"
167
- End If
168
-
169
- SetForegroundWindow hwndParent
170
- strDt = "LoginID"
171
- lngRc = SendMessage(hWndChild1, WM_SETTEXT, 0, strDt)
172
- strDt2 = "password"
173
- lngRc = SendMessage(hWndChild2, WM_SETTEXT, 0, strDt2)
174
- lngRc = SendMessage(hWndChild3, BM_CLICK, 0, 0)
175
-
176
- ここまでーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
177
-
178
- 現在標準モジュールは、VB.NET側のコードを使用ており
1
+ 的確な回答が得られなかったためのコメントは削除いたしました
179
- また、当方が修正できそうにないエラーが出る箇所は、
180
- .Nodes
181
- DoEvents
182
- Right$
183
- Set
184
- LenB
185
- **strSpace**~~ = StrConv(~~**String**~~(50, " "),~~ **vbFromUnicode**)
186
-
187
- かなり複雑になっておりますが、
188
- どうかご教示の程、よろしくお願い致します。