1

I am trying to do IE Automation through Excel VBA code. I have a web page which has cells to add value. I need to change values in these cells by code vba excel example: Now cells in webpage has value : "11_now test" My code vba excel:

ie.Document.getelementbyid("mx7125[R:0"]").Value = "Good" ' value i want to set

When run vba , webpage show value at cell is "Good" but when i click save button, the value at cell is "11_now_test" HTML code is as below: <input aria-labelledby="" id="mx7125[R:0]" class="fld text tt" ctype="textbox" li="mx7126[R:0]" db="mx5134" maxlength="200" style=";width:300px;" ontr='true' async='1' ae="setvalue" type="text" title="Remark: 11_now_test" value="11_now_test" ov="11_now_test" work="1" fldInfo='{&quot;length&quot;:&quot;200&quot;,&quot;inttype&quot;:&quot;0&quot;}'/></div><div aria-live="polite" id="mx7126[R:0]_holder" class="bc"><img sf="1" aria-hidden="true" active="0" ctype="image" alt="" src="../webclient/skins/skins-20171010-1330/tivoli09/images/blank.gif" source="blank" imgtype=".gif" style="display:inline;margin:0px;" border="0" lc="mx7125[R:0]" align="absmiddle" width='0px' height='26px' id="mx7126[R:0]" title=""/></div></td>

* More information*

Dear bros when i press F12 to inspector on webpage. If click to change value on webpage, html code at this time is:

<input aria-labelledby="" id="mx7125[R:0]" class="fld text tt" ctype="textbox" li="mx7126[R:0]" db="mx5134" maxlength="200" style=";width:300px;" ontr="true" async="1" ae="setvalue" type="text" title="Remark: 11 Nov test" value="11 Nov test" ov="11 Nov test" work="1" fldinfo="{&quot;length&quot;:&quot;200&quot;,&quot;inttype&quot;:&quot;0&quot;}" originalvalue="11 Nov test" prekeyvalue="good" stoptcclick="true" keydown="false" changed_by_user="true" changed="true"> When type value " Good" to input cell on webpage ,prekeyvalue ="Good" and keydown will turn "True" then keydown back to "false"

* Javascrip at input cell *

when click to change value at input cell i found javascrip :

