ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Dick K - Move e-mail to different folder (https://www.excelbanter.com/excel-programming/311574-dick-k-move-e-mail-different-folder.html)

Steph[_3_]

Dick K - Move e-mail to different folder
 
Hi Dick (or anyone else who may be able to help!),

I have some code below that copies the excel attachment from every e-mail in
an Outlook folder and saves it to a specified network drive. That part has
been working great. But then I tried to move the e-mail to a different
folder after it copies the attachment. In the Outlook folder, I currently
have ben testing with 4 files.....the code moves 2 of them to the different
outlook folder, while the other 2 stay. Any idea what I'm doing wrong?
Thanks!

Sub Move()
'Saves attachments to a specified folder

Dim ol As Outlook.Application
Dim ns As Namespace
Dim Fldr As MAPIFolder
Dim MoveToFldr As MAPIFolder
Dim Mi As MailItem
Dim Att As Attachment

Set ol = New Outlook.Application
Set ns = ol.GetNamespace("MAPI")
Set Fldr = ns.Folders("Public Folders").Folders("All Public
Folders").Folders("Timesheet")
Set MoveToFldr = ns.Folders("Public Folders").Folders("All Public
Folders").Folders("Public Folders")

For Each Mi In Fldr.Items
If Mi.Attachments.Count 0 Then
For Each Att In Mi.Attachments
iFile = iFile + 1

'Att.SaveAsFile "H:\Timesheet Data\" & Att.Filename &
CStr(iFile)
'Att.SaveAsFile "H:\Timesheet Data\ts" & CStr(iFile) & ".xls"

Att.SaveAsFile "H:\Timesheet Data\" & Format(Mi.ReceivedTime,
"yyyymmddhhmmss") & "-" & CStr(iFile) & ".xls"

Next Att
Mi.Move MoveToFldr

End If
Next Mi

Set Att = Nothing
Set Mi = Nothing
Set Fldr = Nothing
Set ns = Nothing
Set ol = Nothing
Set MoveToFldr = Nothing

End Sub



Dick Kusleika[_3_]

Dick K - Move e-mail to different folder
 
Steph

You need to save the item before you move it

Mi.Save

When you save an attachment, the mailitem becomes dirty and it won't move.

--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com

"Steph" wrote in message
...
Hi Dick (or anyone else who may be able to help!),

I have some code below that copies the excel attachment from every e-mail

in
an Outlook folder and saves it to a specified network drive. That part

has
been working great. But then I tried to move the e-mail to a different
folder after it copies the attachment. In the Outlook folder, I currently
have ben testing with 4 files.....the code moves 2 of them to the

different
outlook folder, while the other 2 stay. Any idea what I'm doing wrong?
Thanks!

Sub Move()
'Saves attachments to a specified folder

Dim ol As Outlook.Application
Dim ns As Namespace
Dim Fldr As MAPIFolder
Dim MoveToFldr As MAPIFolder
Dim Mi As MailItem
Dim Att As Attachment

Set ol = New Outlook.Application
Set ns = ol.GetNamespace("MAPI")
Set Fldr = ns.Folders("Public Folders").Folders("All Public
Folders").Folders("Timesheet")
Set MoveToFldr = ns.Folders("Public Folders").Folders("All Public
Folders").Folders("Public Folders")

For Each Mi In Fldr.Items
If Mi.Attachments.Count 0 Then
For Each Att In Mi.Attachments
iFile = iFile + 1

'Att.SaveAsFile "H:\Timesheet Data\" & Att.Filename &
CStr(iFile)
'Att.SaveAsFile "H:\Timesheet Data\ts" & CStr(iFile) & ".xls"

Att.SaveAsFile "H:\Timesheet Data\" & Format(Mi.ReceivedTime,
"yyyymmddhhmmss") & "-" & CStr(iFile) & ".xls"

Next Att
Mi.Move MoveToFldr

End If
Next Mi

Set Att = Nothing
Set Mi = Nothing
Set Fldr = Nothing
Set ns = Nothing
Set ol = Nothing
Set MoveToFldr = Nothing

End Sub





Steph[_3_]

Dick K - Move e-mail to different folder
 
Hi Dick,

I added the Mi.Save, and same thing happend. Is it because I am using a
ForEach loop and not a counter? It seems very strange - all 4 e-mails in
the inbox have attachments....I'm totally confused as to why only 2 of them
are moving??

"Dick Kusleika" wrote in message
...
Steph

You need to save the item before you move it

Mi.Save

When you save an attachment, the mailitem becomes dirty and it won't move.

--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com

"Steph" wrote in message
...
Hi Dick (or anyone else who may be able to help!),

I have some code below that copies the excel attachment from every

e-mail
in
an Outlook folder and saves it to a specified network drive. That part

has
been working great. But then I tried to move the e-mail to a different
folder after it copies the attachment. In the Outlook folder, I

currently
have ben testing with 4 files.....the code moves 2 of them to the

different
outlook folder, while the other 2 stay. Any idea what I'm doing wrong?
Thanks!

Sub Move()
'Saves attachments to a specified folder

Dim ol As Outlook.Application
Dim ns As Namespace
Dim Fldr As MAPIFolder
Dim MoveToFldr As MAPIFolder
Dim Mi As MailItem
Dim Att As Attachment

Set ol = New Outlook.Application
Set ns = ol.GetNamespace("MAPI")
Set Fldr = ns.Folders("Public Folders").Folders("All Public
Folders").Folders("Timesheet")
Set MoveToFldr = ns.Folders("Public Folders").Folders("All Public
Folders").Folders("Public Folders")

For Each Mi In Fldr.Items
If Mi.Attachments.Count 0 Then
For Each Att In Mi.Attachments
iFile = iFile + 1

'Att.SaveAsFile "H:\Timesheet Data\" & Att.Filename &
CStr(iFile)
'Att.SaveAsFile "H:\Timesheet Data\ts" & CStr(iFile) &

".xls"

Att.SaveAsFile "H:\Timesheet Data\" &

Format(Mi.ReceivedTime,
"yyyymmddhhmmss") & "-" & CStr(iFile) & ".xls"

Next Att
Mi.Move MoveToFldr

End If
Next Mi

Set Att = Nothing
Set Mi = Nothing
Set Fldr = Nothing
Set ns = Nothing
Set ol = Nothing
Set MoveToFldr = Nothing

End Sub







Steph[_3_]

Dick K - Move e-mail to different folder
 
Hi Dick,

I tried the Counter code from your web site, and it works perfectly! Ignore
my last post. Thanks so much for your help!

"Steph" wrote in message
...
Hi Dick,

I added the Mi.Save, and same thing happend. Is it because I am using a
ForEach loop and not a counter? It seems very strange - all 4 e-mails in
the inbox have attachments....I'm totally confused as to why only 2 of

them
are moving??

"Dick Kusleika" wrote in message
...
Steph

You need to save the item before you move it

Mi.Save

When you save an attachment, the mailitem becomes dirty and it won't

move.

--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com

"Steph" wrote in message
...
Hi Dick (or anyone else who may be able to help!),

I have some code below that copies the excel attachment from every

e-mail
in
an Outlook folder and saves it to a specified network drive. That

part
has
been working great. But then I tried to move the e-mail to a

different
folder after it copies the attachment. In the Outlook folder, I

currently
have ben testing with 4 files.....the code moves 2 of them to the

different
outlook folder, while the other 2 stay. Any idea what I'm doing

wrong?
Thanks!

Sub Move()
'Saves attachments to a specified folder

Dim ol As Outlook.Application
Dim ns As Namespace
Dim Fldr As MAPIFolder
Dim MoveToFldr As MAPIFolder
Dim Mi As MailItem
Dim Att As Attachment

Set ol = New Outlook.Application
Set ns = ol.GetNamespace("MAPI")
Set Fldr = ns.Folders("Public Folders").Folders("All Public
Folders").Folders("Timesheet")
Set MoveToFldr = ns.Folders("Public Folders").Folders("All Public
Folders").Folders("Public Folders")

For Each Mi In Fldr.Items
If Mi.Attachments.Count 0 Then
For Each Att In Mi.Attachments
iFile = iFile + 1

'Att.SaveAsFile "H:\Timesheet Data\" & Att.Filename &
CStr(iFile)
'Att.SaveAsFile "H:\Timesheet Data\ts" & CStr(iFile) &

".xls"

Att.SaveAsFile "H:\Timesheet Data\" &

Format(Mi.ReceivedTime,
"yyyymmddhhmmss") & "-" & CStr(iFile) & ".xls"

Next Att
Mi.Move MoveToFldr

End If
Next Mi

Set Att = Nothing
Set Mi = Nothing
Set Fldr = Nothing
Set ns = Nothing
Set ol = Nothing
Set MoveToFldr = Nothing

End Sub









Dick Kusleika[_3_]

Dick K - Move e-mail to different folder
 
Steph

Oh yeah, that's obvious now! You can't iterate through a collection
forwards if you want to move, delete, etc. because it screws up the order of
the collection. I'll bet it was moving every other one.

Mail1 <-- It processes this one first
Mail2
Mail3
Mail4

Mail2
Mail3 <--Now it goes to the "second" email which since Mail1 was moved is
now Mail3
Mail4

Mail2
Mail4
<----Now it goes to the "third" email which is non existent.

Here's a similar problem if you're interested
http://www.dicks-blog.com/excel/2004...o_delete_.html

--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com



"Steph" wrote in message
...
Hi Dick,

I tried the Counter code from your web site, and it works perfectly!

Ignore
my last post. Thanks so much for your help!

"Steph" wrote in message
...
Hi Dick,

I added the Mi.Save, and same thing happend. Is it because I am using a
ForEach loop and not a counter? It seems very strange - all 4 e-mails

in
the inbox have attachments....I'm totally confused as to why only 2 of

them
are moving??

"Dick Kusleika" wrote in

message
...
Steph

You need to save the item before you move it

Mi.Save

When you save an attachment, the mailitem becomes dirty and it won't

move.

--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com

"Steph" wrote in message
...
Hi Dick (or anyone else who may be able to help!),

I have some code below that copies the excel attachment from every

e-mail
in
an Outlook folder and saves it to a specified network drive. That

part
has
been working great. But then I tried to move the e-mail to a

different
folder after it copies the attachment. In the Outlook folder, I

currently
have ben testing with 4 files.....the code moves 2 of them to the
different
outlook folder, while the other 2 stay. Any idea what I'm doing

wrong?
Thanks!

Sub Move()
'Saves attachments to a specified folder

Dim ol As Outlook.Application
Dim ns As Namespace
Dim Fldr As MAPIFolder
Dim MoveToFldr As MAPIFolder
Dim Mi As MailItem
Dim Att As Attachment

Set ol = New Outlook.Application
Set ns = ol.GetNamespace("MAPI")
Set Fldr = ns.Folders("Public Folders").Folders("All Public
Folders").Folders("Timesheet")
Set MoveToFldr = ns.Folders("Public Folders").Folders("All Public
Folders").Folders("Public Folders")

For Each Mi In Fldr.Items
If Mi.Attachments.Count 0 Then
For Each Att In Mi.Attachments
iFile = iFile + 1

'Att.SaveAsFile "H:\Timesheet Data\" & Att.Filename &
CStr(iFile)
'Att.SaveAsFile "H:\Timesheet Data\ts" & CStr(iFile) &

".xls"

Att.SaveAsFile "H:\Timesheet Data\" &

Format(Mi.ReceivedTime,
"yyyymmddhhmmss") & "-" & CStr(iFile) & ".xls"

Next Att
Mi.Move MoveToFldr

End If
Next Mi

Set Att = Nothing
Set Mi = Nothing
Set Fldr = Nothing
Set ns = Nothing
Set ol = Nothing
Set MoveToFldr = Nothing

End Sub











Steph[_3_]

Dick K - Move e-mail to different folder
 
Good to know. It was bugging me a bit as to why it was doing that! Now I
know. Thanks again Dick!

"Dick Kusleika" wrote in message
...
Steph

Oh yeah, that's obvious now! You can't iterate through a collection
forwards if you want to move, delete, etc. because it screws up the order

of
the collection. I'll bet it was moving every other one.

Mail1 <-- It processes this one first
Mail2
Mail3
Mail4

Mail2
Mail3 <--Now it goes to the "second" email which since Mail1 was moved is
now Mail3
Mail4

Mail2
Mail4
<----Now it goes to the "third" email which is non existent.

Here's a similar problem if you're interested
http://www.dicks-blog.com/excel/2004...o_delete_.html

--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com



"Steph" wrote in message
...
Hi Dick,

I tried the Counter code from your web site, and it works perfectly!

Ignore
my last post. Thanks so much for your help!

"Steph" wrote in message
...
Hi Dick,

I added the Mi.Save, and same thing happend. Is it because I am using

a
ForEach loop and not a counter? It seems very strange - all 4 e-mails

in
the inbox have attachments....I'm totally confused as to why only 2 of

them
are moving??

"Dick Kusleika" wrote in

message
...
Steph

You need to save the item before you move it

Mi.Save

When you save an attachment, the mailitem becomes dirty and it won't

move.

--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com

"Steph" wrote in message
...
Hi Dick (or anyone else who may be able to help!),

I have some code below that copies the excel attachment from every
e-mail
in
an Outlook folder and saves it to a specified network drive. That

part
has
been working great. But then I tried to move the e-mail to a

different
folder after it copies the attachment. In the Outlook folder, I
currently
have ben testing with 4 files.....the code moves 2 of them to the
different
outlook folder, while the other 2 stay. Any idea what I'm doing

wrong?
Thanks!

Sub Move()
'Saves attachments to a specified folder

Dim ol As Outlook.Application
Dim ns As Namespace
Dim Fldr As MAPIFolder
Dim MoveToFldr As MAPIFolder
Dim Mi As MailItem
Dim Att As Attachment

Set ol = New Outlook.Application
Set ns = ol.GetNamespace("MAPI")
Set Fldr = ns.Folders("Public Folders").Folders("All Public
Folders").Folders("Timesheet")
Set MoveToFldr = ns.Folders("Public Folders").Folders("All Public
Folders").Folders("Public Folders")

For Each Mi In Fldr.Items
If Mi.Attachments.Count 0 Then
For Each Att In Mi.Attachments
iFile = iFile + 1

'Att.SaveAsFile "H:\Timesheet Data\" & Att.Filename &
CStr(iFile)
'Att.SaveAsFile "H:\Timesheet Data\ts" & CStr(iFile) &
".xls"

Att.SaveAsFile "H:\Timesheet Data\" &
Format(Mi.ReceivedTime,
"yyyymmddhhmmss") & "-" & CStr(iFile) & ".xls"

Next Att
Mi.Move MoveToFldr

End If
Next Mi

Set Att = Nothing
Set Mi = Nothing
Set Fldr = Nothing
Set ns = Nothing
Set ol = Nothing
Set MoveToFldr = Nothing

End Sub














All times are GMT +1. The time now is 07:30 PM.

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