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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook Code to save attachmnet to a folder

Status
Not open for further replies.

romnew

Programmer
Feb 27, 2002
44
ZA
Morning all,
I have tried to set up code from other code obtained from the net. to save an xls attachment to a designated folder from a command button. my intention is to read all unread emails and save the attachments
My code boms out with error message Object required at line
For Each objAtt In itemAttachments
I am totally lost. Please help!
Here is the code:
Dim objOL As Outlook.Application
Dim itemAttachments As Outlook.Attachments

Dim strFile As String
Dim strFolder As String, item As Object
Dim strprogExt As String, arrext() As String, intpos As Integer
Dim objAtt As Attachment, i As Integer, strExt As String


' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")

Set objAtt = itemAttachments


' Get the Temp folder.
strFolder = GetTempDir()
If strFolder = "" Then
MsgBox "Could not get Temp folder", vbOKOnly
GoTo ExitSub
End If
strprogExt = "xls,exe"

' Check each selected item for attachments.
' If attachments exist, save them to the Temp
' folder and strip them from the item.

Set objAtt = itemAttachments
arrext = Split(strprogExt, ",")

For Each objAtt In itemAttachments
Stop
intpos = InStrRev(objAtt.FileName, ".")
If intpos > 0 Then
'check attachment ext
strExt = LCase(Mid(objAtt.FileName, intpos + 1))
For i = LBound(arrext) To LBound(arrext)
If strExt = Trim(arrext(i)) Then
Stop
item.Move strFolder
Exit For
End If
Next
Else
item.Move strFolder
End If
Next




ExitSub:
Set itemAttachments = Nothing
Set objOL = Nothing
End Sub
 
You need to get the Inbox folder as an object. You have neither declared nor set an object to be an Outlook folder. This also requires an declaration and set of a MAPISpace object. The bottom line is ItemsAttachments is not a valid object because your code has no idea what items you are talking about.

Gerry
 
Thanks fumei I will try and sort it out but am having difficultyas it is. Will let you know.
Cheers
 
You are not that far off. You just need to get the object up and running. Here is a start. it is going to look something like this:

Code:
Dim olApp As outlook.Application
Dim objNameSpace As NameSpace
Dim objInbox As MAPIFolder
Dim objMail As MailItem
Dim objAttach As Attachment

Set olApp = CreateObject("Outlook.Application")
Set objNameSpace = olApp.GetNamespace("MAPI")
Set objInbox = objNameSpace.GetDefaultFolder(olFolderInbox)
For Each objMail In objInbox.Items()
  If objMail.UnRead = True Then
    Set objAttach = objMail.Attachments
      objItemAttachments.SaveAsFile "C:\My Documents\" & _
    objAttach.Item(1).DisplayName
End If
Set objInbox = Nothing
Set objnameSpace = Nothing
Set olApp = Nothing

Obviously if you are testing for particular tpesof attachments then you will have to test for that, as you do in your starting code.

Gerry
 
Thanks again Gerry.
I value your attention.
Piet
 
Hi Gerry,
I have adapted your code slightly(you will notice where) but it comes uo with an error type mismatch at the line, marked****.
Any ideas why?
Thanks again for helping
Piet

The code:
Dim olApp As Outlook.Application
Dim objNameSpace As NameSpace
Dim objInbox As MAPIFolder
Dim objMail As MailItem
Dim objAttach As Attachment

Set olApp = CreateObject("Outlook.Application")
Set objNameSpace = olApp.GetNamespace("MAPI")
Set objInbox = objNameSpace.GetDefaultFolder(olFolderInbox)
For Each objMail In objInbox.Items()
If objMail.UnRead = True Then
Set objAttach = objMail.Attachments ****

objAttach.SaveAsFile "C:\Rossair3\" & _
objAttach.item(1).DisplayName

End If
Next
Set objInbox = Nothing
Set objNameSpace = Nothing
Set olApp = Nothing
 
Hi Gerry,
I came across some other code whick I dont understand.
It says it requires Input for the sSavaToPath, but how?
Here is the first line:
Function OutlookSaveAttachments(sSaveToPath As String) As String

I havent a clue
Tried to say sSaveTopath = C;\folder\ but it boms out.
Can you help please
Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top