Answered by:
Change VBA Script to move the mail to another folder. OUTLOOK 2010

Question
-
Hello
this is my problem, I have a script that saves the xml files and the forwarding this email is automatically if there is an xml and pdf file.
I would like in this same script, after forwarding the email, move that email to this email to a folder called "archived" and mark it as read.
I made a code for this but it is not working.the code processes for 1 min in the part where it has the code to move, it does not move the email and finally it passes to the end of the code.
Can someone help me?
Follow the attachmentsPublic Sub ProcessarAnexo(email As MailItem) Dim DiretorioAnexos As String DiretorioAnexos = "C:\Users\gabriel\Documents\xmlepdf\" Dim MailID As String Dim Mail As Outlook.MailItem MailID = email.EntryID Set Mail = Application.Session.GetItemFromID(MailID) flagXML = False flagPDF = False 'SALVAR ANEXO XML 'ENVIAR SE HOUVER PDF E XML For Each anexo In Mail.Attachments If Right(anexo.FileName, 3) = "xml" Or Right(anexo.FileName, 3) = "XML" Then flagXML = True anexo.SaveAsFile DiretorioAnexos & anexo.FileName End If If Right(anexo.FileName, 3) = "pdf" Or Right(anexo.FileName, 3) = "PDF" Then flagPDF = True End If Next 'in case that there are multiple pdf attachemnts in the mail 'use a flag to mark to forward the mails istead of forwarding the mail directly Dim ForWardMail As Outlook.MailItem If (flagXML And flagPDF) Then Set ForWardMail = Mail.Forward With ForWardMail .Recipients.Add "gabriel@b" .Recipients.Add "gabriel@a" .Display 'send with send the mail directly ' .Send End With 'MAIL MOVE Mail.Move (Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("Arquivado")) End If Set Mail = Nothing End Sub
- Edited by Gabriel Alves Ferreira Tuesday, June 12, 2018 11:57 AM
Tuesday, June 12, 2018 11:54 AM
Answers
-
I have to say that at this point I have no idea why you are having problems. On my Outlook 2013 I tested your original code to move a message after creating a subfolder of the Inbox named "Test".
Mail.Move (Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("Test"))
The code ran without a problem and moved the message properly from the Inbox to Test.
I don't know what else I can do to help you.
- Marked as answer by Gabriel Alves Ferreira Tuesday, June 12, 2018 7:48 PM
Tuesday, June 12, 2018 4:41 PM
All replies
-
The "Arquivado" folder is NOT a subfolder of the Inbox. Either make it a subfolder or change the code to properly identify the "Arquivado" folder object.
- Edited by RLWA32 Tuesday, June 12, 2018 12:20 PM
Tuesday, June 12, 2018 12:20 PM -
Sorry I attached the wrong image, I already put the folder in the inbox and it still did not work.
- Edited by Gabriel Alves Ferreira Tuesday, June 12, 2018 12:24 PM
Tuesday, June 12, 2018 12:23 PM -
Does the code work if you try to copy the mail item to a local folder instead of a folder on the IMAP server?Tuesday, June 12, 2018 12:29 PM
-
The email in use is configured on the POP server, so it is already being used local folders right? I tested it on IMAP and the code did not work.Tuesday, June 12, 2018 12:39 PM
-
Try breaking up the Mail.Move statement to retrieve the Namespace, Inbox, and "Arquivado" folder in separate statements before using the destination folder object in the Move method call. Let's see if we can narrow the source of the problem during debugging.Tuesday, June 12, 2018 12:49 PM
-
I adapted the code of this link and it still did not work. When I debugged the code, the code always stops at the command to move the email and does not reach the end of the code.
End With
'after forward the email myNameSpace As Outlook.NameSpace Dim myDestFolder As Outlook.MAPIFolder Dim myItems As Outlook.Items Dim myItem As Object Set myNameSpace = Application.GetNamespace("MAPI") Set Mail = myNameSpace.GetDefaultFolder(olFolderInbox) Set myDestFolder = Mail.Folders("Arquivado") Mail.Move myDestFolder End If
Tuesday, June 12, 2018 1:48 PM -
I adapted the code of this link and it still did not work. When I debugged the code, the code always stops at the command to move the email and does not reach the end of the code.
End With
'after forward the email myNameSpace As Outlook.NameSpace Dim myDestFolder As Outlook.MAPIFolder Dim myItems As Outlook.Items Dim myItem As Object Set myNameSpace = Application.GetNamespace("MAPI") Set Mail = myNameSpace.GetDefaultFolder(olFolderInbox) Set myDestFolder = Mail.Folders("Arquivado") Mail.Move myDestFolder End If
Tuesday, June 12, 2018 2:18 PM -
Dim moveMail As Outlook.MailItem Dim myDestFolder As Outlook.MAPIFolder Dim myItems As Outlook.Items Dim myItem As Object Set myNameSpace = Application.GetNamespace("MAPI") Set moveMail = myNameSpace.GetDefaultFolder(olFolderInbox) Set myDestFolder = moveMail.Folders("Arquivado") moveMail.Move myDestFolder
I changed the variable and the rest of the code, but debugging, it stops in this line and does not finish the code, what is the problem?
problem line
Set moveMail = myNameSpace.GetDefaultFolder(olFolderInbox)
Tuesday, June 12, 2018 2:30 PM -
Dim moveMail As Outlook.MailItem Dim myDestFolder As Outlook.MAPIFolder Dim myItems As Outlook.Items Dim myItem As Object Set myNameSpace = Application.GetNamespace("MAPI") Set moveMail = myNameSpace.GetDefaultFolder(olFolderInbox) Set myDestFolder = moveMail.Folders("Arquivado") moveMail.Move myDestFolder
I changed the variable and the rest of the code, but debugging, it stops in this line and does not finish the code, what is the problem?
problem line
Set moveMail = myNameSpace.GetDefaultFolder(olFolderInbox)
You declare moveMail as an Outlook.MailItem and then try to assign a folder object to it.
Check your code carefully and make sure that the variables you declare and use are of the correct types.
Tuesday, June 12, 2018 2:34 PM -
sorry I'm not a developer, I'm doing what I can do to solve my problem. My company does not have any developers to help me.
But I still can not solve the problem, can you correct the code for me?Tuesday, June 12, 2018 4:19 PM -
I have to say that at this point I have no idea why you are having problems. On my Outlook 2013 I tested your original code to move a message after creating a subfolder of the Inbox named "Test".
Mail.Move (Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("Test"))
The code ran without a problem and moved the message properly from the Inbox to Test.
I don't know what else I can do to help you.
- Marked as answer by Gabriel Alves Ferreira Tuesday, June 12, 2018 7:48 PM
Tuesday, June 12, 2018 4:41 PM -
Following sample worked on my system to move a mail item to "Arquivado" subfolder of Inbox. For test purposes I used the selected mailitem from the Inbox. You would use your own MailItem from your macro.
Dim m As Outlook.MailItem Dim ns As Outlook.NameSpace Dim ib As Outlook.Folder Dim av As Outlook.Folder Set m = Application.ActiveExplorer.Selection(1) Set ns = Application.GetNamespace("MAPI") Set ib = ns.GetDefaultFolder(olFolderInbox) Set av = ib.Folders("Arquivado") m.Move av Set m = Nothing Set av = Nothing Set ib = Nothing Set ns = Nothing Set m = Nothing
Tuesday, June 12, 2018 5:01 PM -
This is the end of my code, I also created the TEST subfolder. during the whole code the new email does not appear in the inbox. Only by passing by the code to move the email the mail appears in the inbox instead of to the test folder, and the first msgbox "ok1" does not appear. And the rest of the code below the end if it takes 2 minutes to finish executing.
Tuesday, June 12, 2018 5:21 PM -
As I said, I can't reproduce your problem on my system.Tuesday, June 12, 2018 5:23 PM
-
I give up, thanks for the attention.Tuesday, June 12, 2018 7:48 PM