ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Saving outlook attachments with VBA code (https://www.excelbanter.com/excel-programming/432043-saving-outlook-attachments-vba-code.html)

ewan7279

Saving outlook attachments with VBA code
 
Hi all,

I have my own outlook inbox and also a generic inbox I share with other
shift members. I have been able to save attachments from emails to my own
inbox using the code found at
http://www.dicks-clicks.com/excel/ol...ve_Attachments , but am
unable to do the same with my generic inbox - I get a message saying the
folder does not exist.

Does anyone know if/how this code can be adjusted to work on a generic
mailbox?

Thanks in advance...


Jacob Skaria

Saving outlook attachments with VBA code
 
In the code the path mentioned is. Have you changed this and if so check
whether the path is a valid path and ends with "\"

MyPath = "C:\My Documents\Completed Survey\"

If this post helps click Yes
---------------
Jacob Skaria


"ewan7279" wrote:

Hi all,

I have my own outlook inbox and also a generic inbox I share with other
shift members. I have been able to save attachments from emails to my own
inbox using the code found at
http://www.dicks-clicks.com/excel/ol...ve_Attachments , but am
unable to do the same with my generic inbox - I get a message saying the
folder does not exist.

Does anyone know if/how this code can be adjusted to work on a generic
mailbox?

Thanks in advance...


ewan7279

Saving outlook attachments with VBA code
 
Sorry,

The problem is the inbox I am trying to save the attached files from.
Currently, I am able to save attachments from emails to my personal inbox,
but not from emails to the generic inbox (where the emails I want to save
attachments from actually arrive into). I currently have: Set Fldr =
olNs.GetDefaultFolder(olFolderInbox), but want to know if the default folder
can be set to a different, generic Mailbox inbox...

The code also fails every time it gets to the end of the loop, so I've had
to add a stupid errorhandler to stop the macro crashing each time. I think
it's something to do with the loop count, but don't know what... any help on
this would also be appreciated.

My actual code is:

Private Sub Workbook_Open()
Application.ScreenUpdating = False

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

On Error GoTo ERRORHANDLER

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox) 'would like to change
this...
Set MoveToFldr = Fldr.Folders("2. STPM")
MyPath = "c:\WD forecasts\"

For i = Fldr.Items.Count To 1 Step -1
Set olMi = Fldr.Items(i)
If InStr(1, olMi.Subject, "Today's Trades") 0 Then
For Each olAtt In olMi.Attachments
olAtt.SaveAsFile MyPath & olAtt.Filename
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

closebook = MsgBox("Process Complete. Do you want to close this workbook?",
vbYesNo)
If closebook = vbYes Then
ActiveWorkbook.Close savechanges = False
End If
ERRORHANDLER:
closebook = MsgBox("Process complete (count failed after event). Do you want
to close this workbook?", vbYesNo)
If closebook = vbYes Then
ActiveWorkbook.Close savechanges = False
End If
Exit Sub

End Sub

"Jacob Skaria" wrote:

In the code the path mentioned is. Have you changed this and if so check
whether the path is a valid path and ends with "\"

MyPath = "C:\My Documents\Completed Survey\"

If this post helps click Yes
---------------
Jacob Skaria


"ewan7279" wrote:

Hi all,

I have my own outlook inbox and also a generic inbox I share with other
shift members. I have been able to save attachments from emails to my own
inbox using the code found at
http://www.dicks-clicks.com/excel/ol...ve_Attachments , but am
unable to do the same with my generic inbox - I get a message saying the
folder does not exist.

Does anyone know if/how this code can be adjusted to work on a generic
mailbox?

Thanks in advance...


Jacob Skaria

Saving outlook attachments with VBA code
 
Is this generic ID setup in outlook or are you accessing this generic ID
using OWA.

(ToolsEmail accounts)

If this post helps click Yes
---------------
Jacob Skaria


"ewan7279" wrote:

Sorry,

The problem is the inbox I am trying to save the attached files from.
Currently, I am able to save attachments from emails to my personal inbox,
but not from emails to the generic inbox (where the emails I want to save
attachments from actually arrive into). I currently have: Set Fldr =
olNs.GetDefaultFolder(olFolderInbox), but want to know if the default folder
can be set to a different, generic Mailbox inbox...

The code also fails every time it gets to the end of the loop, so I've had
to add a stupid errorhandler to stop the macro crashing each time. I think
it's something to do with the loop count, but don't know what... any help on
this would also be appreciated.

My actual code is:

Private Sub Workbook_Open()
Application.ScreenUpdating = False

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

On Error GoTo ERRORHANDLER

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox) 'would like to change
this...
Set MoveToFldr = Fldr.Folders("2. STPM")
MyPath = "c:\WD forecasts\"

For i = Fldr.Items.Count To 1 Step -1
Set olMi = Fldr.Items(i)
If InStr(1, olMi.Subject, "Today's Trades") 0 Then
For Each olAtt In olMi.Attachments
olAtt.SaveAsFile MyPath & olAtt.Filename
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

