首页 > 解决方案 > 无法设置 Internet Explorer 11 以获得焦点

问题描述

我无法使用 MS ACCESS 宏在 IE11 中保存下载的文件。

这是我在下载链接后想要点击的地方:

在此处输入图像描述

我需要使用用户名/密码登录才能进入该页面。

我目前正在使用 SendKeys 来获取文件,当我手动执行我的宏时,它工作得很好。但是,当我使用 Windows 计划任务启动宏时,Internet Explorer 失去焦点,发送键在桌面上执行。

我尝试了几件事,包括:

-IE.可见=真

- 使用 Shell 对象执行 sendkeys

- 尝试过自动化,但我无法使其工作(我不再使用该方法使用“sendkeys”)。我看到有几个关于堆栈溢出的主题可以帮助我,但我没有成功让它们工作:Controlling IE11 "Do you want to Open/Save" dialog window button in VBA or IE11 Frame Notification Bar Save button

- 尝试通过链接下载,但无法使其工作

这是我的代码,我隐藏了重要字段。请原谅评论中的法国人

Option Explicit
'Déclaration pour Sleep
#If VBA7 Then
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems
#Else
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32 Bit Systems
#End If
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long

Public Function Fct_Elemica()

        Dim IEObject                        As InternetExplorer  'Objet Internet Explorer
        Dim IEDoc                           As HTMLDocument      'Document HTML
        Dim Shell                           As Object  'Objet WSScript

        'Automations
        Dim o                               As IUIAutomation
        Dim e                               As IUIAutomationElement
        Dim iCnd                            As IUIAutomationCondition
        Dim Button                          As IUIAutomationElement
        Dim InvokePattern                   As IUIAutomationInvokePattern

        'Phase de login
        Dim InputLogin                      As HTMLInputElement  'Element HTML pour le controle du champ login
        Dim InputPassword                   As HTMLInputElement  'Element HTML pour le controle du champ password
        Dim SubmitButton                    As HTMLInputElement  'Element HTML pour le controle des boutons d'envoi de formulaire

        'Phase de generation du report
        Dim ProductSelection                As HTMLSelectElement 'Element HTML pour le controle du champ type de produit
        Dim DateEndSelection                As HTMLInputElement  'Element HTML pour le controle du champ date de fin

        'Phase de telechargement du lien
        Dim ObjLink                         As Object            'Element HTML pour le controle des liens href
        Dim strDownloadLink                 As String            'URL du fichier a telecharger

        'Variables parametrables
        Dim strDownloadFolder               As String
        Dim strLoginPageURL                 As String
        Dim strLogin                        As String
        Dim strPassword                     As String

        Dim HWNDSrc                         As Long
        Dim h                               As LongPtr
        Dim Child                           As LongPtr




        strDownloadFolder = Nz(DLookup("PAR_TXT_VAL", "tParam", "PAR_LIB = 'DOWNLOAD_PATH'"), "") 'Get the download path
        strLoginPageURL = "https://portal.elemica.com/jsp/index.jsp"                              'Get the link of the website
        strLogin = "**************"                                                               'Login
        strPassword = "***********"                                                               'Password


        'Instanciation de la varirable IE
        Set IEObject = CreateObject("InternetExplorer.Application")

        'Affichage de la fenetre IE
        IEObject.Visible = True



        'Chargement de la page de login du site elemica
        IEObject.Navigate strLoginPageURL

        'On attend le chargement complet de la page
        IE_LOADING_WAIT IEObject

        'On pointe la page courante
        Set IEDoc = IEObject.Document


        'On pointe nos differents controleurs
        Set InputLogin = IEDoc.all("username_id")
        Set InputPassword = IEDoc.all("passwordID")
        Set SubmitButton = IEDoc.all("submitbutton")

        'On definit le texte que l'on souhaite placer a l'interieur et on se loggue
        InputLogin.value = strLogin
        InputPassword.value = strPassword
        SubmitButton.Click

        'Chargement de la page Expeditions -> Tous
        IEObject.Navigate "https://portal.elemica.com/nav?cmd=SRAll&reset=1&groupSelected=-1"

        'On attend le chargement complet de la page
        IE_LOADING_WAIT IEObject

        'On repointe la page courante
        Set IEDoc = IEObject.Document

        'On entre les valeurs pour parametrer la sortie de l'extraction
        Set ProductSelection = IEDoc.getElementsByName("productGroupIDSelected")(0)
        Set DateEndSelection = IEDoc.getElementsByName("toDate")(0)
        Set SubmitButton = IEDoc.getElementsByName("btnView")(0)

        ProductSelection.value = "105"
        DateEndSelection.value = Format(Now() + 3, "dd/mm/yyyy")
        SubmitButton.Click


        'On attend le chargement complet de la page
        IE_LOADING_WAIT IEObject

        'On interrompt de rafraichissement dynamique
        IEObject.Stop

        'On recherche le l'adresse du lien "Telecharger dans Excel"
        For Each ObjLink In IEDoc.Links
            If ObjLink.innerText = "Télécharger dans Excel" Then
                strDownloadLink = ObjLink.href
            End If
        Next ObjLink

        'Navigate to the download link
        IEObject.Navigate strDownloadLink

        'Wait until the page is completely loaded
        IE_LOADING_WAIT IEObject

'        Set o = New CUIAutomation
'        h = IEObject.hWnd
'        Sleep 500
'        Child = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)
'        Set e = o.ElementFromHandle(ByVal h)
'
'        Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "&Apri")
'
'        Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
'        Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
'        InvokePattern.Invoke

        Sleep 500
        SendKeys "^j"
        Sleep 500
        SendKeys "{RIGHT}"
        Sleep 500
        SendKeys "{RIGHT}"
        Sleep 500
        SendKeys "{RIGHT}"
        Sleep 500
        SendKeys "~"
        Sleep 500

        'Wait until the end of the download
        Sleep 20000

        'Close IE
        IEObject.Quit
        Shell ("taskkill /F /IM iexplore.exe")


        'Close vars
        Set InputLogin = Nothing
        Set InputPassword = Nothing
        Set SubmitButton = Nothing
        Set ProductSelection = Nothing
        Set DateEndSelection = Nothing
        Set IEDoc = Nothing
        Set IEObject = Nothing
        Set Shell = Nothing

End Function


Public Function IE_LOADING_WAIT(IEObject As InternetExplorer)

    'On boucle tant que la page n'est pas totalement chargee
    Do Until IEObject.ReadyState = READYSTATE_COMPLETE
        DoEvents
    Loop

End Function

提前感谢您的回答,祝您有美好的一天!

标签: vbams-accessscheduled-tasksinternet-explorer-11

解决方案


您可以尝试参考下面的步骤和示例代码,并尝试修改您的代码可能有助于解决问题。

将文件C:\Windows\System32\UIAutomationCore.dll文件复制到用户文档,即C:\Users\admin\Documents然后将引用UIAutomationClient添加到您的宏文件中。

将以下代码粘贴到您的模块中:

    Option Explicit
    Dim ie As InternetExplorer
    Dim h As LongPtr
    Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr

Sub Download()
    Dim o As IUIAutomation
    Dim e As IUIAutomationElement
    Set o = New CUIAutomation
    h = ie.Hwnd
    h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)
    If h = 0 Then Exit Sub

    Set e = o.ElementFromHandle(ByVal h)
    Dim iCnd As IUIAutomationCondition
    Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Save")

    Dim Button As IUIAutomationElement
    Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
    Dim InvokePattern As IUIAutomationInvokePattern
    Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
    InvokePattern.Invoke
End Sub   

参考:

为 IE9 (vba) 自动保存对话


推荐阅读