-
Outlook:Selecting an email, extract information from it
Hi, I'm new to programming in Outlook. I've searched quite a few places to find out how to do this.
I want to be able to retrieve information such as the name of the sender, the email body, etc. by selecting an email, then click on a button that I've created. I'm not sure how to refer to the selected email as well as referring to the specific fields of it. I would really appreciate if someone can point me in the right direction.
Thanks in advance.
-
Re: Outlook:Selecting an email, extract information from it
I have several Outlook code examples on these Forums. ;)
You will want to start with telling us how you added your "button"? Is it added in via an Add-In, Outlook VBA, VB6 automation, or VB.NET/C#, etc.
An email is an Outlook.MailItem. ;)
-
Re: Outlook:Selecting an email, extract information from it
hey RobDog, thanks for the quick reply :)
I used Visual Studio VB to add the add-in. I found some code, can't remember where now, that adds a button and it's as follow:
VB Code:
Public Sub OnStartupComplete(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnStartupComplete
Dim oCommandBars As CommandBars
Dim oStandardBar As CommandBar
On Error Resume Next
' Set up a custom button on the "Standard" command bar.
oCommandBars = applicationObject.CommandBars
If oCommandBars Is Nothing Then
' Outlook has the CommandBars collection on the Explorer object.
oCommandBars = applicationObject.ActiveExplorer.CommandBars
End If
oStandardBar = oCommandBars.Item("Standard")
If oStandardBar Is Nothing Then
' Access names its main toolbar Database.
oStandardBar = oCommandBars.Item("Database")
End If
' In case the button was not deleted, use the exiting one.
MyButton = oStandardBar.Controls.Item("New Opportunity")
If MyButton Is Nothing Then
MyButton = oStandardBar.Controls.Add(1)
With MyButton
.Caption = "New Opportunity"
.Style = MsoButtonStyle.msoButtonCaption
' The following items are optional, but recommended.
' The Tag property lets you quickly find the control
' and helps MSO keep track of it when more than
' one application window is visible. The property is required
' by some Office applications and should be provided.
.Tag = "New Opportunity"
' The OnAction property is optional but recommended.
' It should be set to the ProgID of the add-in, so that if
' the add-in is not loaded when a user clicks the button,
' MSO loads the add-in automatically and then raises
' the Click event for the add-in to handle.
.OnAction = "!<MyCOMAddin.Connect>"
.Visible = True
End With
End If
' Display a simple message to show which application you started in.
'MsgBox("Started in " & applicationObject.Name & ".")
oStandardBar = Nothing
oCommandBars = Nothing
End Sub
Also, I've found a function that returns the sender's address. But it takes an Outlook.Mailitem as an input. How can I pass the mail that's been selected by the user?
VB Code:
Function GetMailItemSenderAddress(ByVal oOutlookMail As Outlook.MailItem) As String
Dim oMapiMessage As Object 'MAPI.Message
Dim oSession As Object 'MAPI.Session
Dim sEntryID As String
' Dim sEntryID As String
MsgBox(oOutlookMail)
oSession = CreateObject("MAPI.Session")
'Logon to the existing session
oSession.Logon("", "", False, False)
'Get the message Information
sEntryID = oOutlookMail.EntryID
sEntryID = oOutlookMail.Parent.StoreID
'Get the message
oMapiMessage = oSession.GetMessage(sEntryID, sEntryID)
'Extract the Sender's Address and Return value
GetMailItemSenderAddress = oMapiMessage.Sender.Address
oSession.Logoff()
oSession = Nothing
oMapiMessage = Nothing
Exit Function
ErrFailed:
' Debug.Print(Err.Description)
Debug.Assert(False)
GetMailItemSenderAddress = ""
End Function
-
Re: Outlook:Selecting an email, extract information from it
You don't need the MailItem to do this..
Here is some sample code that I have
VB Code:
Dim myOlExp As Explorer
Dim myOlSel As Selection
Dim myOlMail As MailItem
Dim i As Integer, j As Long
Set myOlExp = ActiveExplorer
Set myOlSel = myOlExp.Selection
For j = 1 To myOlSel.Count
Set myOlMail = myOlSel.Item(j)
MsgBox myOlMail.Subject
MsgBox myOlMail.Sender
Set myOlMail = Nothing
Next j
Set myOlSel = Nothing
Set myOlExp = Nothing
It picks up the selected email and get's both the subject and the sender
-
Re: Outlook:Selecting an email, extract information from it
Hey Danny,
On the line: myOlExp = ActiveExplorer
It says ActiveExplorer is not declared. The other three variables I've added 'Outlook.' in front of them and they seem to be fine. How can I fix that line?
-
Re: Outlook:Selecting an email, extract information from it
VB Code:
Dim myOlApp As Outlook.Application
Dim myOlExp As Outlook.Explorers
Dim obSi As Outlook.Selection
Dim obMi As Outlook.MailItem
Set myOlApp = GetObject(,"Outlook.Application")
Set myOlExp = myOlApp.Explorers
Set obSi = myOlExp.Item(1).Selection
For i = 1 To obSi.Count
Set obMi = obSi.Item(i)
Debug.Print obMi.SenderName
Debug.Print obMi.Subject
Next i
Set obMi = Nothing
Set obSi = Nothing
Set myOlExp = Nothing
Set myOlApp = Nothing
Try that one
-
Re: Outlook:Selecting an email, extract information from it
What do I declare obMi, obSi, obEx and obOt as?
-
Re: Outlook:Selecting an email, extract information from it
Sorry.. Copy and paste error.. will fix it in the original
-
Re: Outlook:Selecting an email, extract information from it
It works! :)
Thanks! :thumb:
I have a few more questions though. Where can I look for documentations on other fields? Specifically the body of the email, date, etc.?
When I first access that code, there's a pop up that asks if I'm going to allow the program to access my email. Is it possible to get rid of that somehow? May be not by code but through registry?
Another question, which is an entirely different topic, how persistent are class variables? I have a public class variable in my form that I would like to be accessible in other classes such as Connect, through out the instance of Outlook. How can I achieve that?
-
Re: Outlook:Selecting an email, extract information from it
Because you have declared obMi As Outlook.MailItem you can scroll through the remainder of a mail item's properties by pressing .
The pop up message is outlook security Perform a search for ClickYes, this is program that will continue to scan for messages like this and click's the yes button for you. There are other methods which does include changing the registry.
To allow a variable to be available throughout your whole project declare it as a global variable in module (or Public if you prefer)
-
Re: Outlook:Selecting an email, extract information from it
Since I'm creating an add-in, would the global variable be in the Connect class? I tried to declare the global variable in the form but I suppose the value of the variable is re-initialized everytime the form is loaded.
How can I declare the variable in Connect and be able to use it in the form as well? It wouldn't let me do Connect.globalvar. I've also tried defining an object for connect then go objConnect.globalvar and that crashes.
-
Re: Outlook:Selecting an email, extract information from it
Quote:
Originally Posted by dannymking
You don't need the MailItem to do this..
But you are using the MailItem.
VB Code:
Dim obMi As Outlook.MailItem
-
Re: Outlook:Selecting an email, extract information from it
That's Just Picking on me :D
In fact you don't need the mail item.. it is easier with it though..
VB Code:
Dim myOlApp As Outlook.Application
Dim myOlExp As Outlook.Explorers
Dim obSi As Outlook.Selection
Set myOlApp = GetObject(,"Outlook.Application")
Set myOlExp = myOlApp.Explorers
Set obSi = myOlExp.Item(1).Selection
For i = 1 To obSi.Count
Debug.Print obSi.Item(i).SenderName
Debug.Print obSi.Item(i).Subject
Next i
Set obSi = Nothing
Set myOlExp = Nothing
Set myOlApp = Nothing
-
Re: Outlook:Selecting an email, extract information from it
hey guys...referring to my post , what should I do about global variable?
Danny, your code was working fine but I don't know what's wrong now.
VB Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim myOlApp As Outlook.Application
Dim myOlExp As Outlook.Explorers
Dim obSi As Outlook.Selection
Dim obMi As Outlook.MailItem
Dim i As Integer
MsgBox("1")
myOlApp = GetObject(, "Outlook.Application")
MsgBox("2")
myOlExp = myOlApp.Explorers
MsgBox("3")
obSi = myOlExp.Item(1).Selection
MsgBox("about if")
If obSi.Count > 0 Then
MsgBox("in if")
For i = 1 To obSi.Count
obMi = obSi.Item(i)
MsgBox(obMi.SenderName)
MsgBox(obMi.Body)
Next i
obMi = Nothing
obSi = Nothing
myOlExp = Nothing
myOlApp = Nothing
Else
MsgBox("in else")
MsgBox("Please select an email.")
End If
Me.Close()
End Sub
It crashes after msgbox 1 pops up. Any idea?
-
Re: Outlook:Selecting an email, extract information from it
This is actually inside Outlook? or some other office app?
-
Re: Outlook:Selecting an email, extract information from it
It's used in Outlook, developed in Visual Studio.
-
Re: Outlook:Selecting an email, extract information from it
Ok the code is actually from Access...
The line that is causing the problem is the attempt to GetObject if outlook is not open then this will cause a problem.
If the code is inside Outlook you should be able to drop the outlook part of it... like so
VB Code:
Dim myOlExp As Explorers
Dim obSi As Selection
Dim obMi As MailItem
Set myOlExp = Application.Explorers
Set obSi = myOlExp.Item(1).Selection
For i = 1 To obSi.Count
Set obMi = obSi.Item(i)
Debug.Print obMi.SenderName
Debug.Print obMi.Subject
Next i
Set obMi = Nothing
Set obSi = Nothing
Set myOlExp = Nothing
In my corporation VBA is disabled behind Outlook so I cannot test to see if this will actually work
-
Re: Outlook:Selecting an email, extract information from it
The code wouldn't be used if Outlook is not opened though. I just kept getting the follow msg when it crashes:
Object reference not set to an instance of an object.
Also when I tried the code you provided above, I'm not sure what to set Application as....
-
Re: Outlook:Selecting an email, extract information from it
You do not need to set Application to anything.. It refers to the Application to which the code resides..
-
Re: Outlook:Selecting an email, extract information from it
Looking back over your code you are using .Net to build this.. in testing it will attempt to GetObject of Outlook..
If Outlook is not open then it will fail.. you will need to CreateObject instead.
VB Code:
myOlApp = CreateObject("Outlook.Application")
-
Re: Outlook:Selecting an email, extract information from it
plui31, you are writting this in .NET. Which version and it appears to be automating Outlook and not actually in VBA.
Your attaching your variable object to the .NET Application object and not Outlooks?
Missed that one Danny ;)
-
Re: Outlook:Selecting an email, extract information from it
Quote:
Originally Posted by dannymking
Looking back over your code you are using .Net to build this.. in testing it will attempt to GetObject of Outlook..
If Outlook is not open then it will fail.. you will need to CreateObject instead.
VB Code:
myOlApp = CreateObject("Outlook.Application")
I'm testing it by opening up Outlook manually then click on the button I've added. Should I have CreateObject anyway?
-
Re: Outlook:Selecting an email, extract information from it
I'm using Microsoft Visual Studio .NET 2003.
Quote:
Originally Posted by RobDog888
Your attaching your variable object to the .NET Application object and not Outlooks?
I'm not sure what you're asking here :confused:
-
Re: Outlook:Selecting an email, extract information from it
In VB.NET there is an Application object and also in Outlook. Since there was code like this...
VB Code:
Set myOlExp = Application.Explorers
VB doesnt know which your referring to. Now however your current code is , it needs to make the distinction between the two by using the myOlApp object variable being set to the Outlook.Application instance.
-
Re: Outlook:Selecting an email, extract information from it
aah okay. thanks :) It's working now.
I have a question about the body of the Outlook message. How is a newline represented? I want to replace every newline with a special character. How can I do that?
-
Re: Outlook:Selecting an email, extract information from it
The new line character is ...
vbNewLine
Chr(13) & Chr(10)
vbCrLf
You can replace a new line with your own character by using the Replace function.
VB Code:
strBody = Replace(strBody, vbNewLine, "NL") 'Or ??? character instead of "NL"
-
Re: Outlook:Selecting an email, extract information from it
hey RobDog
I've tired the following:
VB Code:
iBody = Replace(iBody, vbNewLine, "%0a")
iBody = Replace(iBody, Chr(13) & Chr(10), "%0a")
iBody = Replace(iBody, vbCrLf, "%0a")
And I also have this: Replace(iBody, " ", "%20")
I sent myself an email that looks like this:
first line
second line
And after the replace, it merges the string into first%20linesecond%20line.
What's the problem?
-
Re: Outlook:Selecting an email, extract information from it
Nothing. Your taking out the newlines so you end up with a string with your substitute char as the line breaks but since its not a line break it wont break. ;)
-
Re: Outlook:Selecting an email, extract information from it
True...but how come my substitute character, "%0a", isn't there?
-
Re: Outlook:Selecting an email, extract information from it
What char does "%0a" represent? I know its supposed to be a web thing, right?
-
Re: Outlook:Selecting an email, extract information from it
Yeah, it's suppose to be a newline for forms and %20 is a space. Any idea how to get it to work?
-
Re: Outlook:Selecting an email, extract information from it
What is the version of Outlook and what is the .BodyFormat of the email?
-
Re: Outlook:Selecting an email, extract information from it
It's Outlook 2000.
I'm not sure what .BodyFormat is...
This is weird. I changed my code to:
iBody = Replace(iBody, vbNewLine, "xxx")
and it was fine. It got it right. But when it's "%0a", It just merges together...
-
Re: Outlook:Selecting an email, extract information from it
Will the newline work if its not in a form?
.BodyFormat is only available in Outlook 2002+
You may want to use the .HTMLBody to display your body text in HTML. Your special chars should work correctly in that format.
-
Re: Outlook:Selecting an email, extract information from it
Quote:
Originally Posted by RobDog888
Will the newline work if its not in a form?
I think as long as it's a textbox then it should work.
Quote:
Originally Posted by RobDog888
.BodyFormat is only available in Outlook 2002+
You may want to use the .HTMLBody to display your body text in HTML. Your special chars should work correctly in that format.
How do I do that?
-
Re: Outlook:Selecting an email, extract information from it
You can't it's only available in 2002 and you are using 2000
-
Re: Outlook:Selecting an email, extract information from it
Quote:
Originally Posted by dannymking
You can't it's only available in 2002 and you are using 2000
What U talk'n bot willis? ;)
The .HTMLBody is available in Outlook 2000.
Here is the specifics on the HTMLBody property in Outlook 2000.
http://msdn.microsoft.com/library/de...roHTMLBody.asp
-
Re: Outlook:Selecting an email, extract information from it
mis-read.. though he was talking about .bodyformat :D
-
Re: Outlook:Selecting an email, extract information from it
aaah alright. I'll look into it.
Thanks for all your help, Danny & RobDog! :)