closebook = MsgBox("Process Complete. Do you want to close this workbook?",
vbYesNo)
If closebook = vbYes Then
ActiveWorkbook.Close savechanges = False
End If
ERRORHANDLER:
closebook = MsgBox("Process complete (count failed after event). Do you want
to close this workbook?", vbYesNo)
If closebook = vbYes Then
ActiveWorkbook.Close savechanges = False
End If
Exit Sub

End Sub

"Jacob Skaria" wrote:

In the code the path mentioned is. Have you changed this and if so check
whether the path is a valid path and ends with "\"

MyPath = "C:\My Documents\Completed Survey\"

If this post helps click Yes
---------------
Jacob Skaria


"ewan7279" wrote:

Hi all,

I have my own outlook inbox and also a generic inbox I share with other
shift members. I have been able to save attachments from emails to my own
inbox using the code found at
http://www.dicks-clicks.com/excel/ol...ve_Attachments , but am
unable to do the same with my generic inbox - I get a message saying the
folder does not exist.

Does anyone know if/how this code can be adjusted to work on a generic
mailbox?

Thanks in advance...


ewan7279

Saving outlook attachments with VBA code
 
Sorry, not sure I understand the question...

I don't think it's an ID, it's just a mailbox in Outlook that only 5 people
have access to, in addition to our personal inboxes. It is in the same
format (contains the same folders etc) as my personal inbox, but I cannot add
anything to the calendar, all emails I send appear in my personal sent items,
and any emails I delete appear in my deleted items folder rather than the one
in the generic mailbox. I have the choice of sending emails from my own
email address or the generic mailbox address.

"Jacob Skaria" wrote:

Is this generic ID setup in outlook or are you accessing this generic ID
using OWA.

(ToolsEmail accounts)

If this post helps click Yes
---------------
Jacob Skaria


"ewan7279" wrote:

Sorry,

The problem is the inbox I am trying to save the attached files from.
Currently, I am able to save attachments from emails to my personal inbox,
but not from emails to the generic inbox (where the emails I want to save
attachments from actually arrive into). I currently have: Set Fldr =
olNs.GetDefaultFolder(olFolderInbox), but want to know if the default folder
can be set to a different, generic Mailbox inbox...

The code also fails every time it gets to the end of the loop, so I've had
to add a stupid errorhandler to stop the macro crashing each time. I think
it's something to do with the loop count, but don't know what... any help on
this would also be appreciated.

My actual code is:

Private Sub Workbook_Open()
Application.ScreenUpdating = False

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

On Error GoTo ERRORHANDLER

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox) 'would like to change
this...
Set MoveToFldr = Fldr.Folders("2. STPM")
MyPath = "c:\WD forecasts\"

For i = Fldr.Items.Count To 1 Step -1
Set olMi = Fldr.Items(i)
If InStr(1, olMi.Subject, "Today's Trades") 0 Then
For Each olAtt In olMi.Attachments
olAtt.SaveAsFile MyPath & olAtt.Filename
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

closebook = MsgBox("Process Complete. Do you want to close this workbook?",
vbYesNo)
If closebook = vbYes Then
ActiveWorkbook.Close savechanges = False
End If
ERRORHANDLER:
closebook = MsgBox("Process complete (count failed after event). Do you want
to close this workbook?", vbYesNo)
If closebook = vbYes Then
ActiveWorkbook.Close savechanges = False
End If
Exit Sub

End Sub

"Jacob Skaria" wrote:

In the code the path mentioned is. Have you changed this and if so check
whether the path is a valid path and ends with "\"

MyPath = "C:\My Documents\Completed Survey\"

If this post helps click Yes
---------------
Jacob Skaria


"ewan7279" wrote:

Hi all,

I have my own outlook inbox and also a generic inbox I share with other
shift members. I have been able to save attachments from emails to my own
inbox using the code found at
http://www.dicks-clicks.com/excel/ol...ve_Attachments , but am
unable to do the same with my generic inbox - I get a message saying the
folder does not exist.

Does anyone know if/how this code can be adjusted to work on a generic
mailbox?

Thanks in advance...


Steve Yandl[_3_]

Saving outlook attachments with VBA code
 
I'm assuming that the generic inbox is a sibling folder to the default
inbox.

Try something like this to get at that other inbox, assuming the generic
inbox is named "Staff Inbox" in my example:
'------------------------------------
Set objInbox = olNs.GetDefaultFolder(olFolderInbox)
strFolderName = objInbox.Parent
Set objMailbox = olNS.Folders(strFolderName)
Set objStaffInbox = objMailbox.Folders("Staff Inbox")
'------------------------------------

Steve Yandl


"ewan7279" wrote in message
...
Hi all,

I have my own outlook inbox and also a generic inbox I share with other
shift members. I have been able to save attachments from emails to my own
inbox using the code found at
http://www.dicks-clicks.com/excel/ol...ve_Attachments , but
am
unable to do the same with my generic inbox - I get a message saying the
folder does not exist.

Does anyone know if/how this code can be adjusted to work on a generic
mailbox?

Thanks in advance...




All times are GMT +1. The time now is 03:15 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com