Add Signature while replying using Selection c# vsto addin

Ramakrishnan Ramar 20 Reputation points
2024-12-27T05:45:36.4366667+00:00

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?

image

Office
Office
A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.
1,804 questions
Outlook
Outlook
A family of Microsoft email and calendar products.
4,223 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,156 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,107 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.