IE Automation - File Browser freezes VB (1 Viewer)

AdrianThorn

New member
Local time
Yesterday, 22:25
Joined
Jun 16, 2015
Messages
7
I am writing a script that will automate the upload of some shapefiles to a confidential website. I've hit a snag during the final step of the process - specifying the location of each file. The page contains an element which does not accept text, rather you have to click it and it opens a file browser. The pertinent code:

Code:
<input name="FileUpload1" id="FileUpload1" type="file">

My script opens the file browser like so:

Code:
IE.Document.GetElementsByName("FileUpload1")(0).Click

After which it immediately stops running and spits out this error:

"This action cannot be completed because the Upload a Shapefile - Internet Explorer application (Upload a Shapefile - Internet Explorer) is not responding. Choose Switch To to activate Upload a Shapefile - Internet Explorer and correct the problem."

I'm at a loss as to why this is occurring or how to get around it. I can't find a way to suppress the dialogue nor can I interact with it because nothing runs after it opens. Any suggestions? The code (with some redactions) are below:

Code:
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim i As Long
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim SWs As ShellWindows ''Requires Microsoft Internet Controls
Dim oWShell As Object

Set db = CurrentDb
Set SWs = New ShellWindows
 
'Open Internet Explorer
Set IE = CreateObject("InternetExplorer.Application")

IE.Visible = True
    
'Open NMCRIS
IE.Navigate 'main page
TabNum = SWs.Count

'Wait for the page to load
Do While IE.Busy
Loop
        
'Enter in login credentials
Set objCollection = IE.Document.GetElementsByTagName("input")
objCollection(8).Value = 'username
objCollection(9).Value = 'password


CTime = Time
GTime = DateAdd("s", 1, CTime)
    
Do Until CTime >= GTime
    CTime = Time
Loop
    
Do While IE.Busy
Loop
    
'Click the login button
IE.Document.getElementsByClassName("button_link")(0).Click

CTime = Time
GTime = DateAdd("s", 1, CTime)
    
Do Until CTime >= GTime
    CTime = Time
Loop

Do While IE.Busy
Loop

'Do Until a = 208
    'Navigate to the Resource list
    IE.Navigate 'site URL
    
    Do While IE.Busy
    Loop
    
    CTime = Time
    GTime = DateAdd("s", 2, CTime)
    
    Do Until CTime >= GTime
        CTime = Time
    Loop
    
    'Find the site
    IE.Document.getElementsByClassName("Search_Input")(0).Value = Replace(site_array(0), "LA_", "")
    '''''Find the activnity IE.Document.getElementsByClassName("Search_Input")(1).Value = "139788"
    
    CTime = Time
    GTime = DateAdd("s", 1, CTime)
    
    Do Until CTime >= GTime
        CTime = Time
    Loop
    
    Do While IE.Busy
    Loop
       
    'Press the search button
    IE.Document.getElementsByClassName("button_link")(0).Click

    Do While IE.Busy
    Loop
    
    CTime = Time
    GTime = DateAdd("s", 3, CTime)
    
    Do Until CTime >= GTime
        CTime = Time
    Loop

    'Find the site which matches our Activity number, then open the GIS page
    Set objCollection = IE.Document.getElementsByClassName("ttc")
    i = 0

    Do Until i = objCollection.Length
        If InStr(objCollection(i).InnerText, nmcris_activity) > 0 Then
            n = i + 2
            Set objElement = objCollection(n).firstElementChild
        End If
        i = i + 1
    Loop

    objElement.Click
    
    Do While IE.Busy
    Loop

    CTime = Time
    GTime = DateAdd("s", 5, CTime)
    Do Until CTime >= GTime
        CTime = Time
    Loop

    'Switch to the GIS tab
    With CreateObject("Shell.Application").Windows
        Set IE = .Item(SWs.Count - 1)
    End With

    'Open the editor menu
    
    IE.Document.getElementsByClassName("TaskMenu_1 TaskMenu_3 TaskMenu_7")(0).Click
    
    CTime = Time
    GTime = DateAdd("s", 1, CTime)
    Do Until CTime >= GTime
        CTime = Time
    Loop

    Do While IE.Busy
    Loop
    
    'Check to see if the site needs a new shapefile or if we are updating one.
    Set objCollection = IE.Document.GetElementsByName("TaskManager1$EditorTask1$Editor$nmcrisEditPanel$ddSites")
    GIStatus = Null
    i = 0

    Do Until i = objCollection.Length
        If objCollection(i).ID = "TaskManager1_EditorTask1_Editor_nmcrisEditPanel_ddSites" Then
            GIStatus = objCollection(i).Value
            Set objElement = objCollection(i)
        End If
    
        i = i + 1
    Loop

    If IsNull(GIStatus) = True Then
        'Need to create the shape file
    Else
        'Need to update the shaple file.  Activate edits.
        IE.Document.GetElementsByName("TaskManager1$EditorTask1$Editor$nmcrisEditPanel$btnEditSite")(0).Click
        
        CTime = Time
        GTime = DateAdd("s", 1, CTime)
        Do Until CTime >= GTime
            CTime = Time
        Loop
    
        Do While IE.Busy
        Loop
    
        'Wait for the confirmation box to appear, then hit OK
        Set oWShell = CreateObject("wscript.shell")

        Do Until oWShell.AppActivate("Message from webpage")
            CTime = Time
            GTime = DateAdd("s", 0.1, CTime)
            Do Until CTime >= GTime
                CTime = Time
            Loop
        Loop
            
        oWShell.SendKeys "{enter}"
        
        CTime = Time
        GTime = DateAdd("s", 1, CTime)
        Do Until CTime >= GTime
            CTime = Time
        Loop
    
        Do While IE.Busy
        Loop
        
        'SendKeys "~"
        
        'Open up the uploading dialogue
        IE.Navigate ''another URL
        
        Do While IE.Busy
        Loop
        
        CTime = Time
        GTime = DateAdd("s", 2, CTime)
        Do Until CTime >= GTime
            CTime = Time
        Loop
    

        'Input the shape file locations

        CTime = Time
        GTime = DateAdd("s", 1, CTime)
        Do Until CTime >= GTime
            CTime = Time
        Loop
        
IE.Document.GetElementsByName("FileUpload1")(0).Click
 

Users who are viewing this thread

Top Bottom