-1

I got the below vba code from this site. The code will automatically open the site in IE(will be using different url) and will import files as well. When I ran this code,it did not work due to incorrect elementsTagName and maybe InputType as well. What should be the correct codes? I am not sure. The second part is the html codes.

Please help check the codes.

Sub File_Test()
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim HTMLButton As MSHTML.IHTMLElement
Dim btnInput As MSHTML.IHTMLInputElement 
Dim ie As Object 
Dim pointer As Integer 
Set ie = CreateObject("internetexplorer.application") 
ie.Visible = True 
ie.navigate "http://www.htmlquick.com/reference/tags/input-file.html" 
Do While ie.readyState <> READYSTATE_COMPLETE 
Loop 
Set HTMLDoc = ie.document 
Set HTMLButtons = HTMLDoc.getElementsByTagName("Upload Files") 
For Each HTMLButton In HTMLButtons 
    For Each btnInput In HTMLButtons 
        If btnInput.Type = "button" Then 
             HTMLButton.Click btnInput.Value = "C:\temp\test.txt" 
             pointer = 1 
             Exit For 
        End If 
     Next 
     btnInput 
     If pointer = 1 Then Exit For 
  Next 
End sub


    <button title="Upload Files" class="button button--white xc-action-nav__button ng-binding ng-scope" type="button" loading-key="0" ng-click="setLoading('group1', '0'); " ng-disabled="isLoading('group1', null)"><span class="spinner-transition" ng-class="{'spinner spinner--is-loading': isLoading('group1', '0')}"></span> Upload Files</button>
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
LadyTiara
  • 1
  • 3

2 Answers2

0

Some pointers:

This HTMLButton.Click btnInput.Value = "C:\temp\test.txt" should be on two separate lines.

HTMLButton.Click 
btnInput.Value = "C:\temp\test.txt"

I think the second line is redundant. Your code is missing the more complex instructions required to interact with the file dialog for inputing the filepath. A subject for a whole other question which has been addressed elsewhere on SO.

The "buttons" are input tag elements and you need to limit to the appropriate three.

Dim uploads As Object, i As Long

Set uploads = ie.document.querySelectorAll("#examples [type=file]")

For i = 0 To uploads.Length-1
    uploads.item(i).click
    'other code
Next

My advice would be to try and code with the actual url and share the problems you are having with the actual scenario. The code above, IMO, is not fit for purpose for the url given.

QHarr
  • 83,427
  • 12
  • 54
  • 101
  • Thanks QHarr! For the code you suggested, which code should I replace this? Is it starting from Set 'HTMLbuttons' to 'Next'? Also, what is this '#example' you mentioned on your code. Is this the file path? Thank you very much! – LadyTiara Oct 25 '19 at 11:44
  • Change your page load loop to a proper page load loop with While ie.readyState <> 4 Or ie.Busy:DoEvents:Wend then replace the lines after with the code I suggest in the bottom block. # is a css id selector so it select for the parent element which has id _example_ IIRC this limits the returned children (matched by _[type=file]_ to only the 3 visible upload buttons which needs clicking. – QHarr Oct 25 '19 at 11:47
  • Please see: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors – QHarr Oct 25 '19 at 11:48
  • Hi Qharr. I have not tried it yet. Just saw your reply. I will let you know. – LadyTiara Oct 28 '19 at 17:45
  • :-) Coolio. Thanks for letting me know – QHarr Oct 28 '19 at 17:46
  • Hi QHarr. I just replied. It did not work. What does that run time error mean? – LadyTiara Oct 28 '19 at 18:12
  • I tested it but it is not working. I received Run Time Error '-2147417848 (80010108). THE OBJECT INVOKED HAS DISCONNECTED FROM ITS CLIENTS after I clicked F8 in the Loop line.. Also,I changed the url site to my actual url .What's causing this issue? Thanks for your time! – LadyTiara Oct 28 '19 at 18:36
  • On which line did this occur on? – QHarr Oct 28 '19 at 18:40
  • Below is the new code. The error begins in line 8 which is LOOP. Private Sub CommandButton21_Click()   Dim ie As Object Dim uploads As Object, i As Long   Set ie = CreateObject("internetexplorer.application") ie.Visible = True ie.navigate "http://www.htmlquick.com/reference/tags/input-file.html" Do While ie.readyState <> 4   Loop   Set uploads = ie.document.querySelectorAll("#examples [type=file]")   For i = 0 To uploads.Length - 1     uploads.Item(i).Click     'other code Next     End Sub – LadyTiara Oct 28 '19 at 18:54
  • Sounds like you may need to use InternetExplorerMedium... also make sure all instances of IE are closed when running code and use task manager to ensure all open IE processes relating to this are closed. – QHarr Oct 28 '19 at 18:58
  • Yeah,I did that. I made sure no IE was open. But still same error message. Also I was wondering what code to open 'choose to file' windows dialog box after clicking the Upload Files button? – LadyTiara Oct 28 '19 at 19:06
0

I tested it but it is not working. I received Run Time Error '-2147417848 (80010108). THE OBJECT INVOKED HAS DISCONNECTED FROM ITS CLIENTS after I clicked F8 in the Loop line.. Below is the new code as per your suggestion. What is causing this issue? Apologies if I postes my comment here as a simple Add Comment does not allow me to put the codes. Thanks for your time!

Private Sub CommandButton21_Click() Dim ie As Object Dim uploads As Object, i As Long Set ie = CreateObject("internetexplorer.application") ie.Visible = True ie.navigate "http://www.htmlquick.com/reference/tags/input-file.html" Do While ie.readyState <> 4 Loop Set uploads = ie.document.querySelectorAll("#examples [type=file]") For i = 0 To uploads.Length - 1 uploads.Item(i).Click'other code Next End Sub

 

LadyTiara
  • 1
  • 3
  • You could type `taskkill /F /IM iexplore.exe` in cmd to kill all the IE instance and try it again. If you use `InternetExplorerMedium`, you need to add a reference to `Microsoft Internet Controls`. You could also refer to [this answer](https://stackoverflow.com/questions/26925206/automation-error-when-getting-readystate-of-internetexplorer-object/29349900#29349900) and try the workarounds in it. – Yu Zhou Oct 29 '19 at 07:38
  • Thanks for the link!I was able to solve it. However, the Upload file button action is not working. The codes were provided by QHarr. Below is the updated code. Private Sub CommandButton21_Click() Dim ie As Object Dim uploads As Object, i As Long Set ie = New InternetExplorerMedium ie.Visible = True ie.navigate "http://www.htmlquick.com/reference/tags/input-file.html" While ie.readyState <> 4 DoEvents Wend Set uploads = ie.document.querySelectorAll("#examples [type=file]") For i = 0 To uploads.Length - 1     uploads.Item(i).Click Next End Sub – LadyTiara Oct 30 '19 at 21:22
  • What do you mean "upload file button not working"? Is there any error or something? I test with the above code you provide and it will open the upload file window dialog box and we can choose which file to open to upload. – Yu Zhou Oct 31 '19 at 02:29
  • Sorry for the confusion. I did not post the actual URL here for confidentiality. But when I copied the codes and changed the url to my actual url,the upload file button did not work. There was no error message. It just did not work. – LadyTiara Oct 31 '19 at 21:26
  • Have you added breakpoints to debug? You should also change this line `Set uploads = ie.document.querySelectorAll("#examples [type=file]") ` to fit the real situation of yours. How do you use the querySelector? You could also use the querySelector in console in browser to see if it chooses the right element. – Yu Zhou Nov 01 '19 at 01:56