Retrieving Mail Information

  • Thread starter Thread starter DMc2004
  • Start date Start date
D

DMc2004

How can I retrieve information such as Sender, Recipient, Subject, Sent Date,
Mail Size from the Inbox of Microsoft Office Outlook 2003?

Regards

D
 
Use ActiveExplorer.CurrentFolder.Items to get the collection of items in the
folder (if the Inbox is currently being displayed). Otherwise use
NameSpace.GetDefaultFolder(olFolderInbox).Items to get that collection.

Iterate the collection and retrieve whatever properties you want from each
item. Make sure to test for item.Class = olMail if you are only interested
in mail items.
 
Hi Ken,
I'm having problem following the suggestion you offer. I'm converting an
Outlook 2003 add-in that I prototyped in vb using VS2005. The problem is
identifying the currently selected mail item.

in vb I was using :
Dim selItem As Outlook.MailItem = _Explorer.Selection(1)

I converted to C# as:
Outlook.MailItem selItem = _Explorer.Selection(1);

But it will not compile -
I'd really like to iterate through all selected emails but right now I just
need to get the currently selected one.

Thanks
--
Tom Hamilton
Hamilton & Hamilton
Database Consulting Services
Sacramento, CA
 
Never mind - C# indexes with [] which is why () didn't work....

What's the best way to Loop thru Selection?
--
Tom Hamilton
Hamilton & Hamilton
Database Consulting Services
Sacramento, CA
 
It depends on what you're doing inside the loop and what sort of objects
you're dealing with.

