473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Saving outlook email attachment?

how to save outlook email attachments using VB.net?

Apr 25 '06 #1
6 10458
Public Sub GetAttachments()
' Declare variables
Dim ns As [NameSpace]
Dim Inbox As MAPIFolder
Dim Item As MailItem
Dim SubFolder As MAPIFolder
Dim Atmt As Attachment
Dim FileName As String
Dim olApp As Application

Dim myRecipient As Recipient
olApp = CreateObject("Outlook.Application")
ns = olApp.GetNamespace("MAPI")
' Set myRecipient =outlook ns.CreateRecipient("help")
'myRecipient.Resolve
Inbox = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox )
SubFolder = Inbox.Folders("Inbox")
' Check Inbox for messages and exit of none found
If SubFolder.Items.Count = 0 Then
Exit Sub
End If
' Check each message for attachments
For Each Item In SubFolder.Items
' Save attachments found
For Each Atmt In Item.Attachments
FileName = "h:\Email Attachments\" &
Atmt.FileName
Atmt.SaveAsFile(FileName)
End If
Next Atmt
End If
Next Item

Atmt = Nothing
Item = Nothing
ns = Nothing
End Sub

c_shah wrote:
how to save outlook email attachments using VB.net?


Apr 25 '06 #2
I am getting compilation error
this is my code
Imports Outlook = Microsoft.Office.Interop.Outlook

' Create Outlook Application.
Dim objApp As Outlook.Application = New Outlook.Application

'Get Mapi NameSpace
Dim objNS As Outlook.NameSpace = objApp.GetNamespace("MAPI")
' Get Messages collection of Inbox.
Dim objInbox As Outlook.MAPIFolder =
objNS.GetDefaultFolder(Outlook.OlDefaultFolders.ol FolderInbox)
Dim objItems As Outlook.Items = objInbox.Items

' Get unread e-mail messages.
objItems = objItems.Restrict("[Unread] = true")

Dim item As Outlook.MailItem

Dim i As Integer

'Dim Item As Object

For i = 1 To objItems.Count
' MessageBox.Show(objItems.Item(i).Subject)
' MessageBox.Show(objItems.Item(i).Body)

Dim objAttachment As Outlook.Attachment
For Each objAttachment In objItems.Item(i)

Dim filename As String
filename = "C:\Temp\" + objAttachment.FileName
objAttachment.SaveAsFile(filename)
Next objAttachment
Next

Apr 25 '06 #3
C_shah,
I am getting compilation error this is my code

Probably you did not set a reference or something like that.

Cor
Apr 26 '06 #4
It is working now.
Here is my complete code

I am getting a security warning from outlook "a program is trying to
access email address from outlook" how to get rid of that message?

Also, instead of "Unread items" from the inbox I would like to use
selected(highlighted) items from the inbox any way to do this

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' Create Outlook Application.
Dim objApp As Outlook.Application = New Outlook.Application

'Get Mapi NameSpace
Dim objNS As Outlook.NameSpace = objApp.GetNamespace("MAPI")

objNS.Logon("GA\Cshah", Missing.Value, False, True)

' Get Messages collection of Inbox.
Dim objInbox As Outlook.MAPIFolder =
objNS.GetDefaultFolder(Outlook.OlDefaultFolders.ol FolderInbox)
Dim objItems As Outlook.Items = objInbox.Items

' Get unread e-mail messages. (Collection)
objItems = objItems.Restrict("[UnRead] = true")

' objItems = objItems.Restrict(

'Mail Item
Dim oMsg As Outlook.MailItem

For Each oMsg In objItems

Dim objAttachment As Outlook.Attachment
For Each objAttachment In oMsg.Attachments

Dim filename As String
filename = "C:\Temp\" + objAttachment.FileName
objAttachment.SaveAsFile(filename)
Next objAttachment
Next

Label1.Text = "Attachments are saved"
End Sub

Apr 26 '06 #5
C_Shah,

Have a look in this thread there is no 1 to 1 answer, so maybe you find it
in there.

http://groups.google.com/group/micro...a6683ae027bb23

I hope this helps,

Cor

"c_shah" <sh*********@netzero.net> schreef in bericht
news:11**********************@g10g2000cwb.googlegr oups.com...
It is working now.
Here is my complete code

I am getting a security warning from outlook "a program is trying to
access email address from outlook" how to get rid of that message?

Also, instead of "Unread items" from the inbox I would like to use
selected(highlighted) items from the inbox any way to do this

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' Create Outlook Application.
Dim objApp As Outlook.Application = New Outlook.Application

'Get Mapi NameSpace
Dim objNS As Outlook.NameSpace = objApp.GetNamespace("MAPI")

objNS.Logon("GA\Cshah", Missing.Value, False, True)

' Get Messages collection of Inbox.
Dim objInbox As Outlook.MAPIFolder =
objNS.GetDefaultFolder(Outlook.OlDefaultFolders.ol FolderInbox)
Dim objItems As Outlook.Items = objInbox.Items

' Get unread e-mail messages. (Collection)
objItems = objItems.Restrict("[UnRead] = true")

' objItems = objItems.Restrict(

'Mail Item
Dim oMsg As Outlook.MailItem

For Each oMsg In objItems

Dim objAttachment As Outlook.Attachment
For Each objAttachment In oMsg.Attachments

Dim filename As String
filename = "C:\Temp\" + objAttachment.FileName
objAttachment.SaveAsFile(filename)
Next objAttachment
Next

Label1.Text = "Attachments are saved"
End Sub

Apr 26 '06 #6
Thank you Cor.

Also, anyone know a property that exposes currently selected emails
from the outlook? So instead of "Unread items" from the inbox I would
like to read selected(highlighted) items from the inbox

Apr 26 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: john bailo | last post by:
For a c# program to automatically extract information from email attachments using the Outlook 9 OM, rather than saving to a file, then reading the file, I would prefer to 'Save' the attachment to...
4
by: DeeJay | last post by:
Hi all. I need to create program that will extract attachments from Outlook 2000 inbox and save it on disk. Detailed how-to & sample code would be highly appreciated. Regards, DeeJay.
0
by: Karen Grube | last post by:
Hi! I hate to bother you all with this, but I don't know how best to approach a particular task. Here's the deal: Once a month I personally (that is, in my own personal inbox on my...
5
by: Siv | last post by:
Hi, A little while ago I asked if anyone could help me with how to create an email using MS Outlook that contained an embedded picture file. Thanks to Jay Harlow I was able to get this working...
5
by: J-P-W | last post by:
I have some code, from this group (many thanks) that sends an attachment to an email. The following: Dim objNewMail As Outlook.MailItem Dim golApp As...
7
by: Dean Spencer | last post by:
Can anyone help? I am importing Emails from Outlook using the following code: Public Function ScanInbox(SubjectLine As String) Dim TempRst As Recordset Dim OlApp As Outlook.Application Dim...
5
by: Kosmos | last post by:
I have traveled the world and the seven seas and I have yet to come up with an answer to this question.... So I'm adding an attachment to an email from access... The following is the code: ...
7
by: Paridevi | last post by:
Hai , i want to send email in .Net Using OutLook Express,My Project is Web Application using vb.Net 2003 with SQL Server 2000.I have searched a lot in Google, but ican't get any...
6
by: dlblack | last post by:
I am using the following code to send emails with attachments from Access using Outlook. The code works fine if Outlook is already open and emails are sent as expected via Outlook. When Outlook is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.