Notification texts go here Contact Us Buy Now!

How to get selected text from an Outlook received email using in VSTO C#

Reading Selected Text from Outlook Emails using VSTO C#

Introduction

VSTO (Visual Studio Tools for Office) provides developers with the ability to create custom add-ins for Microsoft Office applications, including Outlook. This allows developers to extend the functionality of Outlook with custom features and tools. One of the common requirements for Outlook add-ins is the ability to access and manipulate the content of email messages, including the selected text.

Accessing Selected Text in Outlook

To access the selected text in an Outlook received email using VSTO C#, you can use the following steps:

  1. Create a new VSTO C# project.
  2. Add a reference to the Outlook object library.
  3. Create a new Outlook add-in class that inherits from the ApplicationAddinBase class.
  4. In the Startup method of the add-in class, add the following code:
```csharp this.Application.ActiveExplorer().PreviewPane.SelectionChange += new Outlook.SelectionChangeEventHandler(this.PreviewPane_SelectionChange); ```

This code subscribes to the SelectionChange event of the PreviewPane object, which is raised whenever the selection in the preview pane changes.

  1. Add the following method to the add-in class:
```csharp private void PreviewPane_SelectionChange() { Outlook.Selection selection = this.Application.ActiveExplorer().PreviewPane.Selection; if (selection.Type == Outlook.OlSelectionType.olText) { string selectedText = selection.Text; // Do something with the selected text } } ```

This method is called whenever the selection in the preview pane changes. It checks if the selection is of type OlSelectionType.olText, which indicates that text is selected. If text is selected, it retrieves the selected text using the Text property of the Selection object and stores it in the selectedText variable. You can then use the selectedText variable to perform various operations on the selected text, such as copying it to the clipboard or displaying it in a message box.

Note: The PreviewPane object is available in Outlook 2016 and above versions. If you need to support older versions of Outlook, you can use the ActiveInspector object instead. The ActiveInspector object represents the currently active inspector window, which is used to display and edit email messages. To access the selected text in an Outlook received email using the ActiveInspector object, you can use the following code:

```csharp string selectedText = this.Application.ActiveInspector().WordEditor.Application.Selection.Text; ```

This code retrieves the selected text from the WordEditor object, which is the underlying Word editor component used by Outlook to display and edit email messages.

Conclusion

By using the techniques described in this blog post, you can easily access and manipulate the selected text in Outlook received emails using VSTO C#. This allows you to create custom Outlook add-ins that can perform various tasks based on the selected text, such as extracting data, performing calculations, or sending automated replies.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.