MOVE mail from box to other box
-
- PlatinumLounger
- Posts: 4343
- Joined: 26 Apr 2010, 17:36
MOVE mail from box to other box
I need to cut email from box named "test1" (with instr in object is present the strng "TEST_002") and paste into another box named "test2", naturally via vba code for excel....how to.?
-
- Administrator
- Posts: 78391
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: MOVE mail from box to other box
Should we look for the text "TEST_002" in the subject of the e-mail or in the body of the e-mail?
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4343
- Joined: 26 Apr 2010, 17:36
Re: MOVE mail from box to other box
HansV wrote:Should we look for the text "TEST_002" in the subject of the e-mail or in the body of the e-mail?
i'm sorry... in subject of email!
Note:
you read my mind, as usual!
-
- Administrator
- Posts: 78391
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: MOVE mail from box to other box
Try this macro:
If you get an error message, please note on which line the error occurs and what the error message says.
Code: Select all
Sub MoveMail()
Dim olApp As Object
Dim olNsp As Object
Dim olInbox As Object
Dim olSrc As Object
Dim olTrg As Object
Dim olItm As Object
Dim i As Long
On Error Resume Next
Set olApp = CreateObject(Class:="Outlook.Application")
Set olNsp = olApp.GetNamespace("MAPI")
Set olInbox = olNsp.GetDefaultFolder(6) ' olFolderInbox
Set olSrc = olInbox.Parent.Folders("test1")
Set olTrg = olInbox.Parent.Folders("test2")
For i = olSrc.Items.Count To 1 Step -1
Set olItm = olSrc.Items(i)
If UCase(olItm.Subject) Like "*TEST_002*" Then
olItm.Move olTrg
End If
Next i
End Sub
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4343
- Joined: 26 Apr 2010, 17:36
Re: MOVE mail from box to other box
HansV wrote:Try this macro:
If you get an error message, please note on which line the error occurs and what the error message says.Code: Select all
Sub MoveMail() Dim olApp As Object Dim olNsp As Object Dim olInbox As Object Dim olSrc As Object Dim olTrg As Object Dim olItm As Object Dim i As Long On Error Resume Next Set olApp = CreateObject(Class:="Outlook.Application") Set olNsp = olApp.GetNamespace("MAPI") Set olInbox = olNsp.GetDefaultFolder(6) ' olFolderInbox Set olSrc = olInbox.Parent.Folders("test1") Set olTrg = olInbox.Parent.Folders("test2") For i = olSrc.Items.Count To 1 Step -1 Set olItm = olSrc.Items(i) If UCase(olItm.Subject) Like "*TEST_002*" Then olItm.Move olTrg End If Next i End Sub
NO ERROR!
MEGA CODE!!!!!!
WORK PERFECT,
-
- PlatinumLounger
- Posts: 4343
- Joined: 26 Apr 2010, 17:36
Re: MOVE mail from box to other box
Hans sorry me... but in test2 have a sub box named test3... and i need to move int this box the email. in effect the path is:HansV wrote:Try this macro:
If you get an error message, please note on which line the error occurs and what the error message says.Code: Select all
Sub MoveMail() Dim olApp As Object Dim olNsp As Object Dim olInbox As Object Dim olSrc As Object Dim olTrg As Object Dim olItm As Object Dim i As Long On Error Resume Next Set olApp = CreateObject(Class:="Outlook.Application") Set olNsp = olApp.GetNamespace("MAPI") Set olInbox = olNsp.GetDefaultFolder(6) ' olFolderInbox Set olSrc = olInbox.Parent.Folders("test1") Set olTrg = olInbox.Parent.Folders("test2") For i = olSrc.Items.Count To 1 Step -1 Set olItm = olSrc.Items(i) If UCase(olItm.Subject) Like "*TEST_002*" Then olItm.Move olTrg End If Next i End Sub
...test2\test3\here to email moved
-
- Administrator
- Posts: 78391
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: MOVE mail from box to other box
Change the line
to
Code: Select all
Set olTrg = olInbox.Parent.Folders("test2")
Code: Select all
Set olTrg = olInbox.Parent.Folders("test2").Folders("test3")
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4343
- Joined: 26 Apr 2010, 17:36
Re: MOVE mail from box to other box
TKS!HansV wrote:Change the line
toCode: Select all
Set olTrg = olInbox.Parent.Folders("test2")
Code: Select all
Set olTrg = olInbox.Parent.Folders("test2").Folders("test3")
I have this piece of code:
Set objItms = objFld.Items.Restrict("[SenderName]='gss.italy@iol.it'")
i need to insert this filter code in addiction of:
UCase(olItm.Subject) Like "*TEST_002*"
-
- Administrator
- Posts: 78391
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: MOVE mail from box to other box
Restrict doesn't support the use of Like and wildcards such as "*", so we have to use a trick. Try
Set objItms = objFld.Items.Restrict("@SQL=""urn:schemas:mailheader:subject"" Like '%TEST[_]002%' AND ""urn:schemas:httpmail:sendername"" = 'gss.italy@iol.it'")
Set objItms = objFld.Items.Restrict("@SQL=""urn:schemas:mailheader:subject"" Like '%TEST[_]002%' AND ""urn:schemas:httpmail:sendername"" = 'gss.italy@iol.it'")
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4343
- Joined: 26 Apr 2010, 17:36
Re: MOVE mail from box to other box
BRAKET [ ]... Why!?HansV wrote:Restrict doesn't support the use of Like and wildcards such as "*", so we have to use a trick. Try
Set objItms = objFld.Items.Restrict("@SQL=""urn:schemas:mailheader:subject"" Like '%TEST[_]002%' AND ""urn:schemas:httpmail:sendername"" = 'gss.italy@iol.it'")
and have error in objItms and objFld....
-
- Administrator
- Posts: 78391
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: MOVE mail from box to other box
_ is a wildcard. The [ ] specifies that _ is a literal character here.
What is the error message?
What is the error message?
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4343
- Joined: 26 Apr 2010, 17:36
Re: MOVE mail from box to other box
variable not dimensioned... i think have a different name in initial code:-(HansV wrote:_ is a wildcard. The [ ] specifies that _ is a literal character here.
What is the error message?
-
- Administrator
- Posts: 78391
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: MOVE mail from box to other box
You need
Dim objFld As Object
Dim objItms As Object
Dim objFld As Object
Dim objItms As Object
Best wishes,
Hans
Hans