Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB.NET Outlook

Status
Not open for further replies.

tsp1lrk72

IS-IT--Management
Feb 25, 2008
100
US
bgaines72 posted this code:

Code:
Dim olApp As Outlook.Application = New Outlook.Application()
        Dim olNS As Outlook.NameSpace = olApp.GetNamespace("MAPI")
        olNS.Logon("Brian Gaines", "", False, True)
        Dim olInbox As Outlook.MAPIFolder = olNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        Dim olItems As Outlook.Items = olInbox.Items
        Dim i As Integer
        Try
            If olItems.Count > 0 Then
                ListBox1.Items.Clear()
                For i = 1 To olItems.Count
                    Dim email As Outlook.MailItem = olItems.Item(i)

                    ListBox1.Items.Add(email.Subject & " >>> FROM: " & email.SenderName)
                Next
                Label1.Text = "Emails: " & (i - 1).ToString
                olNS.Logoff()
            End If
        Catch ex As Exception
            lblMessage.Text = ex.ToString
        Finally
            olApp = Nothing
            olNS = Nothing
            olItems = Nothing
            olInbox = Nothing
        End Try

Is there a way to save an attachment from a specific email address to the c: drive?

Thanks
 
I have this code:

Code:
Public Sub GetAttachment()
        Try
            Dim objmessage As Outlook.MailItem
            Dim oApp As Outlook.Application = New Outlook.Application
            Dim oNS As Outlook.NameSpace = oApp.GetNamespace("MAPI")
            Dim MailAttachment As Outlook.Attachment
            Dim oInbox As Outlook.MAPIFolder = oNS.GetDefaultFolder("objRecip, Outlook.OlDefaultFolders.olFolderInbox")
            'Dim intcount As Integer
            Dim SearchString As String
            Dim strName As String
            strName = "EDI-MAN"
            Dim objRecip As Outlook.Recipient
            'intcount = oInbox.Items.Count
            objRecip = oNS.CreateRecipient(strName)
            objRecip.Resolve()
            For Each MailAttachment In oInbox.Items.Attachments
                SearchString = objmessage.Body
                If InStr(1, SearchString, "Attached is the Daily Interchange Report from Inovis", 1) Then
                    MailAttachment.SaveAsFile("\\Enap-svce1\Inovis\InovisReports\" & MailAttachment.FileName)
                End If
                objmessage.Save()
            Next
        Catch e As System.Exception
            MsgBox("An error occured getting the attachment: " & e.Message)
        End Try
    End Sub

and when I debug it, I get this:
Cast from string "objRecip,Outlook.OlDefaultFolder" to type Integer is not valid...

Why is it referring to an Integer when I'm trying to access an Exchange mailbox?

Thanks-
 
because it shouldn't be a string, try this.

Code:
Public Sub GetAttachment()
        Try
            Dim objmessage As Outlook.MailItem
            Dim oApp As Outlook.Application = New Outlook.Application
            Dim oNS As Outlook.NameSpace = oApp.GetNamespace("MAPI")
            Dim MailAttachment As Outlook.Attachment
            Dim oInbox As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
            'Dim intcount As Integer
            Dim SearchString As String
            Dim strName As String
            strName = "EDI-MAN"
            Dim objRecip As Outlook.Recipient
            'intcount = oInbox.Items.Count
            objRecip = oNS.CreateRecipient(strName)
            objRecip.Resolve()
            For Each MailAttachment In oInbox.Items.Attachments
                SearchString = objmessage.Body
                If InStr(1, SearchString, "Attached is the Daily Interchange Report from Inovis", 1) Then
                    MailAttachment.SaveAsFile("\\Enap-svce1\Inovis\InovisReports\" & MailAttachment.FileName)
                End If
                objmessage.Save()
            Next
        Catch e As System.Exception
            MsgBox("An error occured getting the attachment: " & e.Message)
        End Try
    End Sub

Christiaan Baes
Belgium

My Blog
 
Thanks... that message went away, now I'm stuck with:

"Public Member 'Attachments' on type 'ItemClass' not found...

????

Thanks again...
 
For Each MailAttachment In oInbox.Items.Attachments
 
If I remember right you can't do that because both Items and Attachments are arrays. You would need two for each loops. One to iterate through Items and one for attachments.

-I hate Microsoft!
-Forever and always forward.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top