borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (destroying instances of Excel)Next Topic (FileDialog Property doesn\'t work) New Topic New Poll Post Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : HELP!!!!
Poster Message
pcvillaluz
Level: Graduate

Registered: 04-10-2005
Posts: 9

icon HELP!!!!

How do i extract email attachments from a particular person in MS outlook using VB? pls guys need ur help

04-10-2005 at 01:33 AM
View Profile Send Email to User Show All Posts | Quote Reply
TJ_01
Level: VB Lord


Registered: 24-08-2005
Posts: 320
icon Re: HELP!!!!

I have not try this code yet. Check it out if it helps..

Private Sub Command1_Click()
Dim oApp As Outlook.Application
Dim oNameSpace As NameSpace
Dim oFolder As MAPIFolder
Dim oMailItem As Object
Dim sMessage As String

Set oApp = New Outlook.Application
Set oNameSpace = oApp.GetNamespace("MAPI")
Set oFolder = oNameSpace.GetDefaultFolder(olFolderInbox)
For Each oMailItem In oFolder.Items
With oMailItem
If oMailItem.Attachments.Count > 0 Then
  oMailItem.Attachments.Item(1).SaveAsFile "C:\Temp\OutlookAttachments\" & _
  oMailItem.Attachments.Item(1).filename
End If
MsgBox oMailItem.Attachments.Item(1).DisplayName & " was saved as " & _
oMailItem.Attachments.Item(1).filename
End If
End With
Next oMailItem

Set oMailItem = Nothing
Set oFolder = Nothing
Set oNameSpace = Nothing
Set oApp = Nothing
End Sub

04-10-2005 at 01:44 AM
View Profile Send Email to User Show All Posts | Quote Reply
pcvillaluz
Level: Graduate

Registered: 04-10-2005
Posts: 9
icon Re: HELP!!!!

quote:
TJ_01 wrote:
I have not try this code yet. Check it out if it helps..

Private Sub Command1_Click()
Dim oApp As Outlook.Application
Dim oNameSpace As NameSpace
Dim oFolder As MAPIFolder
Dim oMailItem As Object
Dim sMessage As String

Set oApp = New Outlook.Application
Set oNameSpace = oApp.GetNamespace("MAPI")
Set oFolder = oNameSpace.GetDefaultFolder(olFolderInbox)
For Each oMailItem In oFolder.Items
With oMailItem
If oMailItem.Attachments.Count > 0 Then
  oMailItem.Attachments.Item(1).SaveAsFile "C:\Temp\OutlookAttachments\" & _
  oMailItem.Attachments.Item(1).filename
End If
MsgBox oMailItem.Attachments.Item(1).DisplayName & " was saved as " & _
oMailItem.Attachments.Item(1).filename

End If
End With
Next oMailItem

Set oMailItem = Nothing
Set oFolder = Nothing
Set oNameSpace = Nothing
Set oApp = Nothing
End Sub



the bold part produces an error "Array index is out of bounds"
tnx plus i need it to be coming from a partciluar email address how do i do that?
04-10-2005 at 02:05 AM
View Profile Send Email to User Show All Posts | Quote Reply
TJ_01
Level: VB Lord


Registered: 24-08-2005
Posts: 320
icon Re: HELP!!!!

I not working too much in email. I just got this code from my co-programmer here in the office. I assume that the error occurs if there is no email attached. Try to change from

MsgBox oMailItem.Attachments.Item(1).DisplayName & " was saved as " & _
oMailItem.Attachments.Item(1).filename


To this and see if something screw up:

MsgBox oMailItem.Attachments.Item(0).DisplayName & " was saved as " oMailItem.Attachments.Item(0).filename


quote:
tnx plus i need it to be coming from a partciluar email address how do i do that?

See the attached zip files and it helps..


[Edited by TJ_01 on 04-10-2005 at 02:25 AM GMT]

____________________________
Attached:
RetriveEmailDetails.zip 6 KB (Downloads: 1)
04-10-2005 at 02:15 AM
View Profile Send Email to User Show All Posts | Quote Reply
pcvillaluz
Level: Graduate

Registered: 04-10-2005
Posts: 9
icon Re: HELP!!!!


MsgBox oMailItem.Attachments.Item(0).DisplayName & " was saved as " oMailItem.Attachments.Item(0).filename  
Endif

Still returns the same error plus the end if has no if.tnx

04-10-2005 at 02:37 AM
View Profile Send Email to User Show All Posts | Quote Reply
TJ_01
Level: VB Lord


Registered: 24-08-2005
Posts: 320
icon Re: HELP!!!!

Did you add the outlook in the project preference? It works fine. I just test it.

Private Sub Command1_Click()
Dim oApp As Outlook.Application
Dim oNameSpace As NameSpace
Dim oFolder As MAPIFolder
Dim oMailItem As Object
Dim sMessage As String

Set oApp = New Outlook.Application
Set oNameSpace = oApp.GetNamespace("MAPI")
Set oFolder = oNameSpace.GetDefaultFolder(olFolderInbox)
For Each oMailItem In oFolder.Items
With oMailItem
If oMailItem.Attachments.Count > 0 Then
  oMailItem.Attachments.Item(1).SaveAsFile "C:\Temp\OutlookAttachments\" & _
  oMailItem.Attachments.Item(1).FileName
End If
MsgBox oMailItem.Attachments.Item(1).DisplayName & " was saved as " & _
oMailItem.Attachments.Item(1).FileName

End With
Next oMailItem

Set oMailItem = Nothing
Set oFolder = Nothing
Set oNameSpace = Nothing
Set oApp = Nothing
End Sub


Another way is:

'**************************************
' Name: Save Outlook Mail Attachment
' Description:This code snippet is actua
'     lly a Macro for Outlook 97, 98 or 2000 b
'     ut can be easily instituted into VB by c
'     reating your Outlook.Application object
'     to completely automate the mod from VB.
'     It could also be used with MAPI mail as
'     well.
' By: Chris Kesler
'
'
' Inputs:None...
'
' Returns:None... Could be set as a True
'     or False Method for total automation
'
'Assumes:Basic understanding of VBA and
'     VB as well as Office Automation through
'     VB.
'
'Side Effects:None so far...
'This code is copyrighted and has limite
'     d warranties.
'Please see http://www.Planet-Source-Cod
'     e.com/xq/ASP/txtCodeId.27911/lngWId.1/qx
'     /vb/scripts/ShowCode.htm
'for details.
'**************************************



Sub AutomateMe()
    Dim oApp As Application
    Dim oNS As NameSpace
    Dim oMsg As Object
    Dim oAttachments As Outlook.Attachments
    Dim strControl
    Set oApp = New Outlook.Application
    Set oNS = oApp.GetNamespace("MAPI")
    'Set folder to check the INBOX
    Set oFolder = oNS.GetDefaultFolder(olFolderInbox)
    strControl = 0
    


    For Each oMsg In oFolder.Items


        With oMsg
            'Check for new mail (unread=true)


            If .UnRead Then
                'This could use the .Subject as well to
                '     search for text in the subject line.


                If InStr(1, .Body, "Body Text to look for") > 0 Then
                    oMsg.Attachments.Item(1).SaveAsFile "Your Drive:\Your Path\" _
                    & oMsg.Attachments.Item(1).DisplayName
                    'Set mailItem to unread
                    .UnRead = False
                    Exit Sub
                End If
            End If
        End With
    Next
End Sub



or you can Check the attached file.

[Edited by TJ_01 on 04-10-2005 at 02:43 AM GMT]

[Edited by TJ_01 on 04-10-2005 at 02:51 AM GMT]

____________________________
Im JAMES  


____________________________
Attached:
RetriveEmailDetails.zip 6 KB (Downloads: 1)

04-10-2005 at 02:39 AM
View Profile Send Email to User Show All Posts | Quote Reply
pcvillaluz
Level: Graduate

Registered: 04-10-2005
Posts: 9
icon Re: HELP!!!!

it now works, but it gets all the attachments from my inbox.  i just need to get it a partciluar sender how do i do that?

04-10-2005 at 03:01 AM
View Profile Send Email to User Show All Posts | Quote Reply
admin
Level: Administrator


Registered: 04-04-2002
Posts: 530
icon Re: HELP!!!!

the msgbox should be placed inside the IF...END IF

For Each oMailItem In oFolder.Items
With oMailItem
If oMailItem.Attachments.Count > 0 Then
  oMailItem.Attachments.Item(1).SaveAsFile "C:\Temp\OutlookAttachments\" & _
  oMailItem.Attachments.Item(1).FileName
  MsgBox oMailItem.Attachments.Item(1).DisplayName & " was saved as " & _
  oMailItem.Attachments.Item(1).FileName
End If
End With
Next oMailItem


____________________________
AndreaVB

04-10-2005 at 03:26 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : HELP!!!!
Previous Topic (destroying instances of Excel)Next Topic (FileDialog Property doesn\'t work) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder