Too many mistakes: String not correct _1 ; Controlsend?

104 views Asked by At

The string that is supposed to be copied from one program (IE) to my e-mail subject field sometimes contains mistakes:

A messagebox for the variable subject (see below) is however ALWAYS correct:

subject= %protocol% , %LSN% , %Site% , %Requisition%

However when the code runs:

1 out of ten times the string is not correctly put inside the subject field:

I expect:

Protocol C16019 , LSN (102707) , site 22902 , requisition 102921403

I get:

Protocol c16019 (Small letter C instead of capital C) , lSN (&02707° (% instead of 1, ° instead of) ), site é2902 (é instead of 2), requisition &02921403(& instead of 1)

It seems that AHK is using my special caracters (the caracters row below the F1-F12 keys) or that the shift button is getting activated.

Why and how can I resolve this ?

I read somewhere that controlsend doens't always work and that is a known issue.

Sidenote: I am also seeing this in another script where the @ of e-mail addresses is not always put into the send to field

My code:

    Home::

    pwb := WBGet()
    pTable:= pwb.document.GetElementsByTagName("Table") [4] ;outerHTML ;Set Tag name and Array value
    Loop, % pTable.rows.length {
    LSN_1:= pTable.rows[9] .GetElementsByTagName("TD") [1].outerHTML ; LSN

    protocol_1:= pTable.rows[9] .GetElementsByTagName("TD") [3].outerHTML ; Protocol

    Site_1:=pTable.rows[10] .GetElementsByTagName("TD") [1].outerHTML ; Site

    Requisition_1:=pTable.rows[12] .GetElementsByTagName("TD") [3].outerHTML ;   Requisition


    StringTrimLeft, NLSN, LSN_1, 6   ;trims 6 characters from left  ( <TD> >)
    StringTrimRight, fLSN, NLSN, 5   ;trims 5 characters from right (<TD> )

    StringTrimLeft, Nprotocol, protocol_1, 6
    StringTrimRight, fprotocol, Nprotocol, 5


    StringTrimLeft, Nsite, site_1, 6
    StringTrimRight, fsite, Nsite, 5


    StringTrimLeft, NRequisition, Requisition_1, 6
    StringTrimRight, fRequisition, NRequisition, 5


    Requisition= requisition %fRequisition%

    sleep,10

    LSN= LSN (%fLSN%)  ; essential that this variable is put into brackets

    sleep,10

    Site= site %fsite% ; "site" has to be put before the string

    sleep,10

    Protocol= protocol %fprotocol% ;"protocol" has to be put before the string

    sleep,10

    subject= %protocol% , %LSN% , %Site% , %Requisition%

    sleep,150

;the variable in Msgbox is always correct here

    ;send the contents of subject into the subject of outlook
    controlFocus, RichEdit20WPT4, ahk_class rctrl_renwnd32
    controlSend, RichEdit20WPT4, {end} , ahk_class rctrl_renwnd32 ; send to the end of subject field
    controlSendRaw, RichEdit20WPT4, %subject%, ahk_class rctrl_renwnd32
    sleep,100
    subject :=""
    return
    } 

The string is in 10%-20% of the time not correct.

Please note, I am a total newbie, I have been learning and trying write scripts for about 2 weeks now. I do not understand why this script doesn't work 100% of the time.

1

There are 1 answers

0
errorseven On

Is there any reason you are using ControlSendRaw instead of ControlSend? I'm betting this could be your issue.

If you continue to use ControlSendRaw it's suggested you use BlockInput, On and BlockInput, Off surrounding code to limit the interference from Key presses.

Here's a cleaned up version of what you posted above:

Home::

    pwb := WBGet()
    pTable := pwb.document.GetElementsByTagName("Table") [4] ;outerHTML ;Set Tag name and Array value
    LSN_1 := pTable.rows[9] .GetElementsByTagName("TD") [1].outerHTML ; LSN
    protocol_1:= pTable.rows[9] .GetElementsByTagName("TD") [3].outerHTML ; Protocol
    Site_1:=pTable.rows[10] .GetElementsByTagName("TD") [1].outerHTML ; Site
    Requisition_1:=pTable.rows[12] .GetElementsByTagName("TD") [3].outerHTML ;   Requisition  

    subject := "protocol " trimVar(protocol_1) " , LSN (" trimVar(LSN_1) ") , Site " trimVar(Site_1) " ,  Requisition " trimVar(Requisition_1)

    ;send the contents of subject into the subject of outlook
    controlFocus, RichEdit20WPT4, ahk_class rctrl_renwnd32
    controlSend, RichEdit20WPT4, {end} , ahk_class rctrl_renwnd32 ; send to the end of subject field
    BlockInput, On
    controlSendRaw, RichEdit20WPT4, %subject%, ahk_class rctrl_renwnd32
    BlockInput, Off
    sleep,100
    subject :=""
return

trimVar(x) { 
    StringTrimLeft, x, x, 6   ;trims 6 characters from left  ( <TD> >)
    StringTrimRight, x, x, 5   ;trims 5 characters from right (<TD> )
    Return x
}

I didn't see a real need for a loop, left over code from finding the Index of the Rows and Columns perhaps? Or maybe there's a purpose, easy enough to edit them back in. Hope this fixes your Random Character issues. If not, try to use a normal ControlSend instead of the ControlSendRaw, remove the BlockInput...

It seems to me that you might possibly be actively typing an Email in Outlook when you use this Script to insert your text? If this is the case the following the code uses HotStrings to activate it instead of Hotkeys and no longer requires ControlSend:

#IfWinActive ahk_class rctrl_renwnd32 ;The code below will only work if Outlook is the Active application
::insertlsn:: ;Type HotString to activate code below
    pwb := WBGet()
    pTable := pwb.document.GetElementsByTagName("Table") [4] ;outerHTML ;Set Tag name and Array value
    LSN_1 := pTable.rows[9] .GetElementsByTagName("TD") [1].outerHTML ; LSN
    protocol_1:= pTable.rows[9] .GetElementsByTagName("TD") [3].outerHTML ; Protocol
    Site_1:=pTable.rows[10] .GetElementsByTagName("TD") [1].outerHTML ; Site
    Requisition_1:=pTable.rows[12] .GetElementsByTagName("TD") [3].outerHTML ;   Requisition

    subject := "protocol " trimVar(protocol_1) " , LSN (" trimVar(LSN_1) ") , Site " trimVar(Site_1) " ,  Requisition " trimVar(Requisition_1)

    SendInput, %subject%
return 

trimVar(x) { 
    StringTrimLeft, x, x, 6   ;trims 6 characters from left  ( <TD> >)
    StringTrimRight, x, x, 5   ;trims 5 characters from right (<TD> )
    Return x
}

And one final solution would to be use OutLook COM Objects. You have already been working with IE ComObjects so you should be somewhat familiar with the format. Here is an excellent reference if you so choose to implement this method.