2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Excel VBAちゃんが「ブラウザを起動してパスワード入れてログインする」

Last updated at Posted at 2019-08-19

#趣旨
ほんの少しだけでも自動化することで、
大きくストレスが軽減されることがあるよねっていう例

ブラウザを起動してログインするための部品です
そのまま使えることはない
URLやパスワード以外にもカスタマイズが必要です。

VBAProject
参照設定はMicrosoft Internet Controls

IE_login)Internet Explorer用の処理はライブラリを使って正攻法
chr_login)Google Chrome用はSendKeys,sleepを使って力技

Module1.bas
#If VBA7 Then
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal ms As LongPtr)
#Else
Private Declare Sub Sleep Lib "kernel32" (ByVal ms As Long)
#End If


Sub ADM0001()
    Call IE_login("https://www.pokemoon.net:8081/lg/", "ADM0001", "new+new")
End Sub
Sub ADM0002()
    Call IE_login("https://www.pokemoon.net:8082/lg/", "ADM0002", "nekoneko")
End Sub

Sub ADM0001ch()
    Call chr_login("https://www.pokemoon.net:8081/lg/", "ADM0001", "new{+}new") 'エスケープが必要
End Sub
Sub ADM0002ch()
    Call chr_login("https://www.pokemoon.net:8082/lg/", "ADM0002", "nekoneko") 'エスケープが必要
End Sub

Function IE_login(pUrl As String, _
                  pId As String, _
                  pPs As String)
    
    Dim objIE As InternetExplorer
    Set objIE = New InternetExplorer
    
    objIE.Visible = True
    objIE.navigate pUrl
    
    Do While objIE.Busy = True Or objIE.readyState < READYSTATE_COMPLETE
        DoEvents
    Loop
    
    Dim htmlDoc As HTMLDocument
    Set htmlDoc = objIE.document
    
    With htmlDoc
        .getElementById("PUSERID").Value = pId
        .getElementById("PPASSWORD").Value = pPs
    End With
    Set htmlDoc = Nothing
    
    objIE.navigate "javascript:J_FORM_SUBMIT()"
    Set objIE = Nothing

End Function

Function chr_login(pUrl As String, _
                   pId As String, _
                   pPs As String)
    
    Dim objChr As Object
    Set objChr = CreateObject("WScript.Shell")
    objChr.Run ("chrome.exe -url " & pUrl)
    
    Sleep 4000
    SendKeys pId, True
    SendKeys "{ENTER}", True
    SendKeys "{TAB}", True

    Sleep 1000
    SendKeys pPs, True
    SendKeys "{ENTER}", True

    Set objChr = Nothing

End Function

//2019/09/19
関数の呼び出しをDebug.Printでやってる謎の部分をちゃんとCallに直すと、
メモリが足りませんが出なくなりました。とほほ

#先達 参考 Special Thanks
大変ありがとうございます

【エクセルVBAでIE操作】ブラウザの読み込み待ちをしないとダメなのです
https://tonari-it.com/vba-ie-wait/#toc3

タブーの小技 パスワードクリップ BAT/VBA/Javascript
https://qiita.com/lamp/items/7c36efbae41bd30b7774
これの続き
2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?