How to replace or update “Style” attribute value by using VBA

旧巷老猫 提交于 2021-01-29 08:31:43

问题


I am feeding Data on website using VBA. I am want to change/replace or update the value of "Style" attribute on that website. HTML

<div class="timeline-row-item style="left: 556px;"></div>

I want to change the value from style="left: 556px;" to style="left: 300px;"

Sub test1()

Dim IE As New SHDocVw.InternetExplorer

IE.document.querySelectorAll("div[class*= timeline-row-iteml]").setAttribute ("style", left: 300px;")

How can i do this on Excel VBA.

Thank You


回答1:


querySelectorAll returns a collection. You cannot use setAttribute on that. You need an individual node

IE.document.querySelector("div[class*= timeline-row-iteml]").setAttribute("style", "left: 300px;")

Not sure if brackets are needed.

You can also use javascript

IE.document.parentWindow.execScript "document.querySelector('div[class*= timeline-row-iteml]').setAttribute('style', 'left: 300px;');"

If you want to do all matches then you will need to loop the returned nodeList

Dim elems As Object, i As Long
Set elems = IE.document.querySelectorAll("div[class*= timeline-row-iteml]")

For i = 0 To elems.Length -1
    elem.item(i).setAttribute('style', 'left: 300px;');"
Next


来源:https://stackoverflow.com/questions/55396709/how-to-replace-or-update-style-attribute-value-by-using-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!