VBAã§IEãæãï¼DOMããè¦ç´ ãåå¾ï¼
ä»åã¯ãåå¾ããIEããDOMãåå¾ããããã«ããããã®è¦ç´ ãåå¾ãããã¨æãã¾ãã
DOMã£ã¦ä½ï¼
ã¨ãã声ãèãããããèãããªãã£ãã・・・・・・ã
ãã¼ã£ã¨ãç§ãããã¾ã詳ãããããããªãã§ãããJavaScriptã®æ¸ç±ãªã©ããå¾ãç¥èã«ããã¨ï½¥ï½¥ï½¥ï½¥ï½¥ï½¥
Document Object Modelã®ç¥ã§ãHTMLææ¸ããªãã¸ã§ã¯ãã®ããã«æ±ãä»çµã¿ã®ãã¨ã ããã§ãã
ååãIEããªãã¸ã§ã¯ãã¨ãã¦åå¾ããã³ã¼ããæ²è¼ãã¾ããããIEã«ã¯ document ã¨ããããããã£ãããããã®ããããã£ãããHTMLææ¸ã®ãªãã¸ã§ã¯ãåããããã®ãåå¾ãããã¨ãã§ãã¾ãã
ãããªæãã«å³ç¤ºãã¦ã¿ã¾ããã
Â
å³å¯ã«ã¯ããã®HTMLããªãã¸ã§ã¯ãåãããã®ãDOMã¨å¼ã¶ã®ããããããªãã¸ã§ã¯ãã®ããã«æ±ãæ¦å¿µã»ä»æ§ã®ãã¨ã DOM ã¨å¼ã¶ã®ãå®ã¯ããç¥ããªã(ï¼ _ï¼ ;)
ãã¦ããã®ããã«DOMãåå¾ããã®ã§ãããå°ãåéãããããªã®ã¯ãé常ã®ãªãã¸ã§ã¯ãããªã¼ã®ããã«.ï¼ããªãªãï¼ã§åºåã£ã¦
ie.document.html.body.h1.ããããã£ï¼ã¾ãã¯ã¡ã½ããï¼
Â
ã¨ãããããªè¨è¿°ã«ã¯ãæ®å¿µãªããã§ãã¾ããã
ã¡ã³ãã¦ã§ãããè¦ç´ ãè¨ãæããã°ãã¿ã°ãã documentããããã£ã«ç¨æããããgetElementByï½ï¼ã¾ãã¯ãgetElementsByï½ï¼ ã¡ã½ãã ã§ããªãã¸ã§ã¯ãã¨ãã¦ååå¾ããå¿ è¦ãããã¾ããï¼Elementãè¦ç´ ããgetãåå¾ãããï¼
Â
ãã¨ãã°ãHTMLææ¸ä¸ã«ãããªã¿ã°ããã£ãã¨ããã・・・・・・ã
Â
<p id="test1" class="test2" name="test3">ã¦ãã¨</p>
Â
ãã®ã¿ã°ãåå¾ããæ¹æ³ã¯ä¸è¡¨ã®éãã§ãã
ã¡ã½ãã | å¼æ° | 使ç¨ä¾ |
---|---|---|
getElementById | idå±æ§å¤ã§æå® | ie.document.getElementById("test1") |
getElementsByClassName | Classå±æ§å¤ã§æå® | ie.document.getElementsByClassName("test2") |
getElementsByName | Nameå±æ§å¤ã§æå® | ie.document.getElementsByName("test3") |
getElementsByTagName | ã¿ã°åã§æå® | ie.document.getElementsByTagName("p") |
Â
idã¯HTMLææ¸ä¸ã§å¯ä¸ç¡äºãªã®ã§åæ°å½¢ï¼Elementï¼ãä»ã¯åãææ¸ä¸ã«è¤æ°åå¨ãå¾ãã®ã§è¤æ°å½¢ï¼Elementsï¼ã§ãã
ã¿ã°åã§pã¿ã°ãåå¾ããã®ãªãããããªã¤ã¡ã¼ã¸ã
Â
id以å¤ã¯è¤æ°å½¢ã§ã³ã¬ã¯ã·ã§ã³æ±ããããããã対象ã®è¦ç´ ãçµãè¾¼ãããã«ã¯ãã¤ã³ããã¯ã¹ãæå®ããããFor Each ï½Next ãªã©ã§ã²ã¨ã¤ãã¤è©ä¾¡ãã¾ãã
classããnameã®å¤ãææ¸ä¸ã«ä¸ã¤ãããªããã¨ãåãã£ã¦ããã°ã次ã®ããã«æ±ºãæã¡ã§åå¾ã§ãã¾ããï¼ã³ã¬ã¯ã·ã§ã³ã®ã¤ã³ããã¯ã¹çªå·ã¯ 0 ããå§ã¾ããã¨ã«æ³¨æï¼
Â
Set p = ie.document.getElementsByClassName("test2")(0)
Set p = ie.document.getElementsByName("test3")(0)
Â
pã¿ã°ã«æã¾ããæååã"ã¦ãã¨"ãã§è©ä¾¡ãããªãã次ã®ããã« For Each ï½Next ããåå¾ã§ãã¾ãã
For Each tmp In ie.document.getElementsByTagName("p")
If tmp.innerText = "ã¦ãã¨" ThenSet p = tmp Exit For End If Next
Â
ãã¦ãã¦ããªãã¸ã§ã¯ãã¨ãã¦åç §ã§ãããªãããã¨ã¯ãã¤ãéã
p.ããããã£
p.ã¡ã½ãã
ã®ããã«ãè¨è¿°ãããã¨ãã§ãã¾ãã
ãªãã¸ã§ã¯ãã«ã©ã®ãããªããããã£ãã¡ã½ãããç¨æããã¦ãããã¯ãã¤ã³ããªã»ã³ã¹ï¼èªåè£å®ã·ã¹ãã ï¼ã§ç¢ºèªãããã¨ãã§ãã¾ãã
DOMã®ã¤ã³ããªã»ã³ã¹ããªã³ã«ããã«ã¯ãä¸è¨ãåç §è¨å®ãã¾ãã
Microsoft HTML Object Library
Â
ããã¾ã§ããµã¾ããå½ããã°è¨äºã表示ããIEã§ã¡ãã£ã¨ããæä½ã試ãã¦ã¿ã¾ãã
2018-08-11ã®è¨äºã¿ã¤ãã«ãå¤æ´ãã¦ã¿ã・・・・・・
ã½ã¼ã¹ã®è¡¨ç¤ºãªã©ã§ãè¨äºã¿ã¤ãã«ã<a>ã¿ã°ã§æã¾ãã¦ãããã¨ãCSSãé©ç¨ããããã«Classå±æ§ãä»å ããã¦ãããã¨ã«çç®ãã¾ãã
Â
ClassNameã§æ±ºãæã¡ãããªã次ã®ããã«ãã¾ã
Sub test()
Dim ie As InternetExplorer
Dim sh As Object
Dim win As Object
Set sh = CreateObject("Shell.Application")
For Each win In sh.Windows
If win.Name = "Internet Explorer" Then
Set ie = win
Exit For
End If
Next
ie.document.getElementsByClassName("entry-title-link bookmark")(0).innerText = "è¨äºã¿ã¤ãã«ãå¤æ´ãã¦ã¿ã"
End Sub
Â
aã¿ã°ã®ã³ã¬ã¯ã·ã§ã³ãåå¾ããè¨äºã¿ã¤ãã«ã®æååã§è©ä¾¡ãããªã次ã®ããã«ãã¾ã
Sub test()
Dim ie As InternetExplorer
Dim sh As Object
Dim win As Object
Dim a As HTMLAnchorElement
Set sh = CreateObject("Shell.Application")
For Each win In sh.Windows
If win.Name = "Internet Explorer" Then
Set ie = win
Exit For
End If
Next
For Each a In ie.document.getElementsByTagName("a")
If a.innerText = "VBAã§IEãæãï¼èµ·åæ¸ã¿IEãã·ã§ã«ã¨ãã¦åå¾ï¼" Then
a.innerText = "è¨äºã¿ã¤ãã«ãå¤æ´ãã¦ã¿ã"
Exit For
End If
Next a
End Sub
Â
 ã©ã¡ãããçµæãåãããã«ãªãã¾ãã
Â
å¤æ´å
Â
å¤æ´å¾
Â
ã§ã・・・・・・ãè¨äºã¿ã¤ãã«ãå¤æ´ãã¦ã¿ãã¨ããã§ä½ã®ã¡ãªããããªãã£ã(ï¼ _ï¼ ;)âã
Â
次å以éãVBAãããããã¹ãããã¯ã¹ã«æååãå ¥åããããã»ã¬ã¯ãããã¯ã¹ã®é ç®ãé¸æãããããã§ãã¯ããã¯ã¹ã»ã©ã¸ãªãã¿ã³ãONã»OFFãããããªã³ã¯ããã¿ã³ãã¯ãªãã¯ããããã¨ãè²ã ãã£ã¦ã¿ããã¨æãã¾ãã