Sending text from Excel VBA to ServiceNow Text Area

Farcal

New Member
Joined
May 12, 2019
Messages
2
Hello Everyone!

I'm trying to fill out text area in ServiceNow ticketing tool using VBA, but sadly I've been stuck for a couple of days now..

So far I've been successfully using this method and this exact (excluding site's tittle and text area ID) code to fill out text area in different ticketing tool, however in ServiceNow it's not doing anything for some reason.

it does seem to hook into the page just fine, as I can get the return value on the tittle (opening new IE instance with VBA and hooking to it also haven't helped), but while trying to set a value of text area (description field, or any other for that matter) it doesn't send any text over + it also doesn't return any value for any of the other ID's on the web page.

I've tried doing it in few different ways like using .innerHTML, .Value and .text but none seem to either send or return any value, setting it as an object, sending it with pure IE.document.getElementById("incident.comments").value = "test" but to no anvil.


Any pointers on what I might be doing wrong would be greatly appreciated - thank you very much in advance.


Code:
<code>Sub Test()



    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")



    marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For X = 0 To (IE_count - 1)
    On Error Resume Next
    my_url = objShell.Windows(X).******************
    my_title = objShell.Windows(X).document.Title

    If my_title Like "INC0010005" & "*" Then
        Set IE = objShell.Windows(X)
        marker = 1
        Exit For
    Else
    End If
Next


If marker = 0 Then
    MsgBox ("Website not found")
    Exit Sub
End If


'trying to send "test" text into the field, nothing comes up
   Set notesObj = IE.document.getElementById("incident.comments")
   notesObj.Value = "test"

   tests = IE.document.getElementById("incident.comments").innerHTML

'testing for return value (comes up as empty message box each time)
   MsgBox (tests)


Set evt = IE.document.createEvent("keyboardevent")
evt.initEvent "change", True, False
    notesObj.dispatchEvent evt



End Sub</code>


Text area:

Code:
<code><textarea name="incident.comments" class="form-control" id="incident.comments" aria-required="false" style="height: 64px; overflow: hidden; -ms-overflow-y: hidden; -ms-word-wrap: break-word;" spellcheck="true" onkeydown="multiKeyDown(this);;" onkeyup="multiModified(this);fieldTyped(this);" onkeypress="" onfocus="this.isFocused=true;" onblur="this.isFocused=false;" onchange="this.isFocused=false;multiModified(this)" wrap="soft" autocomplete="off" data-type="glide_journal_input" data-ref="incident.comments" data-charlimit="false" data-length="4000"></textarea></code>
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Make coding easier by using early binding of Microsoft HTML Object Library (Tools -> References in the VBA editor). Then you can use all the HTML data types and have intellisense editing.

Try this code (I'm guessing that FireEvent, instead of dispatchEvent, might work).
Code:
    Dim textInput As HTMLTextAreaElement
    Dim i As Long, t As Single
    Dim yourComments As String
    
    yourComments = "Comments string"
    Set textInput = IE.document.getElementById("incident.comments")
    textInput.Focus
    textInput.Click
    textInput.Value = ""
    For i = 1 To Len(yourComments)
        textInput.Value = textInput.Value & Mid(yourComments, i, 1)
        textInput.FireEvent "onkeydown"
        t = Timer: While Timer < t + 0.2: DoEvents: Wend
        textInput.FireEvent "onkeyup"
        t = Timer: While Timer < t + 0.2: DoEvents: Wend
    Next
    textInput.FireEvent "onchange"
    textInput.Blur
 
Upvote 0
Make coding easier by using early binding of Microsoft HTML Object Library (Tools -> References in the VBA editor). Then you can use all the HTML data types and have intellisense editing.

Try this code (I'm guessing that FireEvent, instead of dispatchEvent, might work).
Code:
    Dim textInput As HTMLTextAreaElement
    Dim i As Long, t As Single
    Dim yourComments As String
    
    yourComments = "Comments string"
    Set textInput = IE.document.getElementById("incident.comments")
    textInput.Focus
    textInput.Click
    textInput.Value = ""
    For i = 1 To Len(yourComments)
        textInput.Value = textInput.Value & Mid(yourComments, i, 1)
        textInput.FireEvent "onkeydown"
        t = Timer: While Timer < t + 0.2: DoEvents: Wend
        textInput.FireEvent "onkeyup"
        t = Timer: While Timer < t + 0.2: DoEvents: Wend
    Next
    textInput.FireEvent "onchange"
    textInput.Blur


Hello John and thank you for the reply,

I've enabled the HTML Object Library as You've suggested and tried using Your code but sadly still nothing showed up in the desired text area and I'm still unable to get any value's on anything inside the ServiceNow, aside of the IE tab tittle :(
 
Upvote 0
Really difficult to help you. Any errors? Is textInput referencing the correct element? What events does F12 DOM inspector show on the element, or parent elements? You might need to fireEvent(s) on element(s) higher in the hierarchy.

Maybe dispatchEvent would work:

Code:
    Dim HTMLdoc As HTMLDocument
    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then
        Dim evtKeyDown As DOMKeyboardEvent
        Dim evtKeyUp As DOMKeyboardEvent
        Dim evtChange As DOMEvent
    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
        Dim evtKeyDown As Object
        Dim evtKeyUp As Object
        Dim evtChange As Object
    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If

    Dim textInput As HTMLTextAreaElement
    Dim i As Long, t As Single
    Dim yourComments As String
    
    yourComments = "Comments string"
    Set HTMLdoc = IE.document
    
    Stop
    'Ensure document is completely loaded before creating events
    
    Set evtKeyDown = HTMLdoc.createEvent("KeyboardEvent")
    Set evtKeyUp = HTMLdoc.createEvent("KeyboardEvent")
    Set evtChange = HTMLdoc.createEvent("HTMLEvents")
    evtKeyDown.initEvent "keydown", True, False
    evtKeyUp.initEvent "keyup", True, False
    evtChange.initEvent "change", True, False

    Set textInput = IE.document.getElementById("incident.comments")
    textInput.Focus
    textInput.Click
    textInput.Value = ""
    For i = 1 To Len(yourComments)
        textInput.Value = textInput.Value & Mid(yourComments, i, 1)
        textInput.dispatchEvent evtKeyDown
        t = Timer: While Timer < t + 0.2: DoEvents: Wend
        textInput.dispatchEvent evtKeyUp
        t = Timer: While Timer < t + 0.2: DoEvents: Wend
    Next
    textInput.dispatchEvent evtChange
    textInput.Blur
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top