<サンプル> Dim objIE As InternetExplorer Set objIE = CreateObject("InternetExplorer.Application") objIE.Visible = True '見えるようにする objIE.navigate "http://saru-html.pupu.jp/8_9.shtml" 'リストボックスとコンボボックスが表示されたHPを開く
'1:IEを起動する Do While objIE.Busy Or objIE.readyState < READYSTATE_COMPLETE DoEvents Loop
'2:入力開始 Dim htdoc As HTMLDocument Set htdoc = objIE.document
'name属性[kamoku1]を探す Dim myHTML1 As HTMLAnchorElement
'[デートスポット]リストボックスの[鳥取県]を選択する For Each myHTML1 In htdoc.getElementById("デートスポット") '[kamoku1]タグを用いた場合 If InStr(myHTML1.innerText, "鳥取県") > 0 Then 'myHTML1が鳥取県か調べる myHTML1.Selected = True '選択する Exit For 'ループから抜ける End If Next
Dim objIE As InternetExplorer Set objIE = CreateObject("InternetExplorer.Application") objIE.Visible = True '見えるようにする objIE.navigate "http://www.google.co.jp" 'googleを開く
'1:IEを起動する Do While objIE.Busy Or objIE.readyState < READYSTATE_COMPLETE DoEvents Loop
'2:入力開始 'ID属性[q]へ検索名を入力する objIE.document.getElementById("q").Value = "検索ワード" End Sub
'変数FileN0にファイル番号0を代入 Dim FileN0 As Integer FileN0 = FreeFile()
'テキストファイル作成(ファイル番号0) Open FileName For Output As #FileN0
'ファイルに文字"test"を書き込む Print #FileN0, HTMLString 'テキストファイルとの接続をきる Close #FileN0
MsgBox "出力しました" End Sub
Private Function getIE(arg_title As String) As InternetExplorer '開いているIEの中でタイトルと一致するHPを探す Dim ie As InternetExplorer Dim sh As Object Dim win As Object Dim document_title As String
Set sh = CreateObject("Shell.Application")
For Each win In sh.Windows document_title = "" '開いているIEのタイトルを格納した変数をクリア On Error Resume Next document_title = win.document.Title '開いているIEのタイトルを格納 On Error GoTo 0
If InStr(document_title, arg_title) > 0 Then 'タイトルと引数(検索ワード)とが一致する Set ie = win Exit For '一致したらループから抜ける End If Next
Set getIE = ie End Function
Private Function HTML取得(container As Object, Optional depth As Long = 0) As String Dim ErrorInfo As String Dim htdoc As HTMLDocument
'↓↓↓HTML文書取得時のエラーを無視する↓↓↓ On Error Resume Next Set htdoc = container.document
If Err.Number <> 0 Then 'エラー情報を保持する ErrorInfo = Trim(str(Err.Number)) & ":" & Err.Description End If
On Error GoTo 0 '↑↑↑HTML文書取得時のエラーを無視する↑↑↑
Dim ret As String ret = "-------------" & vbCrLf '区切り値作成 ret = ret & "[" & Trim(str(depth)) & "階層]" & vbCrLf If Not htdoc Is Nothing Then ret = ret & htdoc.Title & " | " & htdoc.Location & " (" & container.Name & ")" & vbCrLf ret = ret & "-------------" & vbCrLf ret = ret & htdoc.getElementsByTagName("HTML")(0).outerHTML & vbCrLf 'HTMLソース全体を取得
'フレームの数だけ繰り返す Dim i As Integer For i = 0 To htdoc.frames.Length - 1 ret = ret & HTML取得(htdoc.frames(i), depth + 1) Next Else ret = ret & "-------------" & vbCrLf ret = ret & ErrorInfo End If
Sub リンククリックテスト() 'あらかじめIEでホームページを開いておく Set objIE = IE取得開始("12星座占い - Yahoo!占い") '開いているHPのタイトルを指定 objIE.Visible = False '見えるないようにする→バックグラウンドで実行する
'1:IEを起動する Do While objIE.Busy Or objIE.readyState < READYSTATE_COMPLETE DoEvents Loop
Dim HTMLDoc As HTMLDocument
Set HTMLDoc = objIE.document
Dim img1 As HTMLImg 'HTMLのイメージを宣言
For Each img1 In HTMLDoc.getElementsByTagName("IMG") If InStr(img1.alt, "しし座") > 0 Then ’しし座という名前が含まれているか確認する img1.Click 'クリックする Exit For 'ループから抜ける End If Next
Call 画面移動が完了するのを待つ
objIE.Visible = True '見えるようにする End Sub
Private Function IE取得開始(arg_title As String) As InternetExplorer '開いているIEの中でタイトルと一致するHPを探す Dim ie As InternetExplorer Dim sh As Object Dim win As Object Dim document_title As String
Set sh = CreateObject("Shell.Application")
For Each win In sh.Windows document_title = "" '開いているIEのタイトルを格納した変数をクリア On Error Resume Next document_title = win.document.Title '開いているIEのタイトルを格納 On Error GoTo 0
If InStr(document_title, arg_title) > 0 Then 'タイトルと引数(検索ワード)とが一致する Set ie = win Exit For '一致したらループから抜ける End If Next
Set IE取得開始 = ie '検索結果を処理をするHPとする End Function
Private Sub 画面移動が完了するのを待つ() 'EADYSTATEがCOMPLETEになるまで待つ
Do While objIE.Busy Or objIE.readyState < READYSTATE_COMPLETE DoEvents Loop End Sub
<サンプル> Const myAns As String = "含まれる文字" Dim i As Long
For i = 1 to 10 If InStr(cells(i,"A").Value, myAns > 0 Then Debug.Print "A列 " & i & "行には" & myAns & "が含まれます" else Debug.Print "A列 " & i & "行には" & myAns & "がありません" End If Next i
基本的な流れは・・・ ①ieのDocumentからクリックしたいHtmlエレメントオブジェクトを探し出して ②clickする となる。 その際に使うコードの例をあげると次の通り id → getElementById name → getElementsByName
<サンプル> '~~~準備 Dim htdoc As HTMLDocument'HTML文書全体を宣言する Set htdoc = ie.document '1:ボタンの名称からリンクへ 'img(ボタン名)が「xxx」である '(HTMLでは[IMG alt=xxx]にて表記している) Dim img1 As HTMLImg 'HTMLのイメージを宣言
For Each img1 In htdoc.getElementsByTagName("IMG") If InStr(img1.alt, "xxx") > 0 Then img1.Click 'クリックする Exit For 'ループから抜ける End If Next
'2:属性から '2-1 タグの属性がIMG で クラスの名前は ○○○ Dim img2 As HTMLImg 'HTMLのイメージを宣言
For Each img2 In htdoc.getElementsByTagName("IMG") If InStr(img2.className, "○○○") > 0 Then img2.Click 'クリックする Exit For 'ループから抜ける End If Next
'2-2 タグの属性がa herf で ボタンの名前は □□□ Dim myHTML1 As HTMLAnchorElement 'ハイパーリンクを指定する[a herf]タグを用いた場合
For Each myHTML1 In htdoc.getElementsByTagName("A") '[a herf]タグを用いた場合 If InStr(myHTML1.className, "□□□") > 0 Then myHTML1.Click 'クリックする Exit For 'ループから抜ける End If Next
'2-3 タグの属性がSPAN で その属性の値は △△△ Dim myHTML2 As HTMLSpanElement
For Each myHTML2 In htdoc.getElementsByTagName("SPAN") If myHTML2.innerText = "△△△" Then myHTML2.Click 'クリックする Exit For 'ループから抜ける End If Next