0

I am new to C# VSTO Add-In development and am trying to add a signature at the cursor's location. However, I'm unable to insert HTML and can only add plain text. How can I append HTML instead?

I tried the code below and was able to add text. How can I append HTML instead?

// Get the active Inspector (email editor)
Outlook.Inspector inspector = outlookApp.ActiveInspector();

if (inspector != null && inspector.CurrentItem is Outlook.MailItem mailItem)
{
    // Get the Word editor for the email
    Document wordDocument = inspector.WordEditor as Document;

    if (wordDocument != null)
    {
        // Access the current selection (cursor position)
        Selection selection = wordDocument.Application.Selection;

        if (selection != null)
        {
            // Insert HTML at the current selection (cursor position)
            Range range = selection.Range;
            selection.TypeText(signature);
            // Set the range's HTML format to preserve HTML tags
            range.FormattedText = wordDocument.Application.Selection.FormattedText;
            // Set the email's HTML body to include the new HTML content
            //mailItem.HTMLBody = mailItem.HTMLBody.Insert(mailItem.HTMLBody.Length, "Ram");
            return "NOTREQUIRED";
        }

    }
}

Then tried below code,

//Insert the HTML content at the current cursor position
 selection.Range.InsertAfter(htmlContent);

HTML is not being added in this location. I'm not sure what I did wrong. Can you help me figure it out?

enter image description here

2 Answers 2

0

Try to use Range.InsertFile to insert the signature's HTML file.

Sign up to request clarification or add additional context in comments.

15 Comments

This is being added as an attachment instead of appending to the existing HTML content.
When you call Range.InsertFile? Or are you are using Attachments.Add?
I need to add the signature design as rich text instead of adding it as plain text. Updated the question, Please have a look.
TypeText assumes plain text. Once again, do use Range.InsertFile - it figures out the text type (txt/html/rtf) based on the file extension.
It's adding as attachment.
|
0

Used tricky way, just paste the HTML using below functions, and it's working.

public void SetClipboardHtml(string htmlContent)
{
    string preamble = "Version:0.9\r\n";
    string htmlStart = "<html><body><!--StartFragment-->";
    string htmlEnd = "<!--EndFragment--></body></html>";

    string fullHtml = htmlStart + htmlContent + htmlEnd;

    int startHtml = preamble.Length;
    int startFragment = startHtml + htmlStart.Length;
    int endFragment = startFragment + htmlContent.Length;
    int endHtml = endFragment + htmlEnd.Length;

    string clipboardFormat = $"{preamble}" +
        $"StartHTML:{startHtml:D8}\r\n" +
        $"EndHTML:{endHtml:D8}\r\n" +
        $"StartFragment:{startFragment:D8}\r\n" +
        $"EndFragment:{endFragment:D8}\r\n" +
        $"{fullHtml}";
    Clipboard.Clear();
    Clipboard.SetText(clipboardFormat, TextDataFormat.Html);
}

if (selection != null)
{
    SetClipboardHtml(signature);
    selection.Paste();
    return "NOTREQUIRED";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.