0

I am trying to input data from Excel workbook to web pages search field and click search button. A lot of different approaches has been tested with success. Current solution gives 424 Error, Object required. Pointing to:

.Document.getElementById("crmGrid_findCriteria").Value = _
      Sheets("Other data").Range("A2").Value

HTML for search field:

<input id="crmGrid_findCriteria" title="Search for records" hintlabelid="crmGrid_findHintText" type="text" class="ms-crm-Dialog-Lookup-QuickFind" maxlength="100" tabindex="0" value="">

HTML for Search button:

<img id="crmGrid_findCriteriaImg" src="/_imgs/imagestrips/transparent_spacer.gif" class="ms-crm-ImageStrip-search " imgbase="/_imgs/search" title="Start search" alt="Start search">

Here is structure:

enter image description here

Here is my VBA (link for CRM system so it is not public):

Sub GoToOpport()

    'This will load a webpage in IE
    Dim ieappIE As InternetExplorerObject
    Dim HWNDSrc As Long
    Dim elementsSet AsappIE Object= CreateObject("internetexplorer.application")

    Set ie = Nothing With appIE
    Set ie = New InternetExplorerMedium      
    ie.Visible = True

    ie.Navigate "https://crm.dynamics.com/main.aspx?area=nav_oppts&etc=3&page=SFA&pageType=EntityList&web=true"

    With ie

    Do
    DoEvents
    Loop Until ie.ReadyState = READYSTATE_COMPLETE   
    End With

ie.Document.getElementById("contentIFrame0").contentDocument.getElementById("crmGrid_findCriteria").Value = "hello"

    'Unload IE
    Set ieappIE = Nothing
End Sub

Error:

enter image description here

10101
  • 2,232
  • 3
  • 26
  • 66
  • Have you checked whether `ie.Document.getElementById("crmGrid_findCriteria") Is Nothing`? Have you tried using an explicit reference to the workbook to which `Sheets("Other data")` belongs? – Stavros Jon Apr 23 '19 at 08:11
  • Depending on what your goal is, have you considered `XmlHttpRequest`? – Brownish Monster Apr 23 '19 at 08:25
  • Additionally, does the search box loads via ajax after page load? –  Apr 23 '19 at 08:30
  • Have a read of [this]9https://stackoverflow.com/questions/26925206/automation-error-when-getting-readystate-of-internetexplorer-object). Specially **Jean-François Corbett** answer – Zac Apr 23 '19 at 09:13
  • @Zac yes. I had it like `Set IE = CreateObject("InternetExplorer.Application")` but then I get Automation error and can not use `ie.ReadyState` – 10101 Apr 23 '19 at 09:24
  • Do you still get a 424 if you step through with F8? – QHarr Apr 23 '19 at 09:53
  • @QHarr yes, still 424. Also I have to use Frame source and not Page source to see source code. I don't know if this is affecting automation somehow? – 10101 Apr 23 '19 at 10:49
  • what do you mean that? Is your element within a frame/iframe? – QHarr Apr 23 '19 at 10:50
  • @QHarr yes, search box is inside frame. For example in Chrome if I use "View page source" I can't see search box in source code but if I choose "View frame source" I can point search box with code in my original question. – 10101 Apr 23 '19 at 11:03
  • ie.document.getElementsByName("frameName")(0).document.getElementById("crmGrid_findCriteria") – QHarr Apr 23 '19 at 11:06
  • ^ something like that where you use name of frame? Or use getElementsByTag("frame")(yourIndexGoesHere).document.getElementById("crmGrid_findCriteria") – QHarr Apr 23 '19 at 11:06
  • 1
    @QHarr I have added structure to original question. Maybe it will help? – 10101 Apr 23 '19 at 11:25

2 Answers2

0

I'm getting a 301 Moved Permanently status when I send a GET request and then I'm redirected here:

https://dynamics.microsoft.com/en-us/crm/what-is-crm/

The id="crmGrid_findCriteria" is nowhere to be found in the HTML response, so my guess is that if you try:

debug.print ie.Document.getElementById("crmGrid_findCriteria") Is Nothing

you'll get TRUE as a result.

In other words, the element you're looking for doesn't exist in the HTML you downloaded.

Stavros Jon
  • 1,695
  • 2
  • 7
  • 17
0

It is in an iframe so you need to access that

ie.document.getElementById("contentIFrame0").contentDocument.getElementById("crmGrid_findCriteria")

You can use js to attempt to assign value

ie.document.parentWindow.execScript "document.getElementById('contentIFrame0').document.getElementById('scrmGrid_findCriteria').value = 'hello';" 

You could also try navigating to the src of the iframe

ie.navigate2 ie.document.querySelector("#crmContentPanel").src
 While ie.Busy Or ie.readyState < 4: DoEvents: Wend 
With ie.document.getElementById("crmGrid_findCriteria")
    .focus
    .value = "abc"
End With
QHarr
  • 83,427
  • 12
  • 54
  • 101
  • Check that contentIFrame0 is the correct id against the source – QHarr Apr 23 '19 at 11:33
  • Now I don't receive any error and process goes forward. However search field is empty for some reason. I have checked that `ThisWorkbook.Sheets("Other data").Range("A2").Value` has value. – 10101 Apr 23 '19 at 14:00
  • Step through with f8 with windows side by side and see if value actually gets input into box first and let me know – QHarr Apr 23 '19 at 14:14
  • I have went through the code with F8 and there are no errors also value is not inputted to search box for some reason. – 10101 May 13 '19 at 10:34
  • ie.document.parentWindow.execScript "document.getElementById('contentIFrame0').document.getElementById('scrmGrid_findCriteria').searchTerm.value = 'hello';" – QHarr May 13 '19 at 10:37
  • ie.document.parentWindow.execScript "document.getElementById('contentIFrame0').document.getElementById('scrmGrid_findCriteria').value = 'hello';" – QHarr May 13 '19 at 10:37
  • I have simplified the code (edited my original question) and now after running it with F8 and ensuring everything is loaded I am getting an error provided in original question. Tried all the solutions. I guess the problem is in iframe and scrmGrid_findCriteria access? – 10101 May 13 '19 at 10:56
  • 1
    After moving `ie.Document.getElementById("contentIFrame0").contentDocument.getElementById("crmGrid_findCriteria").Value = "hello"`out of `With ie` it worked. Edited in my original question as it working now. – 10101 May 13 '19 at 11:15