Insert String at Cursor Position

Add-in Express™ Support Service
That's what is more important than anything else

Insert String at Cursor Position
Addin for Outlook. Insert a string at current cursor position, same way as if user types it in 
SECUDOS SECUDOS




Posts: 4
Joined: 2020-09-24
Hi Support Team,

we have an outlook addin, based on "Add-in Express".

I'd like to insert a string at cursor position. When the user is in compose mode for a new mail.

The result should be the same, as if the user types in the string.


I found this thread.
https://www.add-in-express.com/forum/read.php?FID=1&TID=14006

Just converted it to C#.
It works so far, but I've got one issue.

When the user enters
 {{SomeString}}


The resulting HTML Body part looks like this
 <span style='font-size:11.0pt'>{{SomeString}}<o:p></o:p></span>


But if I set the same string via API, (InsertAfter, Selection, SetRange)
The resulting HTML Body part looks like this
 <span style='font-size:11.0pt'>{{<span class=SpellE>DownloadLink</span>}}<o:p></o:p></span> 


It seems the HTML Engine splits words and special characters.

How can I programmatically add this string, without it beeing split like this.
The HTML Body part should look the same, as typed in by user.
Posted 24 Sep, 2020 04:44:49 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello,

The Word object model doesn't let you use HTML to modify a document. An exception is: if you paste HTML, the Word converter is triggered.

My understanding is: you insert some text which is formatted differently in some aspect from the surrounding text. That's the reason why they insert a span.


Andrei Smolin
Add-in Express Team Leader
Posted 24 Sep, 2020 05:32:57 Top
SECUDOS




Posts: 4
Joined: 2020-09-24
Hello Andrei,

thanks for your quick response.

Just to ensure we don't have a missunderstanding, some code snippets.


Behind an on_click event of a button, I have this code

...
var stringToInsert = "{{SomeString}}";
activeInspector = this.OutlookApp.ActiveInspector();
wordDoc = activeInspector.WordEditor;
wordDoc.Application.Selection.InsertAfter(stringToInsert);
wordDoc.Application.Selection.SetRange(i, i);
...




The Word object model doesn't let you use HTML to modify a document


I don't want to use HTML, I'd just like to insert the fixed String.


My goal should be. The user can type in by hand {{SomeString}} or as an alternative, he can click the button.

But for processing reasons both ways should result in the same generated HTMLBody.

I've tried different approaches, but none seems to make a difference.

For Example

System.Windows.Forms.Clipboard.SetText(stringToInsert);
wordDoc.Application.Selection.Range.PasteSpecial(...);


Same issue.

I don't see a way to control the formatting style, to avoid this behaviour.

Do you have any idea ? Can you point me to a direction ?

Thanks in advance
S?bastien Tête
Posted 24 Sep, 2020 06:24:49 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello S?bastien,

I assume you talk about the span you see in HTML after you insert the text programmatically:

<span class=SpellE>DownloadLink</span>


I suggest that you copy some text from notepad, select the text your add-in inserts in Word - the text within the "{{" and "}}" symbols; these symbols must be excluded - and paste the text. Do you see the span in this case?

Consider this scenario. You have a word in italic while all surrounding text is non-italic; if the text cursor is on that word, you see the "italicized" version of the text cursor. Now, delete that word using {Backspace} or {Delete} and check the cursor: it is still italicized; press {leftArrow} and then {RightArrow}: the cursor is normal. I suppose, wordDoc.Application.Selection gets the formatting associated with that location and this is why you get the extra span. I suggest that before you insert the text programmatically, you move the text cursor back and forth using the arrow keys.


Andrei Smolin
Add-in Express Team Leader
Posted 24 Sep, 2020 08:14:46 Top
SECUDOS




Posts: 4
Joined: 2020-09-24
Hello Andrei,

thanks for your explanation.

Indeed, the resulting formatting is based on the formatting the cursor is currently in.

I will go deeper into these topics and maybe try your approach.

I also have the idea, to modify the HTMLBody, convert the generated HTML part to what is generated when typed in.
Via item_send event.

This is not directly related to Addin-Express.

If I found a stable solution, I'll come back and post it.

regards
S?bastien Tête
Posted 25 Sep, 2020 10:20:32 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello S?bastien,

You can try to call wordDoc.Application.Selection.ClearFormatting() before inserting the text.

If that topic is closed by the time you find a solution, write me to the support email address (see readme.txt) or use the form at https://www.add-in-express.com/support/askus.php and I'll reopen the topic.


Andrei Smolin
Add-in Express Team Leader
Posted 25 Sep, 2020 11:54:46 Top
SECUDOS




Posts: 4
Joined: 2020-09-24
Hello Andrei,

topic seems still open ;)

I did some more research.

The extra span, that is added, is only because of "class=SpellE"

When editing via WordEditor, the editor adds this class as indicator for an "Spell checking issue".

I guess to able to later indicate to the user, this word/string cannot be found in dictionary, or is missspelled.

Well in my case, the string is unique.


For everyone who likes to add custom strings sourrouned by special characters.
=> To avoid the extra span or class

If all characters are uppercase, the WordEditor skips spell checking, he doesn't treat it like a "word", hence no extra span, or "class=SpellE".

Changing to all uppercase had the least influence for any following processing. (in my case)

Maybe this solution is suitable for others.



I've also tried ClearFormatting() without success.


Thanks for your support.

Like others already mentioned a few times, the support here is outstanding!
Existing threads usually solve all my questions :)

regards
S?bastien Tête
Posted 28 Sep, 2020 09:18:41 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello S?bastien,

My congratulations and thanks!

That solution you've found yourself :)


Andrei Smolin
Add-in Express Team Leader
Posted 28 Sep, 2020 09:37:05 Top