selected outlook item

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
------------------------------------------------------------------------
 
G

Graeme

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
=====================================
 
S

Sunny

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
 
S

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
 
C

Cyril Tiwari

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.
 
S

Sunny

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top