none
Get selected text in Outlook message RRS feed

  • Question

  • Hi,
    I would like to "get" a text from outlook message, using VB.NET shared add-in.

    I am trying to do that for several weeks, checked a lot of web resources, but without success.

    I would realy apreciate your help on this matter.

    Here is the whole story:
    First, I've made VBA script and it worked well. Here it is:

    Sub Macro1()
     
    Dim msg As Outlook.MailItem
    Dim insp As Outlook.Inspector
     
     
     
    Set insp = Application.ActiveInspector
     
    If insp.CurrentItem.Class = olMail Then
        Set msg = insp.CurrentItem
        If insp.EditorType = olEditorHTML Then
            a = ActiveInspector.HTMLEditor.Selection.CreateRange.Text
            MsgBox (a)
        ElseIf insp.EditorType = olEditorWord Then
            a = msg.GetInspector.WordEditor.Application.Selection
            MsgBox (a)
        ElseIf insp.EditorType = olEditorRTF Then
           
            MsgBox ("RTF is not supported")
        ElseIf insp.EditorType = olEditorText Then
            MsgBox ("Plain Text is not supported")
        Else
            MsgBox ("Can't get selected text")
        End If
     
     
    End If
    


    Then I've tried to create shared add-in with similar code:

    Try
        objOutlook = DirectCast(System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application"), Outlook.Application)
    Catch
        MessageBox.Show("Can't detect Outlook")
                    End Try
    
                    
                    Dim msg As Outlook.MailItem
                    Dim insp As Outlook.Inspector = objOutlook.ActiveInspector
                    Dim a As String
    
                    If insp.Class = Outlook.OlObjectClass.olMail Then
    
                        msg = CType(insp.CurrentItem, Outlook.MailItem)
    
                        If insp.EditorType = Outlook.OlEditorType.olEditorHTML Then
                            a = insp.HTMLEditor.Selection.CreateRange.Text.ToString
                            MsgBox(a)
    
                        ElseIf insp.EditorType = Outlook.OlEditorType.olEditorWord Then
                            a = msg.GetInspector.WordEditor.Application.Selection
                            MsgBox(a)
                        ElseIf insp.EditorType = Outlook.OlEditorType.olEditorRTF Then
    
                            MsgBox("RTF is not supported")
                        ElseIf insp.EditorType = Outlook.OlEditorType.olEditorText Then
                            MsgBox("Plain Text is not supported")
                        Else
                            MsgBox("Can't get selected text")
                        End If
    
    
                    End If


    I've got the following problems:

    1. "insp.Class" is always "olInspector" instead of "olMail". But this problem is not critical.

    The critical problems are:

    2.  "a = msg.GetInspector.WordEditor.Application.Selection" is not accepted by VB.NET

    3. " a = insp.HTMLEditor.Selection.CreateRange.Text.ToString" is not accepted by VB.NET

    Thhanks in advance,

    Alex. 

    Sunday, November 27, 2011 3:23 PM

Answers

All replies

  • "Not accepted" as in you get a compile time error?

    Did you add Word and DOM type libraries to you project references?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    Monday, November 28, 2011 5:36 AM
  • Hello Dmitry,

    thanks for your reply.

    I am trying to use VBA commands in VB.NET in order to access selected text in the mail body.
    Unfortunately those commands are not existing in Object Model.
    I am trying to find alternative commands, but without success.

    Yes, I've added Outlook, Word and Office libraries.

    What is DOM library?
    Is it Document Object Model (like in javascript)?
    How to add it?

    Thanks.

    Monday, November 28, 2011 7:22 AM
  • Yes, you need to add mshtml.dll type library to the project references.
    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    Monday, November 28, 2011 2:41 PM
  • Hello Dmitry,

    I've added mshtml.dll to the project references, however VBA command "a = ActiveInspector.HTMLEditor.Selection.CreateRange.Text" is not available.

    Monday, November 28, 2011 4:08 PM
  • What exactly is not available? Do you get an error? What is it?
    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    Monday, November 28, 2011 4:42 PM
  • In Visual Studio Intellisense offers available commands. The following command is not offered by Intellisense.

     "a = insp.HTMLEditor.Selection.CreateRange.Text"

    However, it is working. But Outlook displays the following warning:

    "A program is trying to access e-mail addresses you have stored in Outlook. Do you want to allow this? If this is unexpected, it may be virus and you should choose No".

    If I choose Yes, the command works and I get a message box with selected text.

    But I can't send such a progrm to anybbody, people will accept it as a virus!!!

    Do you know any workaround?

    Monday, November 28, 2011 5:02 PM
  • Of course, Outlook type library declares Inspector.HTMLEditor as returning a generic IDispatch, not an IHTMLDocument object.

    If you get a warning, that means you are creating a new (security crippled) Outlook.Application object instead of using an object passed to your add-in on startup.

    If you code is in a standalone exe (not a COM add-in), you can use the SafeInspector object in Redemption - see http://www.dimastr.com/redemption/safeinspector.htm


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!

    Monday, November 28, 2011 8:59 PM
  • Hi Dmitry,

    thanks for your answer.

    It will be COM add in in the later stage. Currently I work with standalone exe, since it is easier to debug.

    However, I am planning to use Redemption on the later stage, I understand that it can support RFF and Text formats.

    Tuesday, November 29, 2011 3:54 AM