Results 1 to 6 of 6
  1. #1
    callmeandy is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2013
    Posts
    3

    VBA and DOM to populate innerHTML on an input tag

    Hi guys dont if anybody in this forum can help as I am not having a lot of luck elsewhere, I am using VBA from Access to automate IE .




    and I have a piece of code that is not playing ball. It's not assigning a value to the innerHTML as expected.


    Code:
    Function PasteToWebPage(txt As String, ie As SHDocVw.InternetExplorer, Optional IfNotExactlyOne As htmlNotExactlyOne = htmlPasteToNone, Optional ElementID As String, Optional ClassName As String, Optional Name As String, Optional TagName As String, Optional href As String, Optional InnerHTML As String) As Long
    Dim hDoc As MSHTML.HTMLDocument
    Dim hCol As MSHTML.IHTMLElementCollection
    Dim hSelect As MSHTML.HTMLSelectElement
    'Dim Target As IHTMLInputElement
    On Error GoTo errorHandler
    
    
    Set hDoc = ie.Document
    
    
    With hDoc
        
        If ElementID <> "" Then
            'ignores any value set for htmlNotExactlyOne
            .getElementByID(ElementID).InnerHTML = txt
            'If Target Is Nothing Then Err.Raise vbObjectError + 513
            'Target.setAttribute "value", txt
    
    
            PasteToWebPage = 1
            Exit Function
        End If
    ...

    I can assign to value i.e. .getElementByID(ElementID).value = txt works fine but doesn't change the visual content on the form - confused which I should use and why its not working.


    Err.Number=600 Err.Description=Application-defined or object-defined error


    Any comments welcome, thanks!

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870

  3. #3
    callmeandy is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2013
    Posts
    3
    Quote Originally Posted by orange View Post
    Well thanks for coming back on this but the first example is in effect what I have done and this is the whole point of my question - its not working.

    The second example I don't think has any bearing at all. I don't have any problem walking around in the DOM, or getting to the node. Indeed I can see from setting a watch that I have correctly got to the correct node in the .getElementById statement. Your first link above has an example that brakes down the problem slightly differently but that accomplishes the same thing - indeed to satisfy this.
    I changed the code thus
    Set Target = .getElementByID(ElementID)

    If I follow that with
    Target.innerHTML = txt what I was intending - it fails and errors as mentioned
    or
    Target.innerText = txt it doesnt error but does not assign a value to innerText - and does to value
    or
    Target.value = txt it assigns the value

    but in no case does it change the appearance of the document, though the watch shows that the DOM has changed!

    Anybody

  4. #4
    redbull's Avatar
    redbull is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480
    Alright, ie automation with vba can be a pain.

    You are just trying to change the value of a text box on a webpage correct?

    and

    getElementbyID.innerHTML = txt is working, meaning debut.print txt shows what you want...

    I guess I don't see where your sending anything into the webpage. Out of curiousity are you using the internet explorer reference library?

    When I try to put something on a webpage it generally looks like this

    oie.document.body.all.item("blah").value = ""
    or to click
    oie.document.body.all.item("blah").click

    I use getlementbyID as well, but sometimes you will find that you are trying to interact with a TD tag and you are limited.

    however there are limitations and annoyances, the biggest one being frames... also javascript can be a pain as well.

  5. #5
    redbull's Avatar
    redbull is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480
    Just for funzies, does this code run?

    Code:
    Function FindIE(strURL As String) As InternetExplorer
      Dim OpenWindows As ShellWindows
      Dim Window As Object
      Set OpenWindows = New ShellWindows
      
      For Each Window In OpenWindows
         If InStr(Window.LocationURL, strURL) > 0 Then
          Set FindIE = Window
        End If
      Next Window
      
      Set Window = Nothing
      Set OpenWindows = Nothing
        
       
    End Function
    
    sub testthisstuff
    
    Dim oie As InternetExplorer
    Set oie = FindIE("theURLyourplayingwith")
    debug.print oie.document.all.body.innertext
    
    end sub

  6. #6
    callmeandy is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2013
    Posts
    3
    Quote Originally Posted by redbull View Post
    Alright, ie automation with vba can be a pain.

    You are just trying to change the value of a text box on a webpage correct?

    and

    getElementbyID.innerHTML = txt is working, meaning debut.print txt shows what you want...

    I guess I don't see where your sending anything into the webpage. Out of curiousity are you using the internet explorer reference library?

    When I try to put something on a webpage it generally looks like this

    oie.document.body.all.item("blah").value = ""
    or to click
    oie.document.body.all.item("blah").click

    I use getlementbyID as well, but sometimes you will find that you are trying to interact with a TD tag and you are limited.

    however there are limitations and annoyances, the biggest one being frames... also javascript can be a pain as well.
    >>getElementbyID.innerHTML = txt is working, meaning debut.print txt shows what you want...
    I was never able to use InnerHTML, NOT working, meaning the DOM attribute is changed after the assignment as evidenced by a watch or debug statement

    >>oie.document.body.all.item("blah").value = ""

    Your statement would have the same result as mine - if it wasn't for the obvious!

    I have solved it now. The problem is not related to the shape of the code but the fact that there is no innerHTML for an INPUT tag, silly me!
    Innertext and value didnt work as it should have because the input tag is type=search and this is running on ie8.

    Ding light comes on! html5 and ie8

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Input Forms - How To Input Multiple Fields/Records?
    By butterbescotch in forum Forms
    Replies: 1
    Last Post: 04-04-2013, 06:30 AM
  2. Accept user input to populate new record
    By bbrazeau in forum Forms
    Replies: 1
    Last Post: 01-03-2012, 05:45 PM
  3. Replies: 8
    Last Post: 12-05-2011, 01:55 PM
  4. Find duplicates query to populate input form
    By kctalent in forum Queries
    Replies: 6
    Last Post: 08-22-2011, 03:12 PM
  5. Replies: 3
    Last Post: 08-25-2010, 09:03 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums