Results 1 to 13 of 13
  1. #1
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'd like to automatically enter a value in a field on a very simple (internal) webpage. On this link: http://www.codeforexcelandoutlook.com/exce...ernet-explorer/ I found some VBA code but the piece where it says:

    Set UserN = appIE.document.getElementsByName("Username")

    does not work for me (I get a Type Mismatch - yes, I tried "uname" here too). The input is a field on the webpage, not a diaplog box by the way. This is an excerpt from the HTML-code from the webpage:

    Username: <input type=text name=uname> (which seems embedded in a <FORM ... > structure)

    What code should I use to read input from this field and/or write output to this field then?

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    [quote name='ErikJan' post='786986' date='31-Jul-2009 06:55']Set UserN = appIE.document.getElementsByName("Username")

    does not work for me (I get a Type Mismatch - yes, I tried "uname" here too).[/quote]
    You are correct to change the name of the field from "Username" to "uname" in your code, to match the form.

    The getElementsByName method returns a collection, which is why the sample code declares UserN as a variant. I think it should be impossible to get a type mismatch when using a variant. If you changed it to an object, string array, or other variable type, try changing back to variant.

    By the way, are you using IE8? There are some changes in IE8 that might affect DOM methods. I haven't researched whether this is one of the ones that changed.

  3. #3
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='jscher2000' post='787038' date='31-Jul-2009 22:06']You are correct to change the name of the field from "Username" to "uname" in your code, to match the form.

    The getElementsByName method returns a collection, which is why the sample code declares UserN as a variant. I think it should be impossible to get a type mismatch when using a variant. If you changed it to an object, string array, or other variable type, try changing back to variant.

    By the way, are you using IE8? There are some changes in IE8 that might affect DOM methods. I haven't researched whether this is one of the ones that changed.[/quote]
    Don't know what was wrong but after rechecking the code (and reloading Excel); now it works.

    Which brings me to a next step; the idea is that the user enters a name and then clicks an "OK" button (I changed the black text):

    <FORM method=post action="http://[an ip adress]" target="[a window title]">Username: <input type=text name=uname><input type=submit name=ok value=OK onClick="return submitreload()"></FORM>

    My code has:
    Set ElementCol = appIE.document.getElementsByTagName("submit")
    For Each btnInput In ElementCol
    If btnInput.Value = "OK" Then
    btnInput.Click
    Exit For
    End If
    Next btnInput

    But the collection remains empty. When I try Set ElementCol = appIE.document.getElementsByName("ok") it works but how to I mimic clicking then?

    PS the html is generated; I need to work with it but cannot change it.

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by ErikJan' post='787291 View Post
    When I try Set ElementCol = appIE.document.getElementsByName("ok") it works but how to I mimic clicking then?
    That would be more efficient. Can you simply invoke the click method on the button using something like this?
    Code:
    ElementCol(1).click()

  5. #5
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='jscher2000' post='787367' date='03-Aug-2009 20:52']The actual tag name for a submit button is input which will gather text fields, checkboxes, etc., but I suspect that none of the others will have a value of OK.


    That would be more efficient. Can you simply invoke the click method on the button using something like this?
    Code:
    ElementCol(1).click()
    [/quote]
    Great thanks again... I'm making slow but good progress and every day I'm getting closer to the end result. I'm learning how this works along the way and have already taken some more hurdles myself.
    There's (I think) one last step to take and I hope you can also provide me with info on how to cope with that. In one of the phases, the webpage actually opens a new instance of IE in which it reports something. All I need is to aknowledge that (and now I know how); problem is, how do I get the window handle? For the start of the code, I opened a window myself using Set appIE = New InternetExplorer but now I need an appIE2 that represents an existing window. I think the window-title is fixed so I guess that could help me get it. Any suggestions?

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    [quote name='ErikJan' post='787460' date='04-Aug-2009 02:48']In one of the phases, the webpage actually opens a new instance of IE in which it reports something. All I need is to aknowledge that (and now I know how); problem is, how do I get the window handle? For the start of the code, I opened a window myself using Set appIE = New InternetExplorer but now I need an appIE2 that represents an existing window. I think the window-title is fixed so I guess that could help me get it. Any suggestions?[/quote]
    I think it is difficult to manipulate an instance of IE that was not launched from within your code. You may be able to find the window using standard Windows API methods, but for security reasons, I think IE will block attempts to interact with the document when you approach it from that direction. It has been several years since I tried that, so my memory may be faulty.

    If you insert a Stop into your code after the point that the new window is launched, and check out the local variables window, can you find a windows or documents collection under appIE, or any other collection, that would give you access to the new window/document?

  7. #7
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='jscher2000' post='787515' date='04-Aug-2009 17:29']I think it is difficult to manipulate an instance of IE that was not launched from within your code. You may be able to find the window using standard Windows API methods, but for security reasons, I think IE will block attempts to interact with the document when you approach it from that direction. It has been several years since I tried that, so my memory may be faulty.

    If you insert a Stop into your code after the point that the new window is launched, and check out the local variables window, can you find a windows or documents collection under appIE, or any other collection, that would give you access to the new window/document?[/quote]


    Appie.locationname (whatever that is); seems to list the content of the little pop-up window; I cannot get to the "Done" button on there as the collection I see is for the underlying main window...

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    [quote name='ErikJan' post='787644' date='05-Aug-2009 06:26']Appie.locationname (whatever that is); seems to list the content of the little pop-up window; I cannot get to the "Done" button on there as the collection I see is for the underlying main window...[/quote]
    I think the Location Name is the <title> of the original page you launched, or whatever is now in that tab.

    If you can get the hWnd from the Windows API, you might be able to get to the page after all. Check out the following page. But I'm not sure how to turn that into VBA.

    jeffdav on code: the internet explorer object cookbook

    (1) IHTMLDocument2 from HWND [bottom of page]
    (2) (if needed) IWebBrowser2 from IHTMLDocument2 [a little further up]

  9. #9
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='jscher2000' post='787701' date='06-Aug-2009 01:37']I think the Location Name is the <title> of the original page you launched, or whatever is now in that tab.

    If you can get the hWnd from the Windows API, you might be able to get to the page after all. Check out the following page. But I'm not sure how to turn that into VBA.

    jeffdav on code: the internet explorer object cookbook

    (1) IHTMLDocument2 from HWND [bottom of page]
    (2) (if needed) IWebBrowser2 from IHTMLDocument2 [a little further up][/quote]

    Just a quick update - this is still open and I need to find some time to try your suggestions. I promise that I will report back but this might take a while.

  10. #10
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='jscher2000' post='787701' date='06-Aug-2009 01:37']I think the Location Name is the <title> of the original page you launched, or whatever is now in that tab.

    If you can get the hWnd from the Windows API, you might be able to get to the page after all. Check out the following page. But I'm not sure how to turn that into VBA.

    jeffdav on code: the internet explorer object cookbook

    (1) IHTMLDocument2 from HWND [bottom of page]
    (2) (if needed) IWebBrowser2 from IHTMLDocument2 [a little further up][/quote]

    OK, I have added some code that indeed finds the (IE) window and returns the handle to me.
    So how do I now add code that interfaces with this window (all I need is press a button); I don't want to stop interfacing with my original window (which I opened with: Set appIE = New InternetExplorer) so I guess I want to set a new variable ("Set appIE2 = ???") that then allows me to interface with the new window. I think the handle will help me but how??

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    [quote name='ErikJan' post='788982' date='14-Aug-2009 07:23']OK, I have added some code that indeed finds the (IE) window and returns the handle to me.
    So how do I now add code that interfaces with this window (all I need is press a button); I don't want to stop interfacing with my original window (which I opened with: Set appIE = New InternetExplorer) so I guess I want to set a new variable ("Set appIE2 = ???") that then allows me to interface with the new window. I think the handle will help me but how?? [/quote]
    Sorry, I can't help with translating from C/C# code/function calls, if that's what those are, to VB.

  12. #12
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='jscher2000' post='789030' date='14-Aug-2009 19:29']Sorry, I can't help with translating from C/C# code/function calls, if that's what those are, to VB.[/quote]


    No, this is the next step in the VBA code I already have... (I didn't go into the C-code myself but I did find VB code to get the hWnd from the new explorer window

  13. #13
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='ErikJan' post='789253' date='16-Aug-2009 22:59']No, this is the next step in the VBA code I already have... (I didn't go into the C-code myself but I did find VB code to get the hWnd from the new explorer window[/quote]

    In summary:

    Here's (parts of) the code (source: http://www.codeforexcelandoutlook.com/exce...ernet-explorer/) I'm using to open and control an IE window (this example clicks the OK-button):

    Dim appIE As InternetExplorer
    Dim Element As HTMLButtonElement
    Dim btnInput As MSHTML.HTMLInputElement
    Dim ElementCol As MSHTML.IHTMLElementCollection
    '
    Set appIE = New InternetExplorer 'Open new IE screen
    '
    With appIE
    .Navigate "
    http://somewhere/"
    .Visible = True 'Watch the IE instance on screen
    End With
    Do While appIE.Busy: Loop ' loop until the page finishes loading

    'Click button
    Set ElementCol = appIE.document.getElementsByTagName("input")
    For Each btnInput In ElementCol
    If btnInput.Value = "OK" Then
    btnInput.Click
    Exit For
    End If
    Next

    This works fine. Now somewhere in the process, a new IE-window opens. I can get the Windows-handle for that window, and all I now want is to Click a similar OK-button in that new window. I guess that in stead of doing: Set appIE = New InternetExplorer to open a new IE window, I just think I need to use the window-handle to do a new Set appIE2 = <????>

    Any suggestions would be apreciated

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •