使用 excel vba 实现自动化
Posted
技术标签:
【中文标题】使用 excel vba 实现自动化【英文标题】:Automation with excel vba 【发布时间】:2016-10-10 03:51:21 【问题描述】:我尝试使用 excel vba 自动化这个过程:
点击链接后...打开下一个窗口:
我的代码:
Sub WFM_test()
Sheets("Preenchimento_Remedy").Activate
Wd = Range("D02").Value 'URL address
Set objShell = CreateObject("Shell.Application")
Set objAllWindows = objShell.Windows
For Each ow In objAllWindows
'MsgBox ow
If (InStr(1, ow, "Internet Explorer", vbTextCompare)) Then
'MsgBox ow.Hwnd & " " & ow & " " & ow.locationURL
If (InStr(1, ow.LocationURL, Wd, vbTextCompare)) Then
Set objRemedy = ow
End If
End If
Next
If objRemedy Is Nothing Then
Else
Set objPage = objRemedy.Document
Set WFM = ObjPage.getElementsByClassName("MenuEntryNameHover")
WFM.item(0).click
End if
End Sub
【问题讨论】:
问题是……? 如何用vba点击“默认WFM”?我试过:Set WFM = ObjPage.getElementsByClassName("MenuEntryNameHover").item(0).click什么都别做.. 如果没有看到 XML 就很难知道,我怀疑有人愿意在 XML 中为您找到变量,我可以建议您循环遍历元素以查找名称吗?像这样: For Each elem In ieApp.Document.all Range("A" & (i + 1)).Value = elem i= i+1 Next 我编辑了图片 01!!见html代码。 【参考方案1】:
试一试。它应该首先选择表格,然后选择属于该表格的所有元素并根据 InnerText 进行匹配。
Sub WFM_test()
Dim AllTableItems As Object
Dim element As Object
Sheets("Preenchimento_Remedy").Activate
Wd = Range("D02").Value 'URL address
Set objShell = CreateObject("Shell.Application")
Set objAllWindows = objShell.Windows
For Each ow In objAllWindows
'MsgBox ow
If (InStr(1, ow, "Internet Explorer", vbTextCompare)) Then
'MsgBox ow.Hwnd & " " & ow & " " & ow.locationURL
If (InStr(1, ow.LocationURL, Wd, vbTextCompare)) Then
Set objRemedy = ow
End If
End If
Next
If Not objRemedy Is Nothing Then
' you need this (0) as you specify which class you want to select
' The classname is not a unique property
Set Table = objRemedy.Document.getElementsByClassName("MenuTable")(0)
'Select all Elements in the table
Set AllTableItems = Table.getElementsbyTagName("*")
'Iterate over the elements in the table and find the match
For Each element In AllTableItems
If element.InnerText = "Default WFM Group" Then element.Click
Next i
End If
End Sub
【讨论】:
太棒了!很高兴它有帮助。将其标记为已接受的答案:)以上是关于使用 excel vba 实现自动化的主要内容,如果未能解决你的问题,请参考以下文章
请问Excel 里面的 VBA 具体能做啥?都有哪些实际的用途?
excel中想实现使用Python代替VBA,请问应该怎么做