Results 1 to 10 of 10

Thread: Attachment from Outlook

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Austin
    Posts
    397

    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.

  2. #2
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Austin
    Posts
    397

    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.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  5. #5
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    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:
    1. 'Set oNS = oApp.GetNamespace("MAPI")
    2. 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:
    1. Set oMsg = oApp.CreateItemFromTemplate("C:\Item[" & i & "].msg")
    2. oMsg.Attachments.Item(1).SaveAsFile ("C:\Item[" & i & "].log")
    3. Kill "C:\Item[" & i & "].msg"
    so am just saving the attachment using -
    VB Code:
    1. 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

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Attachment from Outlook

    Ok, here is how you can logon to opther mailboxes if you have permissions.
    VB Code:
    1. Option Explicit
    2. 'Add a reference to Outlook xx.0 object library
    3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.     Dim oApp As Outlook.Application
    5.     Dim oNS As Outlook.Namespace
    6.     oApp = New Outlook.Application
    7.     oNS = oApp.GetNamespace("MAPI")
    8.     oNS.Logon "ProfileName", "Password", "ShowDialog", "NewSession"
    9. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  7. #7
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    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:
    1. Private Sub btnSaveAttach_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveAttach.Click
    2.         Dim oApp As New Outlook.Application
    3.         Dim oNS As Outlook.NameSpace
    4.         Dim oFolder As Outlook.MAPIFolder
    5.         Dim oEmail As Outlook.MailItem
    6.         Dim oAttachs As Outlook.Attachments
    7.         Dim oAttach As Outlook.Attachment
    8.         Dim i As Integer
    9.  
    10.         oNS = oApp.GetNamespace("MAPI")
    11.         oFolder = GetFolder("Editorial\Inbox")
    12.         For Each oEmail In oFolder.Items
    13.             MessageBox.Show(oEmail.Subject)
    14.  
    15.             oAttachs = oEmail.Attachments
    16.             For i = 1 To oAttachs.Count
    17.                 oAttach = oAttachs.Item(i)
    18.                 MessageBox.Show(oAttach.DisplayName & " " & oAttach.FileName)
    19.                 If oAttach.FileName = "Extensions.doc" Then
    20.                     oAttach.SaveAsFile("C:\" & oAttach.FileName)
    21.                 End If
    22.                 oAttach = Nothing
    23.             Next
    24.         Next oEmail
    25.         oAttachs = Nothing
    26.         oEmail = Nothing
    27.         oFolder = Nothing
    28.         oNS = Nothing
    29.         ' Exit Outlook - shuts it down
    30.         'oApp.Quit
    31.         oApp = Nothing
    32.     End Sub
    33.  
    34.     Public Function GetFolder(ByVal sFolderPath As String) As MAPIFolder
    35.         ' folder path needs to be something like
    36.         '   "Public Folders\All Public Folders\Company\Sales"
    37.         ' source - [url]http://www.outlookcode.com/d/code/getfolder.htm[/url]
    38.         Dim objApp As Outlook.Application
    39.         Dim objNS As Outlook.NameSpace
    40.         Dim colFolders As Outlook.Folders
    41.         Dim objFolder As Outlook.MAPIFolder
    42.         Dim arrFolders() As String
    43.         Dim i As Long
    44.         'On Error Resume Next
    45.  
    46.         sFolderPath = Replace(sFolderPath, "/", "\")
    47.         arrFolders = Split(sFolderPath, "\")
    48.         objApp = CreateObject("Outlook.Application")
    49.         objNS = objApp.GetNamespace("MAPI")
    50.         objFolder = objNS.Folders.Item(arrFolders(0))
    51.         If Not objFolder Is Nothing Then
    52.             For i = 1 To UBound(arrFolders)
    53.                 colFolders = objFolder.Folders
    54.                 objFolder = Nothing
    55.                 objFolder = colFolders.Item(arrFolders(i))
    56.                 If objFolder Is Nothing Then
    57.                     Exit For
    58.                 End If
    59.             Next
    60.         End If
    61.  
    62.         GetFolder = objFolder
    63.         colFolders = Nothing
    64.         objNS = Nothing
    65.         objApp = Nothing
    66.     End Function

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  9. #9
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    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:
    1. Private Sub btnOpenInbox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenInbox.Click
    2.         Dim ds As New DataSet
    3.         Dim dt As DataTable
    4.  
    5.         Try
    6.             'SELECT Subject, Contents FROM Inbox
    7.             Dim da As New OleDbDataAdapter("SELECT * FROM Inbox", _
    8.                                             "Provider=Microsoft.Jet.OLEDB.4.0;Outlook 9.0;" & _
    9.                                             "MAPILEVEL=Mailbox - Jo Smith|;PROFILE=MS Exchange Settings;" & _
    10.                                             "TABLETYPE=0;DATABASE=C:\WINNT\Temp")
    11.  
    12.             da.Fill(ds, "Outlook")
    13.             dt = ds.Tables("Outlook")
    14.             DataGrid1.DataSource = dt
    15.         Catch exc As OleDbException
    16.             Dim OleDBError1 As OleDbError
    17.             For Each OleDBError1 In exc.Errors
    18.                 Console.WriteLine(OleDBError1.Message)
    19.             Next
    20.         End Try
    21.  
    22.     End Sub

    Thanks for all your help

    Cheers Al

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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
  •  



Click Here to Expand Forum to Full Width