0

I'm currently trying to automate my IE at work with VBA and Excel, because we have some repetitive tasks to complete in an SAP System which is called within the Internet Explorer.
So far I am able to start IE and navigate to the starting page of our System.

I looked on the source code from the Sap System to search for an id from a div (lets say its 'SampleID'). But if I try to click it via VBA I only get returned 'nothing'. So I tried to print out the source code via VBA Debug.Print to look at the problem. And here's the thing:

The source code that gets printed is completely different from the Website's Source Code. In the printed code for example is a iframe that I can't find in the website source code. So that's the reason I can't find my div and any other objects, because they are not in the code that VBA is searching through. Can anyone tell me what's going wrong? Why are the source codes completely different?

Sub stgGo(){
 Dim i As Long
 Dim IE As InternetExplorerMedium
 Dim objElement As Object
 Dim objCollection As Object  

 Set IE = New InternetExplorer
 IE.Visible = True
 IE.Navigate ("URL")

'waiting 10 seconds to be really sure javascript is ready
 Application.Wait (Now + TimeValue("0:00:10")) 

'printing out the false source code
 Debug.Print strHTML = myIE.Document.body.innerHTML 

 Set objCollection = IE.document.getElementsByTagName("div")
 i = 0
 While i < objCollection.Length
  If objCollection(i).ID = "SampleID" Then
   objCollection(i).Click
  End If
  i = i + 1
 Wend
End Sub
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 1
    Is this missing `"` after `div` your typo or the original script error? – Styx Oct 25 '17 at 18:10
  • just a typo. i get no compiler errors :) – NathanPalme Oct 25 '17 at 18:17
  • Why are you search the collection and not using `Dim div As HTMLDivElement` and `Set div = IE.document.getElementById("SampleID")`. Are there multiple elements withthe same ID? –  Oct 25 '17 at 18:26
  • You are right, that would be more reasonable. But it should work like this too – NathanPalme Oct 25 '17 at 18:29
  • You cannot exit from a While ... Wend loop unless you use a GoTo. Maybe switch to a For ... Next so you can Exit For after the click. –  Oct 25 '17 at 18:39
  • Yeah but it doesn't even search the right source code, so it wont work with a For...Next either. But thanks for the tip! – NathanPalme Oct 25 '17 at 18:42
  • 1
    Then maybe [search this site](https://stackoverflow.com/search?q=%5Bvba%5Diframe+div) for *vba iframe div* and come up with something like [this](https://stackoverflow.com/questions/44902558/accessing-object-in-iframe-using-vba). –  Oct 25 '17 at 18:43
  • Don't use "view source" for this - open up your browser's Developer tools (F12) and use the tools there to view the as-rendered HTML. Or in IE just right-click the element you want to work with and select "inspect element" – Tim Williams Oct 25 '17 at 19:08
  • Well I dont have the source code for the page you trying to work with, but i normally would something like this: `Set objCollection = ie.Document.getElementsByTagName("input") While i < objCollection.Length If objCollection(i).Name = "ctl00$MainContent$txtUsername" Then objCollection(i).Value = "MyUsername" End If i = i + 1 Wend` – T. Nesset Oct 26 '17 at 06:14
  • Its more or less the same as you do, but I run into problems when I use "div". I would also normally wait for the while..wend to be complete before I `.click`. you could store the SampleID in a new object `Set objElement = objCollection(i)` and when your while..wend is done I would `objElement.Click` – T. Nesset Oct 26 '17 at 06:23

0 Answers0