Get selected text in Outlook message
Get selected text in Outlook message
Get selected text in Outlook message
Get selected text in Outlook message
Get selected text in Outlook message
Get selected text in Outlook message Get selected text in Outlook message Get selected text in Outlook message Get selected text in Outlook message Get selected text in Outlook message Get selected text in Outlook message Get selected text in Outlook message Get selected text in Outlook message
Get selected text in Outlook message Get selected text in Outlook message
Get selected text in Outlook message
Go Back  Xtreme Visual Basic Talk > > > Get selected text in Outlook message


Reply
 
Thread Tools Display Modes
  #1  
Old 11-21-2011, 03:43 PM
Alex30 Alex30 is offline
Regular
 
Join Date: Sep 2007
Posts: 51
Default Get selected text in Outlook message


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

First, I've made VBA script and it worked well. Here it is:
Code:
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, but I've got multiple Warning messages.
Here is the code:
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 searched the WEB for example, but I didn't find.

I would appreciate your suggestions.

Thanks,

Alex.
Reply With Quote
  #2  
Old 11-22-2011, 10:18 AM
DennisW's Avatar
DennisW DennisW is offline
Junior Contributor
 
Join Date: Mar 2006
Location: Östersund, Sweden
Posts: 268
Default

Alex,

Have You resolved it yet or do You want me to help (although Outlook is not my tool so to speak)?
__________________
Kind regards,
Dennis

.NET & Excel | 2nd edition PED | MVP
Reply With Quote
  #3  
Old 11-22-2011, 11:11 AM
Alex30 Alex30 is offline
Regular
 
Join Date: Sep 2007
Posts: 51
Default

Hi Dennis,
unfortunatley I didn't succeed to make it working.
Reply With Quote
  #4  
Old 11-22-2011, 05:01 PM
DennisW's Avatar
DennisW DennisW is offline
Junior Contributor
 
Join Date: Mar 2006
Location: Östersund, Sweden
Posts: 268
Default

Alex,

OK, I will get back to You tomorrow my local time. Hopefully I have a working solution for You :-)
__________________
Kind regards,
Dennis

.NET & Excel | 2nd edition PED | MVP
Reply With Quote
  #5  
Old 11-23-2011, 12:36 AM
Alex30 Alex30 is offline
Regular
 
Join Date: Sep 2007
Posts: 51
Default

Thanks Dennis,
it is very kind of you.
Reply With Quote
  #6  
Old 11-30-2011, 08:35 AM
DennisW's Avatar
DennisW DennisW is offline
Junior Contributor
 
Join Date: Mar 2006
Location: Östersund, Sweden
Posts: 268
Default

Alex,
I still struggling with the Inspector object. Outlook objects model is ... Have You been able to resolve it Yourself?
__________________
Kind regards,
Dennis

.NET & Excel | 2nd edition PED | MVP
Reply With Quote
  #7  
Old 11-30-2011, 11:00 AM
Alex30 Alex30 is offline
Regular
 
Join Date: Sep 2007
Posts: 51
Default

Hi Dennis,
thanks for your effort.

I've been straggling for a long time, but without success.

Then I've tried on another PC (VS 2005 and Office 2003) and it worked!!!

On my PC (VS 2010 and Office 2007 Enterprise) the code can't detect running instance of Outlook. I don't know why. It works for Word, Excel, Power Point, but not for outlook.
I've reinstalled Office 2007, but it didn't help. The following command returns an error:

objOutlook = DirectCast(System.Runtime.InteropServices.Marshal.GetActiveObject("Out look.Application"), Outlook.Application)

I will post the working code tomorrow. It is on other PC and not available right now.
Reply With Quote
  #8  
Old 12-01-2011, 01:01 AM
Alex30 Alex30 is offline
Regular
 
Join Date: Sep 2007
Posts: 51
Default

Here is the code:
Code:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim objOutlook As Outlook.Application
        Dim msg As Outlook.MailItem
        Dim a As String


        Try
                        objOutlook = DirectCast(GetObject(, "Outlook.Application"), Outlook.Application)
                       
        Catch ex As Exception
            MsgBox("There was error getting a reference to Outlook instance")
            Exit Sub
        End Try



        Dim insp As Outlook.Inspector = objOutlook.ActiveInspector
        Try
            msg = CType(insp.CurrentItem, Outlook.MailItem)
        Catch
            MsgBox("Can't detect open mail instance")
            Exit Sub
        End Try


    

        If insp.EditorType = Outlook.OlEditorType.olEditorHTML Then
            Try
                a = insp.HTMLEditor.Selection.CreateRange.Text.ToString
            Catch ex As Exception
                MsgBox("Can't extract selected HTML text")
                Exit Sub
            End Try

            MsgBox(a)


        ElseIf insp.EditorType = Outlook.OlEditorType.olEditorWord Then

            Dim mailWord As Word.Application
            Dim mailDoc As Word.Document

            Try
                mailWord = DirectCast(System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application"), Word.Application)

                
            Catch ex As Exception
                MsgBox("Can't detect open message. Editor type: WordEditor")
                Exit Sub
            End Try


            Try
                mailDoc = mailWord.ActiveDocument
            Catch ex As Exception
                MsgBox("Can't detect active message. Editor type: WordEditor")
                Exit Sub
            End Try

            Try
                a = mailWord.Selection.Text.ToString
            Catch ex As Exception
                MsgBox("Failed to extract selected text")
                Exit Sub
            End Try


            MsgBox(a)

        ElseIf insp.EditorType = Outlook.OlEditorType.olEditorRTF Then
            MsgBox("RFT in not supported")
         
        ElseIf insp.EditorType = Outlook.OlEditorType.olEditorText Then
            MsgBox("Plain text in not supported")
        Else
            MsgBox("Can't detect mail editor type")
        End If

    End Sub
End Class
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Get selected text in Outlook message
Get selected text in Outlook message
Get selected text in Outlook message Get selected text in Outlook message
Get selected text in Outlook message
Get selected text in Outlook message
Get selected text in Outlook message Get selected text in Outlook message Get selected text in Outlook message Get selected text in Outlook message Get selected text in Outlook message Get selected text in Outlook message Get selected text in Outlook message
Get selected text in Outlook message
Get selected text in Outlook message
 
Get selected text in Outlook message
Get selected text in Outlook message
 
-->