MOVE mail from box to other box

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

MOVE mail from box to other box

Post by sal21 »

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.?
:thankyou:

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: MOVE mail from box to other box

Post by HansV »

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

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

Re: MOVE mail from box to other box

Post by sal21 »

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!

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: MOVE mail from box to other box

Post by HansV »

Try this macro:

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
If you get an error message, please note on which line the error occurs and what the error message says.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

Re: MOVE mail from box to other box

Post by sal21 »

HansV wrote:Try this macro:

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
If you get an error message, please note on which line the error occurs and what the error message says.


NO ERROR!
MEGA CODE!!!!!!

WORK PERFECT, :thankyou: :clapping:

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

Re: MOVE mail from box to other box

Post by sal21 »

HansV wrote:Try this macro:

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
If you get an error message, please note on which line the error occurs and what the error message says.
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:
...test2\test3\here to email moved

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: MOVE mail from box to other box

Post by HansV »

Change the line

Code: Select all

    Set olTrg = olInbox.Parent.Folders("test2")
to

Code: Select all

    Set olTrg = olInbox.Parent.Folders("test2").Folders("test3")
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

Re: MOVE mail from box to other box

Post by sal21 »

HansV wrote:Change the line

Code: Select all

    Set olTrg = olInbox.Parent.Folders("test2")
to

Code: Select all

    Set olTrg = olInbox.Parent.Folders("test2").Folders("test3")
TKS!

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*"

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: MOVE mail from box to other box

Post by HansV »

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'")
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

Re: MOVE mail from box to other box

Post by sal21 »

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'")
BRAKET [ ]... Why!?

and have error in objItms and objFld....

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: MOVE mail from box to other box

Post by HansV »

_ is a wildcard. The [ ] specifies that _ is a literal character here.

What is the error message?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

Re: MOVE mail from box to other box

Post by sal21 »

HansV wrote:_ is a wildcard. The [ ] specifies that _ is a literal character here.

What is the error message?
variable not dimensioned... i think have a different name in initial code:-(

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: MOVE mail from box to other box

Post by HansV »

You need

Dim objFld As Object
Dim objItms As Object
Best wishes,
Hans