function tb_(eventOrComponent)
{
    eventOrComponent = (eventOrComponent) ? eventOrComponent : ((window.event) ? window.event : "");
    var eventType = eventOrComponent.type;
    var textbox = this;
    if(undef(eventType) || eventType=="text") {
        eventType = "init";
        textbox = eventOrComponent;
    }
    if(DESIGNMODE)
        return;
    var ro = textbox.readOnly;
    var exc=(textbox.getAttribute("exc")=="1");
    switch(eventType)
    {

    case "init":
        setPromptValue(textbox.id);
        break;
    case "mousedown":
        if(getFocusId()==this.id)
            this.setAttribute("stoptcclick","true");
        break;
    case "mouseup":
        if (isIE() && !hasFocus(this))
        {
            this.focus();
        }
        if (isBidiEnabled)
        {
            adjustCaret(eventOrComponent, this);
        }
        break;
    case "blur":
        input_onblur(eventOrComponent,this);
        if (isBidiEnabled)
            input_bidi_onblur(eventOrComponent, this);
        break;
    case "change":
        if(!ro)
            input_changed(eventOrComponent,this);
        break;
    case "click":
        if(overError(eventOrComponent,this))
            showFieldError(eventOrComponent,this,true);
        var liclick=this.getAttribute("liclick");
        var li=this.getAttribute("li");
        if(li!="" && liclick=="1")
        {
            frontEndEvent(getElement(li),'click');
        }

        if(this.getAttribute("stoptcclick")=="true")
        {
            eventOrComponent.cancelBubble=true;
        }
        this.setAttribute("stoptcclick","false");
        break;
    case "focus":
        input_onfocus(eventOrComponent,this);
        if (isBidiEnabled)
            input_bidi_onfocus(eventOrComponent, this);
        this.select();
        this.setAttribute("prekeyvalue",this.value);
        break;
    case "keydown":
        this.setAttribute("keydown","true");
        if(!ro)
        {
            lastKeyPress = eventOrComponent.keyCode;
            if(eventOrComponent.ctrlKey && hasKeyCode(eventOrComponent,'KEYCODE_SPACEBAR')) {
                stopBubble(eventOrComponent);
                eventOrComponent.cancelBubble=true;
                eventOrComponent.returnValue=false;
                break;
            }       
            if(isBidiEnabled)
                processBackspaceDelete(eventOrComponent,this);
            if(hasKeyCode(eventOrComponent, 'KEYCODE_DELETE') || hasKeyCode(eventOrComponent, 'KEYCODE_BACKSPACE'))
            {
                getHiddenForm().elements.namedItem("changedcomponentvalue").value = this.value;
            }
            if((hasKeyCode(eventOrComponent, 'KEYCODE_TAB') || hasKeyCode(eventOrComponent, 'KEYCODE_ESC')))
            {
                var taMatch = dojo.attr(this, "ta_match");
                if(taMatch) {
                    if(taMatch.toLowerCase().indexOf(this.value.toLowerCase()) == 0)
                    {
                        console.log("tamatch="+taMatch);
                        this.value = taMatch;
                        input_keydown(eventOrComponent, this);
                        dojo.attr(this, {"prekeyvalue" : ""});
                        input_forceChanged(this);
                        inputchanged = false;
                        return; // don't want to do input_keydown again so preKeyValue will work
                    }
                }
                if(this.getAttribute("PopupType"))
                {
                    var popup = dijit.byId(dojohelper.getPopupId(this));
                    if (popup)
                    {
                        dojohelper.closePickerPopup(popup);
                        if(hasKeyCode(eventOrComponent, 'KEYCODE_ESC'))
                        {
                            if (eventOrComponent.preventDefault)
                            {
                                eventOrComponent.preventDefault();
                            }
                            else
                            {
                                eventOrComponent.returnValue  = false;
                            }

                            return;
                        }
                    }
                }
            }
            input_keydown(eventOrComponent,this);
            datespin(eventOrComponent,this);
        }
        else if(hasKeyCode(eventOrComponent,'KEYCODE_ENTER') || (hasKeyCode(eventOrComponent,'KEYCODE_DOWN_ARROW') && this.getAttribute("liclick")))
        {
            var lbId = this.getAttribute("li");
            frontEndEvent(getElement(lbId), 'click');
        }
        else if(hasKeyCode(eventOrComponent,KEYCODE_BACKSPACE))
        {
            eventOrComponent.cancelBubble=true;
            eventOrComponent.returnValue=false;
        }
        break;
    case "keypress":
        if(!ro)
        {
            lastKeyPress = eventOrComponent.keyCode;
            if(eventOrComponent.ctrlKey==false && hasKeyCode(eventOrComponent,'KEYCODE_ENTER'))
            {
                var db = this.getAttribute("db");
                if(db && db!="")
                {
                    input_forceChanged(this);
                    sendClick(db);
                    // IV32363 - When default button is a new row button, then we must set focus on 
                    // the button or the value in text box will be copied to the new row 
                    if (db)
                    {
                        window.setTimeout("focusElement(document.getElementById('"+db+"'))", 10);
                    }
                }
            }
        }
        break;
    case "keyup":
        var keyDown = this.getAttribute("keydown");
        this.setAttribute("keydown","false");
        if(eventOrComponent.ctrlKey && hasKeyCode(eventOrComponent,'KEYCODE_SPACEBAR'))
        {
            if(showFieldError(eventOrComponent,this,true))
            {
                return;
            }
            else
            {
                menus.typeAhead(this,0);
            }
        }
        if(!ro)
        {
            if(isBidiEnabled)
                processBidiKeys(eventOrComponent,this);

            numericcheck(eventOrComponent,this);
            var min = this.getAttribute("min");
            var max = this.getAttribute("max");

            if(min && max && min!="NONE" || max!="NONE")
            {
                if(min!="NONE" && parseInt(this.value)<parseInt(min))
                {
                    this.value=min;
                    getHiddenForm().elements.namedItem("changedcomponentvalue").value = this.value;
                    this.select();
                    return false;
                }

                if(max!="NONE" && parseInt(this.value)>parseInt(max))
                {
                    this.value=max;
                    getHiddenForm().elements.namedItem("changedcomponentvalue").value = this.value;
                    this.select();
                    return false;
                }
            }
            var defaultButton = false;
            if(eventOrComponent.ctrlKey==false && hasKeyCode(eventOrComponent,'KEYCODE_ENTER'))
            {
                var db = this.getAttribute("db");
                if(db && db!="")
                {
                    defaultButton=true;
                }
            }
            input_changed(eventOrComponent,this);
        }
        else
        {
            setFocusId(eventOrComponent,this);
        }
        if(showFieldHelp(eventOrComponent, this))
        {
            return;
        }
        if(keyDown=="true" && hasKeyCode(eventOrComponent, 'KEYCODE_ENTER') && !eventOrComponent.ctrlKey && !eventOrComponent.altKey)
        {
            menus.typeAhead(this,0);
            return;
        }
        if(!hasKeyCode(eventOrComponent, 'KEYCODE_ENTER|KEYCODE_SHIFT|KEYCODE_CTRL|KEYCODE_ESC|KEYCODE_ALT|KEYCODE_TAB|KEYCODE_END|KEYCODE_HOME|KEYCODE_RIGHT_ARROW|KEYCODE_LEFT_ARROW')
                && !eventOrComponent.ctrlKey && !eventOrComponent.altKey)
        {
            menus.typeAhead(this,0);
        }
        break;
    case "mousemove":
        overError(eventOrComponent,this);
        break;
    case "drop":
        input_onfocus(eventOrComponent,this);
        if (isBidiEnabled)
        {
            input_bidi_onfocus(eventOrComponent, this);
        }
        this.select();
        if(!ro) 
        {
            this.setAttribute("prekeyvalue",this.value);
        }
    case "cut":
    case "paste":
        if(!ro)
        {
            var fldInfo = this.getAttribute("fldInfo");
            if(fldInfo)
            {
                fldInfo = dojo.fromJson(fldInfo);
                if(!fldInfo.query || fldInfo.query!=true)
                {
                    setButtonEnabled(saveButton,true);
                }
            }
            window.setTimeout("inputchanged=true;input_forceChanged(dojo.byId('"+this.id+"'));", 20);
        }
        break;
    }
}

