|
-
Jul 9th, 2004, 02:42 PM
#1
Thread Starter
Hyperactive Member
Attachment from Outlook
Using Outlook rules wizard and VB.NET how would I extract an attachment from an email? Once I have the file I will be importing it into SQL Server or Access.
-
Aug 11th, 2005, 03:45 AM
#2
Fanatic Member
Re: Attachment from Outlook
Texas,
Did you find a solution for this problem?
I am wanting to do the same thing.
I know how to connect to the mailbox and I know how to check if the email has an attachment, just don't know how to display it/link to it and or save it or import it to my db?
See - http://www.vbforums.com/showthread.php?t=352571
I would be very grateful for any advise
Cheers Al
-
Aug 11th, 2005, 12:11 PM
#3
Thread Starter
Hyperactive Member
Re: Attachment from Outlook
no idea to be honest.
i think i ended up having someone or something drop the file on the network then i imported it into sql.
-
Aug 11th, 2005, 12:38 PM
#4
Re: Attachment from Outlook
Outlook code is almost the same in VB6 and VB.NET. I have allot of code examples on the Forums but here is one that should show you the mechanics and logic involved. Just customize to your needs. 
Outlook Attachments
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Aug 12th, 2005, 04:48 AM
#5
Fanatic Member
Re: Attachment from Outlook
Gangsta Y,
Thanks very much, I hope you don't mind if I ask a few questions.
I am looking to get the messages directly out of exchange and want to pass in different mailbox ID depending on which group the user who's logged in belongs to. Is this possible?
So it may be a sales mailbox or a marketing mailbox. Was hoping I could do something like -
VB Code:
'Set oNS = oApp.GetNamespace("MAPI")
Set oNS = oApp.GetNamespace("MAPILEVEL=Mailbox - Joe Bloggs")
I've managed to get your sample working, have commented out these lines, was getting a permissions error, plus I don't understand what they're supposed to be doing?
VB Code:
Set oMsg = oApp.CreateItemFromTemplate("C:\Item[" & i & "].msg")
oMsg.Attachments.Item(1).SaveAsFile ("C:\Item[" & i & "].log")
Kill "C:\Item[" & i & "].msg"
so am just saving the attachment using -
VB Code:
oAttach.SaveAsFile ("C:\" & oAttach.FileName)
On the side: Once I get this thing working in .Net I'll be moving it to ASP.NET
Cheers Al
Last edited by aconybeare; Aug 12th, 2005 at 06:50 AM.
Reason: Added MapiLevel snippet
-
Aug 12th, 2005, 10:35 AM
#6
Re: Attachment from Outlook
Ok, here is how you can logon to opther mailboxes if you have permissions.
VB Code:
Option Explicit
'Add a reference to Outlook xx.0 object library
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oApp As Outlook.Application
Dim oNS As Outlook.Namespace
oApp = New Outlook.Application
oNS = oApp.GetNamespace("MAPI")
oNS.Logon "ProfileName", "Password", "ShowDialog", "NewSession"
End Sub
You dont need the CreateItemFromTemplate because it is just opening a saved message from the filesystem. The SaveAsFile you should provide a complete path or it will save to the temp dir.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Aug 12th, 2005, 11:03 AM
#7
Fanatic Member
Re: Attachment from Outlook
RobDog,
Thanks a whole bunch for that, do you know whether this will work if it's running on a web server with no one logged in?
I have a horrible feeling it won't... although with the method of connection that you've described above (passing in the username & pwd) it could work??
This is what I have so far, what do you think -
VB Code:
Private Sub btnSaveAttach_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveAttach.Click
Dim oApp As New Outlook.Application
Dim oNS As Outlook.NameSpace
Dim oFolder As Outlook.MAPIFolder
Dim oEmail As Outlook.MailItem
Dim oAttachs As Outlook.Attachments
Dim oAttach As Outlook.Attachment
Dim i As Integer
oNS = oApp.GetNamespace("MAPI")
oFolder = GetFolder("Editorial\Inbox")
For Each oEmail In oFolder.Items
MessageBox.Show(oEmail.Subject)
oAttachs = oEmail.Attachments
For i = 1 To oAttachs.Count
oAttach = oAttachs.Item(i)
MessageBox.Show(oAttach.DisplayName & " " & oAttach.FileName)
If oAttach.FileName = "Extensions.doc" Then
oAttach.SaveAsFile("C:\" & oAttach.FileName)
End If
oAttach = Nothing
Next
Next oEmail
oAttachs = Nothing
oEmail = Nothing
oFolder = Nothing
oNS = Nothing
' Exit Outlook - shuts it down
'oApp.Quit
oApp = Nothing
End Sub
Public Function GetFolder(ByVal sFolderPath As String) As MAPIFolder
' folder path needs to be something like
' "Public Folders\All Public Folders\Company\Sales"
' source - [url]http://www.outlookcode.com/d/code/getfolder.htm[/url]
Dim objApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim colFolders As Outlook.Folders
Dim objFolder As Outlook.MAPIFolder
Dim arrFolders() As String
Dim i As Long
'On Error Resume Next
sFolderPath = Replace(sFolderPath, "/", "\")
arrFolders = Split(sFolderPath, "\")
objApp = CreateObject("Outlook.Application")
objNS = objApp.GetNamespace("MAPI")
objFolder = objNS.Folders.Item(arrFolders(0))
If Not objFolder Is Nothing Then
For i = 1 To UBound(arrFolders)
colFolders = objFolder.Folders
objFolder = Nothing
objFolder = colFolders.Item(arrFolders(i))
If objFolder Is Nothing Then
Exit For
End If
Next
End If
GetFolder = objFolder
colFolders = Nothing
objNS = Nothing
objApp = Nothing
End Function
-
Aug 12th, 2005, 11:07 AM
#8
Re: Attachment from Outlook
I dont believe it will run if the system is locked. What you would be better off doing is writting a web service or plain service.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Aug 12th, 2005, 11:35 AM
#9
Fanatic Member
Re: Attachment from Outlook
Right looks like I'll have to look into the web service thing then
This code works great, albeit a bit slow, but I can't work out how to get the attachments, there is a flag in the returned dataset which marks whether there is an attachment or not. How to get them that's the question?
VB Code:
Private Sub btnOpenInbox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenInbox.Click
Dim ds As New DataSet
Dim dt As DataTable
Try
'SELECT Subject, Contents FROM Inbox
Dim da As New OleDbDataAdapter("SELECT * FROM Inbox", _
"Provider=Microsoft.Jet.OLEDB.4.0;Outlook 9.0;" & _
"MAPILEVEL=Mailbox - Jo Smith|;PROFILE=MS Exchange Settings;" & _
"TABLETYPE=0;DATABASE=C:\WINNT\Temp")
da.Fill(ds, "Outlook")
dt = ds.Tables("Outlook")
DataGrid1.DataSource = dt
Catch exc As OleDbException
Dim OleDBError1 As OleDbError
For Each OleDBError1 In exc.Errors
Console.WriteLine(OleDBError1.Message)
Next
End Try
End Sub
Thanks for all your help
Cheers Al
-
Aug 12th, 2005, 11:40 AM
#10
Re: Attachment from Outlook
Nope, not at the folder level. You would need to check each item in the folder for the existance of it in the Attachments collection of the item.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|