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
|
TJ_01 Level: VB Lord Registered: 24-08-2005 Posts: 320
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
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
|
TJ_01 Level: VB Lord Registered: 24-08-2005 Posts: 320
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?
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
|
TJ_01 Level: VB Lord Registered: 24-08-2005 Posts: 320
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
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