* HIDDEN FORM *

Dear @Zhi Lv - MSFT ! i tried sendkeys method but not success! i found the same problem at this link. Extractly that i want to change value in maximo website, has hiddenform but at that linked they use javascript, don't use IE automation vba excel.When i inspect at input cells and type document.getelementbyid("hiddenform") , found this hiddenform:

<form aria-hidden="true" id="hiddenform" name="hiddenform" method="POST" action="http://maximo.mysite.com/maximo/ui/maximo.jsp" style="padding:3px;">
        <input type="text" size="45" name="event" id="event" title="event type"><br>
        <input type="text" size="45" name="targetid" id="targetid" title="target id"><br>
        <input type="hidden" size="45" name="value" id="value" value=""><!-- this must be type hidden to support \n in the value -->
        <input type="text" size="45" name="changedcomponentid" id="changedcomponentid" title="changed component id"><br>
        <input type="text" size="45" name="vischangedcomponentvalue" id="vischangedcomponentvalue" title="changed component value"><br>
        <input type="hidden" name="changedcomponentvalue" value="">
        <input type="text" size="45" name="currentfocus" id="currentfocus" title="focus id"><br>
        <input name="scrollleftpos" id="scrollleftpos" size="45" title="Scroll Left"><br>
        <input name="scrolltoppos" id="scrolltoppos" size="45" title="Scroll Top"><br>
        <input type="text" size="45" name="uisessionid" id="uisessionid" value="4770" class="fld_ro" readonly="readonly" title="ui session id"><br>
        <input type="text" size="45" name="csrftokenholder" id="csrftokenholder" value="ocvdjoiuq2ht8detn26pkjeekg" title="CSRF Token" readonly="readonly" class="fld_ro"><br>
    </form>

thanks!!!

Xpbk
  • 11
  • 4

2 Answers2

0

I'am sure there are events to the input fields. Like onChange(), onFocus(), onBlur() and/ or others. You can find out it if you press F12 on the tab with the opened page. Track through the tree of the DOM Inspector. In the row with your code snippet will be a button with the caption "event" at end of line. With a click on the button you can see which events there are.

In the past it was possible to trigger an event with fireEvent(). Another way is dispatchEvent() with some other methods like initEvent().

The words between [] are "outdated" ;-) [What ever I tryed in the past, nothing worked. So I can't tell how you can solve your problem but I' am sure it is an event problem.]

This link shows you how it should work: Vba, HTMLSelect: FireEvent OnChange or DispatchEvent

