2

I am quite new to coding and have the following issue: I want to write a macro, that goes to the website: https://displaypurposes.com/ inserts a keyword and copy the results into my excel sheet.

This is how my code looks at the moment:

Sub DisplayPurposes()


Dim objIE As InternetExplorer
Dim aEle As HTMLLinkElement
Dim Url As String
Dim Keyword As String

Url = "https://displaypurposes.com/"
Keyword = "skiing"

'initiating a new instance of Internet Explorer and assigning it to objIE
Set objIE = New InternetExplorer

'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True

'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate (Url)

'Wait for IE to load page
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

'BOTH OF THESE INSERT THE KEYWORD
'Insert Version 01
objIE.document.getElementsByTagName("input")(0).innerText = "Keyword"
'Insert Version
objIE.document.getElementsByTagName("input")(0).Value = "Keyword"

'NOW I DON'T KNOW HOW TO MAKE THE PAGE UPDATE ITSELF

End Sub

I figured out that I might be able to solve the problem with .fireEvent(), Sendkeys or the solution posted by dyanisis2

Set evt = objIE.Document.createEvent("keyboardevent")
    evt.initEvent "change", True, False
    PW.all(0).dispatchEvent evt

However, I didn't manage to make the page show the searchresults for the keyword so I could scrape them. Since I am quite new to the community I hope I posted everything according to the guidelines and answered everything you need to know!

Websitewichtel
  • 125
  • 3
  • 10

2 Answers2

1

Looking at the way the search for this web page works, is that it requires one letter at a time to complete the search.

You can set the focus to the 'input' tag name and then use send keys to complete the search:

objIE.document.getElementsByTagName("input")(0).Focus
Application.SendKeys "s"
Application.SendKeys "e"
Application.SendKeys "a"
Application.SendKeys "r"
Application.SendKeys "c"
Application.SendKeys "h"

Now you know how it works, you can use this code so that it will work with the keyword value:

objIE.document.getElementsByTagName("input")(0).Focus
Dim i As Integer
Dim EachLetter As Variant
     EachLetter = Split(Keyword, " ")
For i = 0 To UBound(EachLetter)
    Application.SendKeys EachLetter(i)
Next i
5202456
  • 966
  • 14
  • 24
  • Thank you very much for your analysis and code! You were absolutly right And I now I got the code to run! – Websitewichtel Feb 28 '18 at 11:15
  • Since I read that the Sendkeys method should be avoided in general and I am eager to always imorove my skills I was wondering if there was a solution without using the sendkeys method. – Websitewichtel Feb 28 '18 at 11:17
  • In this instance, sendkeys is not too much of an issue, as the focus is already on the input text box but it still can glitch. If you where to use sendkeys as keyboard navigation through a page, then it would be less reliable. There are also issues when using IE7 (who uses that now a days anyway) and also can be slow in some other versions of IE. To help anyone else viewing this question looking for a similar answer, then you can accept it as answered, this does not have to be my answer but any answer you think is the best solution for you. – 5202456 Feb 28 '18 at 11:58
0

To trigger the fireEvent on the field, you have to type your string (you've already figured that out). Before you type, set the focus to the field:

objIE.document.getElementsByTagName("input")(0).Focus

I would add a click in there as well just to be safe

objIE.document.getElementsByTagName("input")(0).Click

Now all you have to do is send the keys:

Dim sMyString As String: sMyString = "#mystring"
Dim iC As Integer
For iC = 1 To Len(sMyString)
    Application.SendKeys Mid(sMyString, iC, 1)
Next
Zac
  • 1,924
  • 1
  • 8
  • 21
  • Since I read that the Sendkeys method should be avoided in general and I am eager to always imorove my skills I was wondering if there was a solution without using the sendkeys method. – Websitewichtel Feb 28 '18 at 11:17