5

I have written a macro that will open a webpage, find the spots I need to put the data into, and then I want the macro to hit a prefill button, then hit Ok.

The page source code for the button is:

<input type="text" size="30" maxlength="40" name="name" id="name">
<input type="button" value="Prefill" onclick="prefill()">

I've been searching for answers all week and I think I have a basic understanding of how this is supposed to work by running a loop to search for it, but I'm having no luck in my endeavor of actually getting this to work.

Can someone show me the loop that will search my page for this button?

Thank you in advance.

Requested code so far

Private Sub Populate_Click()
    Dim i As Long
    Dim IE As Object
    Dim objElement As Object
    Dim objCollection As Object

    Set IE = CreateObject("InternetExplorer.Application")

    IE.Visible = True
    IE.Navigate "website" 'make sure you've logged into the page

    Do
        DoEvents
    Loop Until IE.READYSTATE = 3

    Do
        DoEvents
    Loop Until IE.READYSTATE = 4
    ActiveSheet.EnableCalculation = False
    ActiveSheet.EnableCalculation = True
    Call IE.document.getelementbyid("name").SetAttribute("value", ActiveSheet.Range("b2").Value)
    Call IE.document.getelementbyid("aw_login").SetAttribute("value", ActiveSheet.Range("a2").Value)

    Set objCollection = IE.document.getElementsByTagName("input")
    i = 0
    While i < objCollection.Length
        If objCollection(i).Type = "button" And _
            objCollection(i).Name = "Prefill" Then
                Set objElement = objCollection(i)
        End If
        i = i + 1
    Wend
    objElement.Click
End Sub
Zev Spitz
  • 13,950
  • 6
  • 64
  • 136
Michaeljwjr
  • 423
  • 4
  • 7
  • 13
  • 1
    Can you post the code that you have so far? – sigil Jun 18 '13 at 22:12
  • I'm curious if VBA can be used for a function so alien, so I'll stay tuned. You can probably remove the `macros` tag, and you might want to say what version of Office you're using. – Smandoli Jun 18 '13 at 22:19
  • you find your window using winAPI and send a click –  Jun 18 '13 at 22:28
  • @Smandoli It can be done in VBA, by creating an instance of IE `Set ie = CreateObject("InternetExplorer.Application")`. We can set `Visible` to false, `Navigate` to a page and access `ie.Document`. A sleep method is needed to wait for the page to load. – Andy G Jun 18 '13 at 22:32
  • 1
    Depending on how you're doing it you can just directly call the function inside the button instead of simulating a click on said button. If you're using the IHTMLDocument, you can use `document.ExecScript([javascripthere])`. – NickSlash Jun 18 '13 at 22:36
  • Can you elaborate that nick slash? – Michaeljwjr Jun 18 '13 at 23:05
  • 1
    What does the button do? If e.g. the HTML for the button is: ``, then you can use `document.ExecScript("thisAction()","javascript")` to do whatever the button is supposed to do when clicked. – sigil Jun 18 '13 at 23:20
  • 1
    In the HTML you posted, the button has no `name` attribute, but it does have a `value`. In your loop you're checking for a match on `name` instead of `value`. – Tim Williams Jun 19 '13 at 00:00

1 Answers1

4

Looks like you are pretty close, this is what I have used to do something similiar

    With ie.document

        Set elems = .getelementsbytagname("input")
        For Each e In elems
            If (e.getattribute("className") = "saveComment") Then
                e.Click
                Exit For
            End If
        Next e

    End With

You will probably just have to change the if statement.

I also notice that your code refers to .Name = "Prefill" but your html snippet refers to .value = "Prefill"

Rick
  • 1,063
  • 8
  • 26