LoginSignup
5
7

More than 5 years have passed since last update.

VBAによるIE自動制御

Last updated at Posted at 2016-07-28

pearltrees
http://www.pearltrees.com/yuaisheng/tree/id9610952

ExcelVBA 逆引き大全 620の極意
http://www.shuwasystem.co.jp/support/7980html/2744.html

Windows Forms
https://en.wikipedia.org/wiki/Windows_Forms

IE Automation Library and Web Macro Recorder
http://codecentrix.blogspot.jp/
http://codecentrix.com/

VBAによるIE自動制御
http://vba-code.net/ie/click-an-image/


'日経平均のチャートをクリックする
Sub ClickNikkeiChart()
    Dim objIE As Object

    'IE起動
    Set objIE = CreateObject("InternetExplorer.Application")
    objIE.Visible = True

    'Yahooファイナンスに接続
    objIE.navigate "http://finance.yahoo.co.jp/"

    'IEを待機
    Call IEWait(objIE)

    '5秒停止
    Call WaitFor(5)

    '日経平均株価のチャート画像をクリック
    Call IEImageClick(objIE, "s?s=998407.O")

    'IEを待機
    Call IEWait(objIE)

    '5秒停止
    Call WaitFor(5)

    'IE終了
    objIE.Quit

    Set objIE = Nothing
End Sub

'画像をクリックする関数
Public Function IEImageClick(ByRef objIE As Object, imageSrc As String)
    Dim objImage As Object
    For Each objImage In objIE.Document.getElementsByTagName("IMG")
        If InStr(objImage.src, imageSrc) > 0 Then
            objImage.Click
            Exit For
        End If
    Next
End Function

'IEを待機する関数
Function IEWait(ByRef objIE As Object)
    Do While objIE.Busy = True Or objIE.readyState  4
        DoEvents
    Loop
End Function

'指定した秒だけ停止する関数
Function WaitFor(ByVal second As Integer)
    Dim futureTime As Date

    futureTime = DateAdd("s", second, Now)
    While Now < futureTime
       DoEvents
    Wend
End Function
function WaitForLoad($ie)
{
    while($ie.Busy)
    {
        sleep -Milliseconds 50
    }
}

$ie = New-Object -COM InternetExplorer.Application
$ie.Navigate("http://www.google.com")
$ie.Visible = $true
WaitForLoad($ie)
$searchBox = $ie.Document.getElementById("q")
$searchBox.value = "Windows PowerShell"
$goButton = $ie.Document.getElementByID("btnG")
$goButton.click()

function NavigateIE($url)
{
    $ie = new-object -com "InternetExplorer.Application";
    $ie.navigate($url);
    #ie.visible=true;
    [System.Threading.Thread]::Sleep(2000);
    return $ie;
}

function FindTag($params)
{
    $ie = $params[0];
    $tagname = $params[1];
    $contain = $params[2];
    $doc = $ie.document;
    $tags = @($doc.getElementsByTagName($tagname));
    foreach($tag in $tags)
    {
        if($tag.outerHTML.contains($contain))
        {
            $sel = $tag;
            break;
        }
    }
    return $sel;
}

$ie = NavigateIE("http://weibo.com/12321");

$talk = FindTag($ie, "textarea", 'id=publish_editor');

$but = FindTag($ie, "span", 'class=bgColorB');

$talk.value = "test";
$but.click(); 
5
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7