Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 191
Default Saving attachments from Outlook

Hi there,

I am trying to write a macro that will search my outlook inbox for a
specific subject heading then save the attatchment to a specific folder.

I have picked up this code with help from this site but my VBA skills are
extremely limited and I can't get it to work. When I run it It stops at the
first line and gives this error message:

Compile error:

User-defined type not defined

Code:

Sub SaveAttachments()

Set myOLApp = CreateObject("Outlook.Application")

Dim olApp As Outlook.Application
Dim olNs As NameSpace
Dim Fldr As MAPIFolder
Dim MoveToFldr As MAPIFolder
Dim olMi As MailItem
Dim olAtt As Attachment
Dim MyPath As String
Dim i As Long

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set MoveToFldr = Fldr.Folders("eisreq")
MyPath = "I:\EIS\Forms\"

For i = Fldr.Items.Count To 1 Step -1
Set olMi = Fldr.Items(i)
If InStr(1, olMi.Subject, "EIS") 0 Then
For Each olAtt In olMi.Attachments
If olAtt.Filename = "EIS Request.xls" Then
olAtt.SaveAsFile MyPath & olMi.SenderName & ".xls"
End If
Next olAtt
olMi.Save
olMi.Move MoveToFldr
End If
Next i

Set olAtt = Nothing
Set olMi = Nothing
Set Fldr = Nothing
Set MoveToFldr = Nothing
Set olNs = Nothing
Set olApp = Nothing

End Sub


Can anyone tell me what I'm doing wrong?

Any help would be greatfully appreciated.

Thanks in advance

Jamie
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Saving attachments from Outlook

Hi Jamie

You must set a reference (VBE - Tools -
References) to the Outlook Object Library.


--
Regards Ron de Bruin
http://www.rondebruin.nl



"Jamie" wrote in message ...
Hi there,

I am trying to write a macro that will search my outlook inbox for a
specific subject heading then save the attatchment to a specific folder.

I have picked up this code with help from this site but my VBA skills are
extremely limited and I can't get it to work. When I run it It stops at the
first line and gives this error message:

Compile error:

User-defined type not defined

Code:

Sub SaveAttachments()

Set myOLApp = CreateObject("Outlook.Application")

Dim olApp As Outlook.Application
Dim olNs As NameSpace
Dim Fldr As MAPIFolder
Dim MoveToFldr As MAPIFolder
Dim olMi As MailItem
Dim olAtt As Attachment
Dim MyPath As String
Dim i As Long

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set MoveToFldr = Fldr.Folders("eisreq")
MyPath = "I:\EIS\Forms\"

For i = Fldr.Items.Count To 1 Step -1
Set olMi = Fldr.Items(i)
If InStr(1, olMi.Subject, "EIS") 0 Then
For Each olAtt In olMi.Attachments
If olAtt.Filename = "EIS Request.xls" Then
olAtt.SaveAsFile MyPath & olMi.SenderName & ".xls"
End If
Next olAtt
olMi.Save
olMi.Move MoveToFldr
End If
Next i

Set olAtt = Nothing
Set olMi = Nothing
Set Fldr = Nothing
Set MoveToFldr = Nothing
Set olNs = Nothing
Set olApp = Nothing

End Sub


Can anyone tell me what I'm doing wrong?

Any help would be greatfully appreciated.

Thanks in advance

Jamie



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,120
Default Saving attachments from Outlook

Set a reference to the Microsoft Outlook type library in the VBE
(ToolsReferences)

--
HTH

Bob Phillips

"Jamie" wrote in message
...
Hi there,

I am trying to write a macro that will search my outlook inbox for a
specific subject heading then save the attatchment to a specific folder.

I have picked up this code with help from this site but my VBA skills are
extremely limited and I can't get it to work. When I run it It stops at

the
first line and gives this error message:

Compile error:

User-defined type not defined

Code:

Sub SaveAttachments()

Set myOLApp = CreateObject("Outlook.Application")

Dim olApp As Outlook.Application
Dim olNs As NameSpace
Dim Fldr As MAPIFolder
Dim MoveToFldr As MAPIFolder
Dim olMi As MailItem
Dim olAtt As Attachment
Dim MyPath As String
Dim i As Long

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set MoveToFldr = Fldr.Folders("eisreq")
MyPath = "I:\EIS\Forms\"

For i = Fldr.Items.Count To 1 Step -1
Set olMi = Fldr.Items(i)
If InStr(1, olMi.Subject, "EIS") 0 Then
For Each olAtt In olMi.Attachments
If olAtt.Filename = "EIS Request.xls" Then
olAtt.SaveAsFile MyPath & olMi.SenderName & ".xls"
End If
Next olAtt
olMi.Save
olMi.Move MoveToFldr
End If
Next i