For deleting or moving items in a loop you should use a down counting loop
so changes to the collection's count don't mess with the loop indexing. So a
for loop that starts at Selection.Count (use an int variable to pick that up
and don't use Selection.Count in the loop) and works down to 1 would work. A
do loop can also be used that checks for a Selection.Count of 0.

For anything else you can use any sort of loop construct you're most
comfortable with. That can be a for loop, do loop, foreach loop or whatever.
 
CommandBar Controls Order

Hi Ken,
The buttons/combobox/labels don't appear to be in any order - what do I need to change to control the presentation?

Should be
'Anon', cboAnon, 'States', cboStates, 'Action', cboAction, btnLink

Here is my code:

Code:
[color=#0000ff][color=#0000ff]public[/color][/color] [color=#0000ff][color=#0000ff]partial[/color][/color] [color=#0000ff][color=#0000ff]class[/color][/color] [color=#2b91af][color=#2b91af]ThisAddIn
 
[/color][/color]{
 
[color=#0000ff][color=#0000ff]#region[/color][/color] DECLARATIONS
 
[color=#0000ff][color=#0000ff]const[/color][/color] [color=#0000ff][color=#0000ff]int[/color][/color] MISSING_ANON = 1;
 
[color=#0000ff][color=#0000ff]const[/color][/color] [color=#0000ff][color=#0000ff]int[/color][/color] MISSING_TA = 2;
 
[color=#0000ff][color=#0000ff]const[/color][/color] [color=#0000ff][color=#0000ff]int[/color][/color] MISSING_EMAIL = 3;
 
[color=#0000ff][color=#0000ff]private[/color][/color] [color=#0000ff][color=#0000ff]readonly[/color][/color] [color=#0000ff][color=#0000ff]string[/color][/color] CrLf = [color=#2b91af][color=#2b91af]Environment[/color][/color].NewLine;
 
[color=#0000ff][color=#0000ff]private[/color][/color] [color=#0000ff][color=#0000ff]const[/color][/color] [color=#0000ff][color=#0000ff]string[/color][/color] CMDBARNAME = [color=#a31515][color=#a31515]"VDK_App"[/color][/color];
 
[color=#0000ff][color=#0000ff]private[/color][/color] Outlook.[color=#2b91af][color=#2b91af]Explorer[/color][/color] _Explorer; [color=#008000][color=#008000]// App walker
 
[/color][/color][color=#0000ff][color=#0000ff]private[/color][/color] Office.[color=#2b91af][color=#2b91af]CommandBar[/color][/color] cmdBar; [color=#008000][color=#008000]// Instance of Command bar 
 
[/color][/color][color=#0000ff][color=#0000ff]private[/color][/color] Office.[color=#2b91af][color=#2b91af]CommandBarComboBox[/color][/color] cboAction; [color=#008000][color=#008000]// Combo box - Step (in case ptocess)
 
[/color][/color][color=#0000ff][color=#0000ff]private[/color][/color] Office.[color=#2b91af][color=#2b91af]CommandBarButton[/color][/color] lblAction; [color=#008000][color=#008000]// Label
 
[/color][/color][color=#0000ff][color=#0000ff]private[/color][/color] Office.[color=#2b91af][color=#2b91af]CommandBarComboBox[/color][/color] cboStates; [color=#008000][color=#008000]// Combo box - States/Tax Authorities
 
[/color][/color][color=#0000ff][color=#0000ff]private[/color][/color] Office.[color=#2b91af][color=#2b91af]CommandBarButton[/color][/color] lblStates; [color=#008000][color=#008000]// Label
 
[/color][/color][color=#0000ff][color=#0000ff]private[/color][/color] Office.[color=#2b91af][color=#2b91af]CommandBarButton[/color][/color] lblAnon; [color=#008000][color=#008000]// Label
 
[/color][/color][color=#0000ff][color=#0000ff]private[/color][/color] Office.[color=#2b91af][color=#2b91af]CommandBarComboBox[/color][/color] cboAnon; [color=#008000][color=#008000]// Combo box - AnonNum
 
[/color][/color][color=#0000ff][color=#0000ff]private[/color][/color] Office.[color=#2b91af][color=#2b91af]CommandBarButton[/color][/color] btnLink; [color=#008000][color=#008000]// Button - Execute linking code
 
[/color][/color][color=#2b91af][color=#2b91af]SqlConnection[/color][/color] sConn = [color=#0000ff][color=#0000ff]new[/color][/color] [color=#2b91af][color=#2b91af]SqlConnection[/color][/color](); [color=#008000][color=#008000]// Common connection - reusable
 
[/color][/color][color=#0000ff][color=#0000ff]static[/color][/color] [color=#0000ff][color=#0000ff]string[/color][/color] strAnonNum; [color=#008000][color=#008000]// Used to call fillStates()
 
[/color][/color][color=#0000ff][color=#0000ff]public[/color][/color] [color=#2b91af][color=#2b91af]StringBuilder[/color][/color] sbMsg = [color=#0000ff][color=#0000ff]new[/color][/color] [color=#2b91af][color=#2b91af]StringBuilder[/color][/color](); [color=#008000][color=#008000]// Stringbuilder version is cleaner and faster 
 
[/color][/color]string ConnString = ("connnection stuff");
 
#endregion DECLARATIONS
[color=#0000ff][color=#0000ff]
#region[/color][/color] ThisAddIn Generated events
[color=#0000ff][color=#0000ff]private[/color][/color] [color=#0000ff][color=#0000ff]void[/color][/color] ThisAddIn_Startup([color=#0000ff][color=#0000ff]object[/color][/color] sender, System.[color=#2b91af][color=#2b91af]EventArgs[/color][/color] e)
 
{
 
 
 
BuildUI([color=#0000ff][color=#0000ff]ref[/color][/color] cmdBar, CMDBARNAME, [color=#0000ff][color=#0000ff]ref[/color][/color] _Explorer); [color=#008000][color=#008000]// Build the user interface [/color][/color][color=#008000][color=#008000]
 
[/color][/color]}
 
 
 
[color=#0000ff][color=#0000ff]private[/color][/color] [color=#0000ff][color=#0000ff]void[/color][/color] ThisAddIn_Shutdown([color=#0000ff][color=#0000ff]object[/color][/color] sender, System.[color=#2b91af][color=#2b91af]EventArgs[/color][/color] e)
 
{
 
[color=#008000][color=#008000]// SaveSettings(); // So we can return to last setup
 
[/color][/color]}
 
[color=#0000ff][color=#0000ff]#endregion[/color][/color] ThisAddIn Generated events
 
 
 
[color=#0000ff][color=#0000ff]#region[/color][/color] BUILDER CODE
 
[color=#0000ff][color=#0000ff]private[/color][/color] [color=#0000ff][color=#0000ff]void[/color][/color] BuildUI([color=#0000ff][color=#0000ff]ref[/color][/color] Office.[color=#2b91af][color=#2b91af]CommandBar[/color][/color] argBar, [color=#0000ff][color=#0000ff]string[/color][/color] argBarName, [color=#0000ff][color=#0000ff]ref[/color][/color] Outlook.[color=#2b91af][color=#2b91af]Explorer[/color][/color] argExplorer)
 
{
 
[color=#0000ff][color=#0000ff]try
 
[/color][/color]{
 
[color=#2b91af][color=#2b91af]StringBuilder[/color][/color] sb = [color=#0000ff][color=#0000ff]new[/color][/color] [color=#2b91af][color=#2b91af]StringBuilder[/color][/color]();
 
Office.[color=#2b91af][color=#2b91af]CommandBars[/color][/color] cmdBars = Application.ActiveExplorer().CommandBars;
 
Office.[color=#2b91af][color=#2b91af]CommandBar[/color][/color] cmdBarVDK;
 
cmdBarVDK = cmdBars.Add(CMDBARNAME, Office.[color=#2b91af][color=#2b91af]MsoBarPosition[/color][/color].msoBarTop, [color=#0000ff][color=#0000ff]false[/color][/color], [color=#0000ff][color=#0000ff]true[/color][/color]);
 
[color=#0000ff][color=#0000ff]for[/color][/color] ([color=#0000ff][color=#0000ff]int[/color][/color] i = 1; i <= cmdBars.Count; i++)
 
{
 
cmdBar = cmdBars[i];
 
[color=#808080][color=#808080]///[/color][/color][color=#008000][color=#008000] Checking for strays (Dev environment) V-Link add-in should only exist in the TEST environment
 
[/color][/color][color=#0000ff][color=#0000ff]if[/color][/color] (cmdBar.Name == [color=#a31515][color=#a31515]"V-Link"[/color][/color])
 
{
 
cmdBar.Visible = [color=#0000ff][color=#0000ff]false[/color][/color];
 
cmdBar.Enabled = [color=#0000ff][color=#0000ff]false[/color][/color];
 
} 
 
}
 
 
 
ButtonAdd([color=#0000ff][color=#0000ff]ref[/color][/color] cmdBar, [color=#0000ff][color=#0000ff]ref[/color][/color] btnLink, [color=#a31515][color=#a31515]"Link to VD&K"[/color][/color], [color=#a31515][color=#a31515]"btnLink"[/color][/color]);
 
ButtonAdd([color=#0000ff][color=#0000ff]ref[/color][/color] cmdBar, [color=#0000ff][color=#0000ff]ref[/color][/color] lblAnon, [color=#a31515][color=#a31515]"Anon #"[/color][/color], [color=#a31515][color=#a31515]"lblAnonNum"[/color][/color]); [color=#008000][color=#008000]// Label for Anon#
 
[/color][/color]cboAnon = (Office.[color=#2b91af][color=#2b91af]CommandBarComboBox[/color][/color])cmdBar.Controls.Add(Office.[color=#2b91af][color=#2b91af]MsoControlType[/color][/color].msoControlComboBox, missing, missing, missing, 1);
 
cboAnon.Caption = [color=#a31515][color=#a31515]"Anonymous number"[/color][/color];
 
cboAnon.Width = 60;
 
ButtonAdd([color=#0000ff][color=#0000ff]ref[/color][/color] cmdBar, [color=#0000ff][color=#0000ff]ref[/color][/color] lblStates, [color=#a31515][color=#a31515]"States"[/color][/color], [color=#a31515][color=#a31515]"lblStates"[/color][/color]); [color=#008000][color=#008000]// Label for States/Tax Authority
 
[/color][/color]cboStates = (Office.[color=#2b91af][color=#2b91af]CommandBarComboBox[/color][/color])cmdBar.Controls.Add(Office.[color=#2b91af][color=#2b91af]MsoControlType[/color][/color].msoControlComboBox, missing, missing, 1, 1);
 
cboStates.Caption = [color=#a31515][color=#a31515]"States/Tax Authority"[/color][/color];
 
cboStates.Width = 100;
 
ButtonAdd([color=#0000ff][color=#0000ff]ref[/color][/color] cmdBar, [color=#0000ff][color=#0000ff]ref[/color][/color] lblAction, [color=#a31515][color=#a31515]"Action"[/color][/color], [color=#a31515][color=#a31515]"lblAction"[/color][/color]); [color=#008000][color=#008000]// Label for Actions
 
[/color][/color]cboAction = (Office.[color=#2b91af][color=#2b91af]CommandBarComboBox[/color][/color])cmdBar.Controls.Add(Office.[color=#2b91af][color=#2b91af]MsoControlType[/color][/color].msoControlComboBox, missing, missing, 1, 1);
 
cboAction.Caption = [color=#a31515][color=#a31515]"Action"[/color][/color];
 
cboAction.Width = 300;
 
[color=#008000][color=#008000]//Event definitions
 
[/color][/color]cboAnon.Change += [color=#0000ff][color=#0000ff]new[/color][/color] Office.[color=#2b91af][color=#2b91af]_CommandBarComboBoxEvents_ChangeEventHandler[/color][/color](cboAnon_Change); 
 
btnLink.Click += [color=#0000ff][color=#0000ff]new[/color][/color] Office.[color=#2b91af][color=#2b91af]_CommandBarButtonEvents_ClickEventHandler[/color][/color](btnLink_Click);
 
cmdBar.Visible = [color=#0000ff][color=#0000ff]true[/color][/color];
 
cmdBar.Enabled = [color=#0000ff][color=#0000ff]true[/color][/color];
 
 
 
fillAnon();
 
fillAction();
 
}
 
[color=#0000ff][color=#0000ff]catch[/color][/color] ([color=#2b91af][color=#2b91af]ArgumentException[/color][/color] ex)
 
{
 
[color=#2b91af][color=#2b91af]MessageBox[/color][/color].Show(ex.Message);
 
}
 
}
 
[color=#008000][color=#008000]// CONTROL WRAPPERS
 
[/color][/color][color=#0000ff][color=#0000ff]private[/color][/color] [color=#0000ff][color=#0000ff]void[/color][/color] ComboBoxAdd([color=#0000ff][color=#0000ff]ref[/color][/color] Office.[color=#2b91af][color=#2b91af]CommandBar[/color][/color] argBar, [color=#0000ff][color=#0000ff]ref[/color][/color] Office.[color=#2b91af][color=#2b91af]CommandBarComboBox[/color][/color] argCBO,
 
[color=#0000ff][color=#0000ff]string[/color][/color] argCaption, [color=#0000ff][color=#0000ff]string[/color][/color] argTag)
 
{
 
argCBO = (Office.[color=#2b91af][color=#2b91af]CommandBarComboBox[/color][/color])argBar.Controls.Add(1, missing, missing, missing, missing);
 
argCBO.Style = Office.[color=#2b91af][color=#2b91af]MsoComboStyle[/color][/color].msoComboNormal;
 
argCBO.Caption = argCaption;
 
argCBO.Tag = argTag;
 
}
 
[color=#0000ff][color=#0000ff]private[/color][/color] [color=#0000ff][color=#0000ff]void[/color][/color] ButtonAdd([color=#0000ff][color=#0000ff]ref[/color][/color] Office.[color=#2b91af][color=#2b91af]CommandBar[/color][/color] argBar, [color=#0000ff][color=#0000ff]ref[/color][/color] Office.[color=#2b91af][color=#2b91af]CommandBarButton[/color][/color] argButton,
 
[color=#0000ff][color=#0000ff]string[/color][/color] argCaption, [color=#0000ff][color=#0000ff]string[/color][/color] argTag)
 
{
 
argButton = (Office.[color=#2b91af][color=#2b91af]CommandBarButton[/color][/color])argBar.Controls.Add(1, missing, missing, missing, missing);
 
argButton.Style = Office.[color=#2b91af][color=#2b91af]MsoButtonStyle[/color][/color].msoButtonCaption;
 
argButton.Caption = argCaption;
 
argButton.Tag = argTag;
 
}
 
 
 
[color=#008000][color=#008000]// EVENT HANDLERS
 
[/color][/color][color=#0000ff][color=#0000ff]private[/color][/color] [color=#0000ff][color=#0000ff]void[/color][/color] cboAnon_Change(Office.[color=#2b91af][color=#2b91af]CommandBarComboBox[/color][/color] Ctrl)
 
{
 
strAnonNum = Ctrl.Text.ToString();
 
[color=#008000][color=#008000]//MessageBox.Show("User chose: " + strAnonNum);
 
[/color][/color][color=#0000ff][color=#0000ff]if[/color][/color] (strAnonNum.Length < 2) [color=#008000][color=#008000]// Need better validation...
 
[/color][/color]{
 
[color=#2b91af][color=#2b91af]MessageBox[/color][/color].Show([color=#a31515][color=#a31515]"NO ANONYMOUS NUMBER SELECTED"[/color][/color] + CrLf +
 
CrLf + [color=#a31515][color=#a31515]"1) Select Anonymous number "[/color][/color] +
 
CrLf + [color=#a31515][color=#a31515]"2) Click the 'VDK Link' button"[/color][/color]);
 
[color=#0000ff][color=#0000ff]return[/color][/color];
 
}
 
fillStates(strAnonNum);
 
}
 
 
 
[color=#0000ff][color=#0000ff]void[/color][/color] btnLink_Click(Office.[color=#2b91af][color=#2b91af]CommandBarButton[/color][/color] Ctrl, [color=#0000ff][color=#0000ff]ref[/color][/color] [color=#0000ff][color=#0000ff]bool[/color][/color] CancelDefault)
 
{
 
[color=#0000ff][color=#0000ff]if[/color][/color] (Check_OkToPost() == 0)
 
{
 
Outlook.[color=#2b91af][color=#2b91af]MailItem[/color][/color] selItem;
 
selItem = (Outlook.[color=#2b91af][color=#2b91af]MailItem[/color][/color])[color=#0000ff][color=#0000ff]this[/color][/color].Application.ActiveExplorer().Selection[1];
 
[color=#0000ff][color=#0000ff]if[/color][/color] (selItem != [color=#0000ff][color=#0000ff]null[/color][/color])
 
{
 
[color=#008000][color=#008000]//strMsg = "*****************************************************************" + CrLf +
 
[/color][/color]sbMsg.Append([color=#a31515][color=#a31515]"*****************************************************************"[/color][/color] + CrLf +
 
[color=#a31515][color=#a31515]" LOGGING MESSAGE "[/color][/color] + CrLf +
 
[color=#a31515][color=#a31515]"*****************************************************************"[/color][/color] + CrLf +
 
CrLf + [color=#a31515][color=#a31515]"Anonymous Num : "[/color][/color] + [color=#0000ff][color=#0000ff]this[/color][/color].cboAnon.Text + [color=#a31515][color=#a31515]" "[/color][/color] +
 
CrLf + [color=#a31515][color=#a31515]"State/Tax Auth : "[/color][/color] + [color=#0000ff][color=#0000ff]this[/color][/color].cboStates.Text +
 
CrLf + [color=#a31515][color=#a31515]"Action/Workflow : "[/color][/color] + [color=#0000ff][color=#0000ff]this[/color][/color].cboAction.Text + CrLf +
 
CrLf + [color=#a31515][color=#a31515]"From : "[/color][/color] + selItem.To.ToString() +
 
CrLf + [color=#a31515][color=#a31515]"To : "[/color][/color] + selItem.SenderName.ToString() + [color=#a31515][color=#a31515]" "[/color][/color] + selItem.SenderEmailAddress.ToString() +
 
CrLf + [color=#a31515][color=#a31515]"Subject : "[/color][/color] + selItem.Subject.ToString() +
 
CrLf + [color=#a31515][color=#a31515]"Received : "[/color][/color] + selItem.ReceivedTime.ToString() +
 
CrLf + [color=#a31515][color=#a31515]"Body: "[/color][/color] + selItem.Body.ToString());
 
[color=#2b91af][color=#2b91af]MessageBox[/color][/color].Show(sbMsg.ToString(), [color=#a31515][color=#a31515]"Current message(Explorer)"[/color][/color]);
 
}
 
 
 
}
 
}
 
 
 
[color=#0000ff][color=#0000ff]#endregion[/color][/color] BUILDER CODE
 
Back
Top