The words between [[]] are not relevant for IE11 [[Another more modern way is *addEventListener()*. But I don't know with which method the IE11 is working: EventTarget.addEventListener() The problem for me with *addEventListener()* is, the IE has not eventTarget() Constructer (see browser compatibility table at the bottom of the page): EventTarget()]]

Edit 1:

In IE11 it works with dispatchEvent() and the other relevant things. You must place all commands in an own method. Two examples:

For onChange() use:

Private Sub Event_onChange(htmlDocument As Object, htmlElementWithEvent As Object)

  Dim Event_onChange As Object

  htmlElementWithEvent.Focus
  Set Event_onChange = htmlDocument.createEvent("HTMLEvents")
  Event_onChange.initEvent "change", True, False
  htmlElementWithEvent.dispatchEvent Event_onChange
End Sub

For onKeyDown() use :

Private Sub Event_onKeyDown(htmlDocument As Object, htmlElementWithEvent As Object)

  Dim Event_onKeyDown As Object

  htmlElementWithEvent.Focus
  Set Event_onKeyDown = htmlDocument.createEvent("HTMLEvents")
  Event_onKeyDown.initEvent "onkeydown", True, False
  htmlElementWithEvent.dispatchEvent Event_onKeyDown
End Sub

I know that now because we disscuss this right now in a german forum. The solution with the own sub() is from the user Anton (not involved in the discussion). With Antons sub() I figure out right now how the German Post will let a macro insert adresses in an order blank.

The german discussion: Webformular (DHL) ausfüllen

Or after one week in the archive: Webformular (DHL) ausfüllen (archive thread)

Edit 2:

I think you must trigger the onKeyDown event. Try the following:

Dim nodeInput As Object
Set nodeInput = ie.Document.getelementbyid("mx7125[R:0]")
Call TriggerEvent(ie.Document, nodeInput, "onKeyDown")
nodeInput.Value = "Good"

Here is the function to trigger an event like QHarr wrote in the comments with the event type as third function parameter:

Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)

  Dim theEvent As Object

  htmlElementWithEvent.Focus
  Set theEvent = htmlDocument.createEvent("HTMLEvents")
  theEvent.initEvent eventType, True, False
  htmlElementWithEvent.dispatchEvent theEvent
End Sub

In the above linked german forum the Deutsche Post form works now with this function. But it is necessary to put in all values two times.

We don't know anything about your page. The url seems to be a secret. If you need more help we need more infos.

Zwenn
  • 2,147
  • 2
  • 8
  • 14
  • 1
    Nice idea. You could make this more flexible by passing the event type as a param to the sub. – QHarr Nov 12 '19 at 19:31
  • thanks !! but still not working. Could you please explain more – Xpbk Nov 13 '19 at 00:34
  • Dear Zwenn!! You know about maximo? i found the same problem at link [link](https://stackoverflow.com/questions/10891556/javascript-force-an-onchange-in-maximo/16693743#16693743), but now still not working for me! I want to change value at input cell but still stack, Thanks – Xpbk Nov 15 '19 at 15:41
0

How do you get the input text value when click the save button? I have created a sample using the following code, it works well on my side. Please refer to it.

IE.Document.getelementbyid("mx7125[R:0]").Value = "Good"

And web page resource as below (using javascript script get the value document.getElementById('mx7125[R:0]').value):

<div>
<input aria-labelledby="" id="mx7125[R:0]" class="fld text   tt" ctype="textbox" li="mx7126[R:0]" 
       db="mx5134" maxlength="200" style="width:300px;" ontr='true' async='1' ae="setvalue" type="text" 
       title="Remark: 11_now_test" value="11_now_test" ov="11_now_test" work="1" fldInfo='{&quot;length&quot;:&quot;200&quot;,&quot;inttype&quot;:&quot;0&quot;}' />
</div>
<div aria-live="polite" id="mx7126[R:0]_holder" class="bc">
<img sf="1" aria-hidden="true" active="0" ctype="image" alt=""  src="../webclient/skins/skins-20171010-1330/tivoli09/images/blank.gif" source="blank" imgtype=".gif" 
     style="display:inline;margin:0px;" border="0" lc="mx7125[R:0]" align="absmiddle" width='0px' height='26px' id="mx7126[R:0]" title="" /></div> 

<input id="btnSubmit" type="button" value="Submit" onclick="Javascript: alert(document.getElementById('mx7125[R:0]').value);" />

If still not working, please post the related code about the save button.

Zhi Lv
  • 18,845
  • 1
  • 19
  • 30