首页 文章

使用excel vba实现自动化

提问于
浏览
0

我尝试使用excel vba自动执行此过程:

01

点击链接后...打开下一个窗口:

02

我的代码:

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

1 回答

  • 2

    试一试 . 它应首先选择表,然后选择属于该表的所有元素并根据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
    

相关问题