Move email to directory folder, and then move email to another fol

  • Thread starter Thread starter margarita25
  • Start date Start date
M

margarita25

Hello everyone,
I'm pretty new to VBA, sorry. But I need your help please. I'm moving
emails from a certain outlook folder "Incoming" to a directory folder
C:\\folder name. Then once the emails have moved, I need to move the emails
from "Incoming" to "Loaded" but only the ones that have moved to the
directory folder.

I have a script (see below) that will move the emails from the Incoming
folder to the directory just fine, but the second step, to then move those
emails to another outlook folder is not working. It doesn't error, its just
nothing. I'm sure I have the syntax incorrect, but just don't know how to
make it work.

Sub Export_CD_Requ(MyMail As MailItem)
'Exports the email into a folder as a txt file to import delimited line into
dB
Dim Message As Outlook.MailItem
Dim myFolder
Dim myDstFolder

Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox).Folders("Incoming")
'Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
strname = Format$(Now, "yyyymmdd_hhnnssmm")

For Each Message In myInbox.Items
Message.SaveAs "C:\MailMessages\" & strname & "_" & Message.Subject &
".txt", olTXT
Next Message

Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox).Folders("Incoming")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox).Folders("Loaded")
myFolder.MoveTo myDstFolder
End Sub

Any idea's anyone?

True Regards
Margarita
 
Your move code isn't inside a loop, you are never instantiating myDstFolder
and you are trying to move the entire Loaded folder to a folder object that
isn't instantiated.

For moving items from a collection you should never use For...Each, it will
only move every other item since as it moves items it messes with the total
collection count. Use a count down For loop for that.

Also, why is this sub being passed a MailItem as an argument? There is no
use at all of that item in the sub.

Something like this should do it:

Sub Export_CD_Requ()
'Exports the email into a folder as a txt file to import delimited line into
dB
Dim Message As Outlook.MailItem
Dim Dummy As Outlook.MailItem
Dim myFolder As Outlook.MAPIFolder
Dim myDstFolder As Outlook.MAPIFolder
Dim myNameSpace As Outlook.NameSpace
Dim strname As String
Dim i As Long

Set myNameSpace = Application.GetNamespace("MAPI")

Set myInbox =
myNameSpace.GetDefaultFolder(olFolderInbox).Folders("Incoming")
Set myDstFolder =
myNameSpace.GetDefaultFolder(olFolderInbox).Folders("Loaded")

strname = Format$(Now, "yyyymmdd_hhnnssmm")

For i = myInbox.Items.Count To 1 Step -1
Set Message = myInbox.Items,Item(i)
Message.SaveAs "C:\MailMessages\" & strname & "_" & _
Message.Subject & ".txt", olTXT

Set Dummy = Message.Move(myDstFolder)
Next i

End Sub
 
PERFECT!
Thank you! Thank you for using your brain powers for good!!!

FYI, for any newbies like me should you want to copy the script from here.
On the line:

Set Message = myInbox.Items,Item(i)

There is a comma between .Items and Item(i) instead of a period. Once I
changed to a period . all went perfectly fabulous!

Thanks once again Ken!

Cheers!

--
Margarita



Ken Slovak - said:
Your move code isn't inside a loop, you are never instantiating myDstFolder
and you are trying to move the entire Loaded folder to a folder object that
isn't instantiated.

For moving items from a collection you should never use For...Each, it will
only move every other item since as it moves items it messes with the total
collection count. Use a count down For loop for that.

Also, why is this sub being passed a MailItem as an argument? There is no
use at all of that item in the sub.

Something like this should do it:

Sub Export_CD_Requ()
'Exports the email into a folder as a txt file to import delimited line into
dB
Dim Message As Outlook.MailItem
Dim Dummy As Outlook.MailItem
Dim myFolder As Outlook.MAPIFolder
Dim myDstFolder As Outlook.MAPIFolder
Dim myNameSpace As Outlook.NameSpace
Dim strname As String
Dim i As Long

Set myNameSpace = Application.GetNamespace("MAPI")

Set myInbox =
myNameSpace.GetDefaultFolder(olFolderInbox).Folders("Incoming")
Set myDstFolder =
myNameSpace.GetDefaultFolder(olFolderInbox).Folders("Loaded")

strname = Format$(Now, "yyyymmdd_hhnnssmm")

For i = myInbox.Items.Count To 1 Step -1
Set Message = myInbox.Items,Item(i)
Message.SaveAs "C:\MailMessages\" & strname & "_" & _
Message.Subject & ".txt", olTXT

Set Dummy = Message.Move(myDstFolder)
Next i

End Sub
 
Back
Top