質問内容
VBA(IE)でWebページ上の情報を収集してExcelに転記するコーディングを行っています。
Webページは親ページ・子ページの階層構造になっており、処理の概要は下記の通りです。
- 親ページ上で情報を収集・Excelに転記する
- 子ページに遷移、子ページ上で情報を収集・Excelに転記する
- 親ページに戻る
- 上記1.~3.を全要素に対して繰り返す
親ページで子ページのURLをクリック後、子ページの各項目の情報をExcelに転記していますが
子ページに移動したタイミングで親ページの情報を格納した変数elが初期化されてしまい
次のループ処理(Next el)がエラー(実行時エラー'70': 書き込みできません。)となります。
変数elを静的変数(Sub→Static)として試してみましたが改善されない状況です。
対策方法をご教示頂けますでしょうか。
<親ページ>
http://www.squid-cache.org/Versions/v3/3.1/cfgman/
<子ページ>
http://www.squid-cache.org/Versions/v3/3.1/cfgman/auth_param.html
http://www.squid-cache.org/Versions/v3/3.1/cfgman/authenticate_cache_garbage_interval.html
...
VBAソースコード
Option Explicit Function IEWait(ByRef objIE As Object) Do While objIE.Busy = True Or objIE.readyState <> 4 DoEvents Loop End Function Sub Main() Worksheets("Sheet1").Select Worksheets("Sheet1").Cells.Clear Cells(1, 1) = "No:" Cells(1, 2) = "Option Name:" Cells(1, 3) = "Option Name:" Cells(1, 4) = "Replaces:" Cells(1, 5) = "Requires:" Cells(1, 6) = "Default Value:" Cells(1, 7) = "Suggested Config:" Dim objIE As New InternetExplorer Set objIE = New InternetExplorer objIE.Visible = True objIE.navigate "http://www.squid-cache.org/Versions/v3/3.1/cfgman/" Call IEWait(objIE) Dim el As Object Dim n As Integer n = 1 For Each el In objIE.document.getElementsByTagName("a") Cells(n + 1, 1) = n Cells(n + 1, 2) = el.innerText el.Click Call IEWait(objIE) Cells(n + 1, 3) = objIE.document.getElementById("name").innerText Cells(n + 1, 4) = objIE.document.getElementById("alias").innerText Cells(n + 1, 5) = objIE.document.getElementById("requires").innerText Cells(n + 1, 6) = objIE.document.getElementById("default").innerText Cells(n + 1, 7) = objIE.document.getElementById("config").innerText objIE.GoBack Call IEWait(objIE) n = n + 1 Next el End Sub
el変数状態
01_el.Cllick_実行前.PNG →親ページの設定値あり
02_el.Cllick_実行後.PNG →親ページの設定値なし
Excle Sheet1(1個目の要素は期待通りに取得できる)
No: | Option Name: | Option Name: | Replaces: | Requires: | Default Value: | Suggested Config: |
---|---|---|---|---|---|---|
1 | auth_param | auth_param | none | #Recommended minimum configuration per scheme:#auth_param negotiate program <uncomment and complete this line to activate>#auth_param negotiate children 5#auth_param negotiate keep_alive on##auth_param ntlm program <uncomment and complete this line to activate>#auth_param ntlm children 5#auth_param ntlm keep_alive on##auth_param digest program <uncomment and complete this line>#auth_param digest children 5#auth_param digest realm Squid proxy-caching web server#auth_param digest nonce_garbage_interval 5 minutes#auth_param digest nonce_max_duration 30 minutes#auth_param digest nonce_max_count 50##auth_param basic program <uncomment and complete this line>#auth_param basic children 5#auth_param basic realm Squid proxy-caching web server#auth_param basic credentialsttl 2 hours | ||
2 |
補足情報(FW/ツールのバージョンなど)
Microsoft Visual Basic for Applications 7.1.1127
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/11/03 09:57