0

I have a script in VBA that navigates to several pages in IE. I have about 100 people using this script with no issues, however 1 person gets an error at the same spot every time. The only difference between this button and the rest is that it is an img instead of a button I am unable to figure out the issue, please help! When the script reaches this spot it does find the "View as PDF" and clicks on it, however the PDF is not loaded.

Sub Invoicepull()

Dim I As SHDocVw.InternetExplorer, idoc As MSHTML.HTMLDocument
Dim doc_ele As MSHTML.IHTMLElement, doc_eles As MSHTML.IHTMLElementCollection, doc_click As MSHTML.IHTMLElement, doc_clicks As MSHTML.IHTMLElementCollection
Dim doc_ele2 As MSHTML.IHTMLElement, doc_eles2 As MSHTML.IHTMLElementCollection
Dim UserID As String, MyURL As String
Dim WHttp As Object, FileData() As Byte, filenum As Long
Dim Store_ele As MSHTML.IHTMLElement, BPN As String
Dim x As Integer, Y As Integer, Invoices As String
Dim M As Integer, MyFile As String
Dim XX As Double, Cycle As Integer


'i.Visible = True 'for testing only
UserID = Range("k1").Value

    Set I = New InternetExplorerMedium
    Found1 = False
    BPN = ""
    I.Navigate "http://pbi_e1_vault.pb.com/ServiceWeb2/Interface"
    Do While I.ReadyState <> READYSTATE_COMPLETE
    Loop

    Set idoc = I.Document
    On Error GoTo VPN1

    If Range("m1").Value = "US" Then
        If Left(MDI.txtinv.Value, 1) = 3 Then
            idoc.getElementsByTagName("Select").Item("DB").Value = "uslease"
        Else
            idoc.getElementsByTagName("Select").Item("DB").Value = "usnonlease"
        End If
    ElseIf Range("m1").Value = "CA" Then
        If Left(MDI.txtinv.Value, 1) = 3 Then
            idoc.getElementsByTagName("Select").Item("DB").Value = "calease"
        Else
            idoc.getElementsByTagName("Select").Item("DB").Value = "canonlease"
        End If

    End If
    On Error GoTo 0
    Set doc_eles = idoc.getElementsByTagName("input")
    For Each doc_ele In doc_eles
        If doc_ele.Title = "Search" Then
        doc_ele.Click
        Exit For
        End If
    Next doc_ele
    Do While I.Busy
    Loop

    idoc.getElementsByTagName("Select").Item("K").Value = "InvoiceNumber"
    idoc.getElementsByTagName("Input").Item("Q").Value = MDI.txtinv.Value

    Set doc_eles = idoc.getElementsByTagName("input")
    For Each doc_ele In doc_eles
        If doc_ele.Title = "Search" Then
        doc_ele.Click
        Exit For
        End If
    Next doc_ele

    Do While I.Busy
    Loop
    'search for inv
    Set doc_eles = idoc.getElementsByTagName("a")
    For Each doc_ele In doc_eles
        If doc_ele.innerText = MDI.txtinv.Value Then
            If Found1 = False Then
                Found1 = True
                Set Store_ele = doc_ele
            Else
                'found a duplicate invoice request bpn
                BPN = InputBox("More than one invoice found, please enter 
the BPN for invoice " & MDI.txtinv.Value, "Multiple invoices", 
"0012345678")
TryAgain:
                Set doc_eles2 = idoc.getElementsByTagName("a")
                For Each doc_ele2 In doc_eles2
                    If doc_ele.innerText = BPN Then
                        doc_ele.Click
                        Exit For
                    End If
                Next doc_ele2
                If doc_ele.innerText <> BPN Then
                    BPN = InputBox("BPN was not found, please make sure to 
include 00 before the number", "Not found", "0012345678")
                    GoTo TryAgain
                End If
            End If
            If BPN <> "" Then
                Exit For
            Else
                Store_ele.Click
                Exit For
            End If
        End If
    Next doc_ele
    If doc_ele Is Nothing Then GoTo NotFound1
    Do While I.Busy
    Loop

    'click pdf
    Set doc_eles = idoc.getElementsByTagName("img")
    For Each doc_ele In doc_eles
        'If doc_ele.getAttribute("src") = "images/csearch.gif" Then
        If doc_ele.Title = "View as PDF" Then
          ''Error is here''' Clicks Doc_ele but does not actually load pdf''' 
            doc_ele.Click
        Exit For
        End If
    Next doc_ele

    'Application.Wait Now + #12:00:03 AM#
    'save PDF
Save2:
    MyURL = I.LocationURL
    Set WHttp = CreateObject("WinHTTP.WinHTTPrequest.5.1")
    WHttp.Open "GET", MyURL, False
    On Error GoTo Pause
    WHttp.send
    On Error GoTo 0
    XX = InStr(1, WHttp.responseText, "/Title")
    If XX = 0 Then
        Application.Wait Now + #12:00:01 AM#
        Cycle = Cycle + 1
        If Cycle = 9 Then GoTo NotFound1
        GoTo Save2
    End If
    Cycle = 0
    FileData = WHttp.responseBody
    Set WHttp = Nothing
    If Worksheets("Tracking").Range("b15").Value = "" Then
MyFile = "C:\Users\" & Trim(UserID) & "\Desktop\" & MDI.txtinv.Value & 
".pdf"
    Else
MyFile = Worksheets("Tracking").Range("b15").Value & "\" & 
MDI.txtinv.Value & ".pdf"
    End If
    filenum = FreeFile
    On Error GoTo PathCheck
    Open MyFile For Binary Access Write As #filenum
    On Error GoTo 0
        Put #filenum, 1, FileData
    Close #filenum
    Erase FileData()
    I.Quit 'close IE
         'add invoice to list and track

            If Range("a2").Value = "" Then
                Range("a2").Value = MDI.txtinv.Value
                Range("b2").Value = "Pulled"
                Range("c2").Value = MDI.lblpath.Caption
                Range("f2").Value = Format(Now, "MM/DD/YYYY")
            Else
     Range("a1").End(xlDown).Offset(1).Value = MDI.txtinv.Value
     Range("b1").End(xlDown).Offset(1).Value = "Pulled"
     Range("a1").End(xlDown).Offset(, 2).Value = MDI.lblpath.Caption
     Range("a1").End(xlDown).Offset(, 5).Value = Format(Now, "MM/DD/YYYY")
            End If
                Worksheets("Tracking").Range("A1").Value = 
Worksheets("Tracking").Range("A1").Value + 1
                If Worksheets("Tracking").Range("A1").Value >= 40 Then 
Call SendInv.SendUpdate
      ThisWorkbook.FollowHyperlink (MyFile) 'open PDF
Exit Sub
Jeff F
  • 15
  • 4
  • What is the error message this one person gets? On which line does it occur? Any major differences in set-up between this person and the other unaffected users? – QHarr Jun 08 '20 at 21:05
  • After clicking the button, how do you display the PDF file, directly displays the PDF content on the web page or using the Adobe PDF Reader add-on? You could check this part of code in the website. Besides, you could also suggest the user to check whether he has installed the Adobe PDF reader add-on (Open IE browser -> click IE Tools -> Click the Manage Add-ons option, check the installed add-ons). Finally, as QHarr said, what is the error message? You could try to use the F12 developer tools to get the error message. – Zhi Lv Jun 09 '20 at 01:37
  • There is no system error at this part since it does find the "View as PDF" and executes the click but it doesn't actually go to the page. – Jeff F Jun 09 '20 at 13:19
  • The website pulls up the PDF as a render on the site, I then download it and save using Set WHttp = CreateObject("WinHTTP.WinHTTPrequest.5.1") but the script is not even getting to this part for the user. – Jeff F Jun 09 '20 at 13:21
  • Try to directly view the website using IE browser (without VBA), check whether could view the PDF file? If it's still not working, perhaps the issue is related to the website, please check your website code. If this issue only happens with VBA, can you post the Enough code (include the Html resource, JavaScript or VBA code)to reproduce the problem as in [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Zhi Lv Jun 10 '20 at 02:13
  • Thanks Zhi Lv. I did have the user navigate to the site manually and they did not have this issue. Posting my code below shortly – Jeff F Jun 11 '20 at 16:32
  • Since, the site(http://pbi_e1_vault.pb.com/ServiceWeb2/Interface) can’t be reached, according to the VBA code, I suppose in your website, you are displaying the pdf via blob data, right? please check [this thread](https://stackoverflow.com/questions/26161314/displaying-binary-file-pdf-in-ie-11/37443172) whether you are using the same method to view PDF file? If that is the case, since, IE 11 blocks display of blob, in the IE browser, you have to download it to local first, then display them, otherwise, you could try to use [pdf.js](https://mozilla.github.io/pdf.js/) to display the pdf file. – Zhi Lv Jun 12 '20 at 03:33
  • if I misunderstand your problem, can you post the related code in the html page, about how do you view the PDF file? – Zhi Lv Jun 12 '20 at 03:35
  • The problem is not about how I am viewing the PDF file, the issue is when the script executes the doc_ele.click line. The code executes but the button doesn't actually click in IE – Jeff F Jun 12 '20 at 16:32
  • @JeffF, You could try to use the execScript method to execute the JavaScript script and click the "View as PDF" button. – Zhi Lv Jun 18 '20 at 13:43

1 Answers1

0

Try to use the execScript method to execute the JavaScript script and click the button.

Call IE.document.parentWindow.execScript("document.querySelector('img[title=""View as PDF""]').click();", "JavaScript")
Zhi Lv
  • 18,845
  • 1
  • 19
  • 30