Set olAtt = Nothing
Set olMi = Nothing
Set Fldr = Nothing
Set MoveToFldr = Nothing
Set olNs = Nothing
Set olApp = Nothing

End Sub


Can anyone tell me what I'm doing wrong?

Any help would be greatfully appreciated.

Thanks in advance

Jamie



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,120
Default Saving attachments from Outlook

Oh, and remove the line


Set myOLApp = CreateObject("Outlook.Application")

--
HTH

Bob Phillips

"Jamie" wrote in message
...
Hi there,

I am trying to write a macro that will search my outlook inbox for a
specific subject heading then save the attatchment to a specific folder.

I have picked up this code with help from this site but my VBA skills are
extremely limited and I can't get it to work. When I run it It stops at

the
first line and gives this error message:

Compile error:

User-defined type not defined

Code:

Sub SaveAttachments()

Set myOLApp = CreateObject("Outlook.Application")

Dim olApp As Outlook.Application
Dim olNs As NameSpace
Dim Fldr As MAPIFolder
Dim MoveToFldr As MAPIFolder
Dim olMi As MailItem
Dim olAtt As Attachment
Dim MyPath As String
Dim i As Long

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set MoveToFldr = Fldr.Folders("eisreq")
MyPath = "I:\EIS\Forms\"

For i = Fldr.Items.Count To 1 Step -1
Set olMi = Fldr.Items(i)
If InStr(1, olMi.Subject, "EIS") 0 Then
For Each olAtt In olMi.Attachments
If olAtt.Filename = "EIS Request.xls" Then
olAtt.SaveAsFile MyPath & olMi.SenderName & ".xls"
End If
Next olAtt
olMi.Save
olMi.Move MoveToFldr
End If
Next i

Set olAtt = Nothing
Set olMi = Nothing
Set Fldr = Nothing
Set MoveToFldr = Nothing
Set olNs = Nothing
Set olApp = Nothing

End Sub


Can anyone tell me what I'm doing wrong?

Any help would be greatfully appreciated.

Thanks in advance

Jamie



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 191
Default Saving attachments from Outlook

Thanks a lot for that. I have one further question though.

At the moment this code saves the attachment as the name of the person who
sent it which is great except when someone sends two. If this happens the
first spreadsheet from that person is overwritten. Is there any way around
this?

"Ron de Bruin" wrote:

Hi Jamie

You must set a reference (VBE - Tools -
References) to the Outlook Object Library.


--
Regards Ron de Bruin
http://www.rondebruin.nl



"Jamie" wrote in message ...
Hi there,

I am trying to write a macro that will search my outlook inbox for a
specific subject heading then save the attatchment to a specific folder.

I have picked up this code with help from this site but my VBA skills are
extremely limited and I can't get it to work. When I run it It stops at the
first line and gives this error message:

Compile error:

User-defined type not defined

Code:

Sub SaveAttachments()

Set myOLApp = CreateObject("Outlook.Application")

Dim olApp As Outlook.Application
Dim olNs As NameSpace
Dim Fldr As MAPIFolder
Dim MoveToFldr As MAPIFolder
Dim olMi As MailItem
Dim olAtt As Attachment
Dim MyPath As String
Dim i As Long

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set MoveToFldr = Fldr.Folders("eisreq")
MyPath = "I:\EIS\Forms\"

For i = Fldr.Items.Count To 1 Step -1
Set olMi = Fldr.Items(i)
If InStr(1, olMi.Subject, "EIS") 0 Then
For Each olAtt In olMi.Attachments
If olAtt.Filename = "EIS Request.xls" Then
olAtt.SaveAsFile MyPath & olMi.SenderName & ".xls"
End If
Next olAtt
olMi.Save
olMi.Move MoveToFldr
End If
Next i

Set olAtt = Nothing
Set olMi = Nothing
Set Fldr = Nothing
Set MoveToFldr = Nothing
Set olNs = Nothing
Set olApp = Nothing

End Sub


Can anyone tell me what I'm doing wrong?

Any help would be greatfully appreciated.

Thanks in advance

Jamie






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Saving attachments from Outlook

You can add the date and/or time after the person's name

olAtt.SaveAsFile MyPath & olMi.SenderName & now() & ".xls

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot open Excel (.xls) attachments in Outlook childofthe1980s Excel Discussion (Misc queries) 1 May 29th 10 12:11 AM
Excel Attachments not opening in Outlook gish Excel Discussion (Misc queries) 1 June 6th 06 09:49 PM
Outlook Webmail and Attachments Jane Excel Discussion (Misc queries) 0 October 12th 05 06:19 PM
unblock .exe attachments in outlook Todd Htutenstine Excel Programming 3 February 3rd 04 11:55 PM
Open Outlook from Excel and send attachments Bob[_49_] Excel Programming 4 January 29th 04 04:38 AM


All times are GMT +1. The time now is 12:27 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"