# Sending text from Excel VBA to ServiceNow Text Area



## Farcal (May 12, 2019)

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>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><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>
```


----------



## John_w (May 13, 2019)

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).

```
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
```


----------



## Farcal (May 13, 2019)

John_w said:


> 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).
> 
> ...




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


----------



## John_w (May 14, 2019)

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:


```
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
```


----------



## John_w (May 14, 2019)

Cross-posted on several forums.  In accordance with MrExcel's rules (rule 13), please post links to your posts on the other forums.


----------

