selected outlook item

  • Thread starter Thread starter Cyril Tiwari
  • Start date Start date
C

Cyril Tiwari

I've created an outlook 2002 add-in which adds a new button to the
standard command bar.

I want to write a method (vb.net) that will cause a msgBox to pop-up
and display the contents of the e-mail subject of the selected outlook
e-mail item.

The code below doesn't work. It compiles, but, when I click on the
button, nothing happens. Any help would be greatly appreciated.

-----------------------------------------------------------------------
Private Sub MyButton_Click(ByVal Ctrl As
Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As
Boolean) Handles MyButton.Click
Dim oMsg As Outlook.MailItem
Dim oApp As New Outlook.Application()
oMsg = oApp.ActiveExplorer.Selection(0)
MsgBox(oMsg.Body)
End Sub
------------------------------------------------------------------------
 
Where do you assign MyButton to an Outlook button object?

Regards
Graeme
=====================================
Websetters - home of the WS:Outlook AddIn
the only limits are those of vision...
www.websetters.co.uk
=====================================
 
Hi,
as I see, you are creating a new Application object?!?!? but you want to get
an item from the currently running?
In the OnConnection method you have to save a reference to the Application
object, and after that to use it's ActiveExplorer ...
Right now you are trying to reach something nonexisting.

Sunny
 
I didn't know this ... but one have to learn whole live :)

Ciril, as an addition: why dont you try :
oMsg = oApp.ActiveExplorer.Selection.Item(1)

Dmitry Streblechenko said:
This is perfectly fine: creating a new instance of Outlook.Application will
return an already running instance if it exists. Saving Application passed
to OnConnection is more efficient of course.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
Sunny said:
I didn't know this ... but one have to learn whole live :)

Ciril, as an addition: why dont you try :
oMsg = oApp.ActiveExplorer.Selection.Item(1)

Thanks for the replies.

I tried what Sue suggested - oApp.ActiveExplorer.Selection - and what
Sunny suggested - oApp.ActiveExplorer.Selection.Item(1), but neither
seem to make a difference. (Nothing happens when the button is
clicked)

I am sure that there is nothing wrong with the button itself, as I can
get it to open a new instance of IE and go to a specific url. It must
be something else.
 
Hi Cyril,
try to add On Error Resume Next (I think that was in VB) in the handler to
see if exception is thrown. You may then check after every assignment if
there was an Err and what kind. Usually such a things happens when there is
no valid assignment (cast of type, etc.).

Also, you may change your code like:

Private Sub MyButton_Click(ByVal Ctrl As
Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As
Boolean) Handles MyButton.Click
Dim oMsg As Outlook.MailItem
Dim oApp As New Outlook.Application()
MsgBox(oApp.Explorers.Count) 'to see if there are explorers
' the above line have to show you at least 1 explorer
MsgBox(oApp.ActiveExplorer.Selection.Count) 'to see if there are
items
' the above line have to show you at least 1 Item
oMsg = oApp.ActiveExplorer.Selection(0)
MsgBox(oMsg.Body)
End Sub

Please, correct the above code, as I'm not very good with VB :) and inspect
what exactly is happening there, and let the Force be with you :)

Sunny
 
Back
Top