[FAQ's: OD] How do I save/remove an attachment from an email message?
An Outlook email is a MailItem type. This type has an .Attachments collection that also contains a .SaveAsFile and .Delete methods that we can take advantage of to perform our task(s).
Outlook 2003 and VB 6 Code Example:
VB Code:
Option Explicit
'Add a reference to MS Outlook xx.0 Object Library
Private Sub Command1_Click()
Dim oApp As Outlook.Application
Dim oInbox As Outlook.MAPIFolder
Dim oEmail As Object
Dim oAttach As Outlook.Attachment
Set oApp = New Outlook.Application
'Reference the default Inbox
Set oInbox = oApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
For Each oEmail In oInbox.Items
'Make sure its a mailitem and not a meeting request or read receipt etc.
If oEmail.Class = olMail Then
'Check if it has any attachments
If oEmail.Attachments.Count > 0 Then
For Each oAttach In oEmail.Attachments
oAttach.SaveAsFile "C:\" & oAttach.FileName
'Uncomment next line to optionally remove the attachment
'oAttach.Delete
'oEmail.Save ?
Next
End If
End If
Next
'Clean up
Set oEmail = Nothing
Set oInbox = Nothing
oApp.Quit
Set oApp = Nothing
End Sub
Re: [FAQ's: OD] How do I save/remove an attachment from an email message?
Outlook 2003 And VB.NET 2003 Code Example:
VB Code:
Option Explicit On
Option Strict On
'Add a reference to MS Office Outlook xx.0 Object Library
Imports Microsoft.Office.Interop
Public Class Form1
Inherits System.Windows.Forms.Form
[color=dimgray]"Windows Form Designer generated code"[/color]
Private moApp As Outlook.Application
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
moApp = DirectCast(GetObject(, "Outlook.Application"), Outlook.Application)
Catch ex As Exception
If TypeName(moApp) = "Nothing" Then
moApp = DirectCast(CreateObject("Outlook.Application"), Outlook.Application)
Else
MessageBox.Show(ex.Message, "VB/Office Guruâ„¢ Outlook Attachment Demo .NET", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
btnSave.Enabled = False
End If
End Try
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim oInbox As Outlook.MAPIFolder
Dim oEmail As Object
Dim oAttach As Outlook.Attachment
'Reference the default Inbox
oInbox = moApp.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
For Each oEmail In oInbox.Items
'Make sure its a mailitem and not a meeting request or read receipt etc.
If CType(oEmail, Outlook.MailItem).Class = Outlook.OlObjectClass.olMail Then
'Check if it has any attachments
If CType(oEmail, Outlook.MailItem).Attachments.Count > 0 Then
For Each oAttach In CType(oEmail, Outlook.MailItem).Attachments
oAttach.SaveAsFile("C:\" & oAttach.FileName)
'Uncomment next lines to optionally remove the attachment
'oAttach.Delete
'oEmail.Save ?
Next
End If
End If
Next
'Clean up
oAttach = Nothing
oEmail = Nothing
oInbox = Nothing
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
moApp.Quit()
moApp = Nothing
End Sub
End Class
Re: [FAQ's: OD] How do I save/remove an attachment from an email message?
Outlook 2003 And C# 2003 Code Example:
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Outlook = Microsoft.Office.Interop.Outlook;
///using Outlook = Microsoft.Office.Core.Outlook;
namespace Outlook_Attachments_CS
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private Outlook.Application moApp;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
"Windows Form Designer generated code"
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
moApp = new Outlook.Application();
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
moApp.Quit();
moApp = null;
}
private void btnSave_Click(object sender, System.EventArgs e)
{
Outlook.MAPIFolder oInbox;
///Reference the default Inbox
oInbox = moApp.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
foreach (Outlook.MailItem oEmail in oInbox.Items)
{
///Make sure its a mailitem and not a meeting request or read receipt etc.
if (((Outlook.MailItem)(oEmail)).Class == Outlook.OlObjectClass.olMail)
{
///Check if it has any attachments
if (((Outlook.MailItem)(oEmail)).Attachments.Count > 0)
{
foreach (Outlook.Attachment oAttach in ((Outlook.MailItem)(oEmail)).Attachments)
{
oAttach.SaveAsFile(@"C:\" + oAttach.FileName);
///Uncomment next lines to optionally remove the attachment
///oAttach.Delete
///oEmail.Save
}
}
}
}
oInbox = null;
}
}
}