Loop through Outbox with Inbox, Delete message with same subject line from outbox












1















Im trying to look through 2 different boxes(inbox & Outbox), compare the subject and delete the message in the outbox when a match is found. What am I doing incorrectly? Do I need to create another Folder object for each box? EDIT Im getting a "runtime error 13; type mismatch"



Sub DEID()

Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder

Set objNS = GetNamespace("MAPI")
Set objFolder = objNS.Folders.GetFirst
Set objIFolder = objFolder.Folders("Inbox")
Set objOFolder = objFolder.Folders("Outbox")

Dim Item, OItem As Outlook.MailItem

For Each Item In objIFolder.Items
Set ISub = Right(CStr(Item.Subject), Len(Item.Subject) - 6)
Set ISub = CStr(ISub)
For Each OItem In objOFolder.Items
Set OSub = Right(CStr(OItem.Subject), Len(OItem.Subject) - 6)
Set ISub = CStr(OSub)
If StrComp(ISub = OSub, 1) = 0 Then
OItem.Delete
End If
Next OItem
Next Item

End Sub









share|improve this question

























  • Does Set objFolder = objNS.Folders.GetFirst return the correct store on your system? It doesn't on mine. If I type ? Session.Folders.GetFirst.Name on my system it displays "Outlook Data File" which is the default store but not the one that holds emails if you use the wizard to install your email accounts.

    – Tony Dallimore
    Nov 17 '18 at 22:40











  • You appear to be trying to match emails by subject excluding the first six characters, If I send you an email with a subject of "abcdefghijklmnopqrstuvwxyz" your reply is likely to have a subject like "Re: abcdefghijklmnopqrstuvwxyz". Stripping off the first six characters will not make these match. You need to be more careful in preparing the subjects for comparison.

    – Tony Dallimore
    Nov 17 '18 at 22:51











  • On my system, emails only remain in "Outbox" until they are sent. After that they are in "Sent Items".

    – Tony Dallimore
    Nov 17 '18 at 22:54











  • To me this is dangerous. You assume subjects are unique. At the very least I would check the recipient of the sent email matched the sender of the received email. Are you assuming the reply will contain the text of the original email? This is very common but is not compulsory.

    – Tony Dallimore
    Nov 17 '18 at 22:59











  • Thank you for mentioning the "Re: " change to the subject. I have the outbox set on a delay. Im sure it works. Im using the same code else where.

    – user241619
    Nov 17 '18 at 23:03
















1















Im trying to look through 2 different boxes(inbox & Outbox), compare the subject and delete the message in the outbox when a match is found. What am I doing incorrectly? Do I need to create another Folder object for each box? EDIT Im getting a "runtime error 13; type mismatch"



Sub DEID()

Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder

Set objNS = GetNamespace("MAPI")
Set objFolder = objNS.Folders.GetFirst
Set objIFolder = objFolder.Folders("Inbox")
Set objOFolder = objFolder.Folders("Outbox")

Dim Item, OItem As Outlook.MailItem

For Each Item In objIFolder.Items
Set ISub = Right(CStr(Item.Subject), Len(Item.Subject) - 6)
Set ISub = CStr(ISub)
For Each OItem In objOFolder.Items
Set OSub = Right(CStr(OItem.Subject), Len(OItem.Subject) - 6)
Set ISub = CStr(OSub)
If StrComp(ISub = OSub, 1) = 0 Then
OItem.Delete
End If
Next OItem
Next Item

End Sub









share|improve this question

























  • Does Set objFolder = objNS.Folders.GetFirst return the correct store on your system? It doesn't on mine. If I type ? Session.Folders.GetFirst.Name on my system it displays "Outlook Data File" which is the default store but not the one that holds emails if you use the wizard to install your email accounts.

    – Tony Dallimore
    Nov 17 '18 at 22:40











  • You appear to be trying to match emails by subject excluding the first six characters, If I send you an email with a subject of "abcdefghijklmnopqrstuvwxyz" your reply is likely to have a subject like "Re: abcdefghijklmnopqrstuvwxyz". Stripping off the first six characters will not make these match. You need to be more careful in preparing the subjects for comparison.

    – Tony Dallimore
    Nov 17 '18 at 22:51











  • On my system, emails only remain in "Outbox" until they are sent. After that they are in "Sent Items".

    – Tony Dallimore
    Nov 17 '18 at 22:54











  • To me this is dangerous. You assume subjects are unique. At the very least I would check the recipient of the sent email matched the sender of the received email. Are you assuming the reply will contain the text of the original email? This is very common but is not compulsory.

    – Tony Dallimore
    Nov 17 '18 at 22:59











  • Thank you for mentioning the "Re: " change to the subject. I have the outbox set on a delay. Im sure it works. Im using the same code else where.

    – user241619
    Nov 17 '18 at 23:03














1












1








1








Im trying to look through 2 different boxes(inbox & Outbox), compare the subject and delete the message in the outbox when a match is found. What am I doing incorrectly? Do I need to create another Folder object for each box? EDIT Im getting a "runtime error 13; type mismatch"



Sub DEID()

Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder

Set objNS = GetNamespace("MAPI")
Set objFolder = objNS.Folders.GetFirst
Set objIFolder = objFolder.Folders("Inbox")
Set objOFolder = objFolder.Folders("Outbox")

Dim Item, OItem As Outlook.MailItem

For Each Item In objIFolder.Items
Set ISub = Right(CStr(Item.Subject), Len(Item.Subject) - 6)
Set ISub = CStr(ISub)
For Each OItem In objOFolder.Items
Set OSub = Right(CStr(OItem.Subject), Len(OItem.Subject) - 6)
Set ISub = CStr(OSub)
If StrComp(ISub = OSub, 1) = 0 Then
OItem.Delete
End If
Next OItem
Next Item

End Sub









share|improve this question
















Im trying to look through 2 different boxes(inbox & Outbox), compare the subject and delete the message in the outbox when a match is found. What am I doing incorrectly? Do I need to create another Folder object for each box? EDIT Im getting a "runtime error 13; type mismatch"



Sub DEID()

Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder

Set objNS = GetNamespace("MAPI")
Set objFolder = objNS.Folders.GetFirst
Set objIFolder = objFolder.Folders("Inbox")
Set objOFolder = objFolder.Folders("Outbox")

Dim Item, OItem As Outlook.MailItem

For Each Item In objIFolder.Items
Set ISub = Right(CStr(Item.Subject), Len(Item.Subject) - 6)
Set ISub = CStr(ISub)
For Each OItem In objOFolder.Items
Set OSub = Right(CStr(OItem.Subject), Len(OItem.Subject) - 6)
Set ISub = CStr(OSub)
If StrComp(ISub = OSub, 1) = 0 Then
OItem.Delete
End If
Next OItem
Next Item

End Sub






vba outlook outlook-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 21:03







user241619

















asked Nov 17 '18 at 20:29









user241619user241619

257




257













  • Does Set objFolder = objNS.Folders.GetFirst return the correct store on your system? It doesn't on mine. If I type ? Session.Folders.GetFirst.Name on my system it displays "Outlook Data File" which is the default store but not the one that holds emails if you use the wizard to install your email accounts.

    – Tony Dallimore
    Nov 17 '18 at 22:40











  • You appear to be trying to match emails by subject excluding the first six characters, If I send you an email with a subject of "abcdefghijklmnopqrstuvwxyz" your reply is likely to have a subject like "Re: abcdefghijklmnopqrstuvwxyz". Stripping off the first six characters will not make these match. You need to be more careful in preparing the subjects for comparison.

    – Tony Dallimore
    Nov 17 '18 at 22:51











  • On my system, emails only remain in "Outbox" until they are sent. After that they are in "Sent Items".

    – Tony Dallimore
    Nov 17 '18 at 22:54











  • To me this is dangerous. You assume subjects are unique. At the very least I would check the recipient of the sent email matched the sender of the received email. Are you assuming the reply will contain the text of the original email? This is very common but is not compulsory.

    – Tony Dallimore
    Nov 17 '18 at 22:59











  • Thank you for mentioning the "Re: " change to the subject. I have the outbox set on a delay. Im sure it works. Im using the same code else where.

    – user241619
    Nov 17 '18 at 23:03



















  • Does Set objFolder = objNS.Folders.GetFirst return the correct store on your system? It doesn't on mine. If I type ? Session.Folders.GetFirst.Name on my system it displays "Outlook Data File" which is the default store but not the one that holds emails if you use the wizard to install your email accounts.

    – Tony Dallimore
    Nov 17 '18 at 22:40











  • You appear to be trying to match emails by subject excluding the first six characters, If I send you an email with a subject of "abcdefghijklmnopqrstuvwxyz" your reply is likely to have a subject like "Re: abcdefghijklmnopqrstuvwxyz". Stripping off the first six characters will not make these match. You need to be more careful in preparing the subjects for comparison.

    – Tony Dallimore
    Nov 17 '18 at 22:51











  • On my system, emails only remain in "Outbox" until they are sent. After that they are in "Sent Items".

    – Tony Dallimore
    Nov 17 '18 at 22:54











  • To me this is dangerous. You assume subjects are unique. At the very least I would check the recipient of the sent email matched the sender of the received email. Are you assuming the reply will contain the text of the original email? This is very common but is not compulsory.

    – Tony Dallimore
    Nov 17 '18 at 22:59











  • Thank you for mentioning the "Re: " change to the subject. I have the outbox set on a delay. Im sure it works. Im using the same code else where.

    – user241619
    Nov 17 '18 at 23:03

















Does Set objFolder = objNS.Folders.GetFirst return the correct store on your system? It doesn't on mine. If I type ? Session.Folders.GetFirst.Name on my system it displays "Outlook Data File" which is the default store but not the one that holds emails if you use the wizard to install your email accounts.

– Tony Dallimore
Nov 17 '18 at 22:40





Does Set objFolder = objNS.Folders.GetFirst return the correct store on your system? It doesn't on mine. If I type ? Session.Folders.GetFirst.Name on my system it displays "Outlook Data File" which is the default store but not the one that holds emails if you use the wizard to install your email accounts.

– Tony Dallimore
Nov 17 '18 at 22:40













You appear to be trying to match emails by subject excluding the first six characters, If I send you an email with a subject of "abcdefghijklmnopqrstuvwxyz" your reply is likely to have a subject like "Re: abcdefghijklmnopqrstuvwxyz". Stripping off the first six characters will not make these match. You need to be more careful in preparing the subjects for comparison.

– Tony Dallimore
Nov 17 '18 at 22:51





You appear to be trying to match emails by subject excluding the first six characters, If I send you an email with a subject of "abcdefghijklmnopqrstuvwxyz" your reply is likely to have a subject like "Re: abcdefghijklmnopqrstuvwxyz". Stripping off the first six characters will not make these match. You need to be more careful in preparing the subjects for comparison.

– Tony Dallimore
Nov 17 '18 at 22:51













On my system, emails only remain in "Outbox" until they are sent. After that they are in "Sent Items".

– Tony Dallimore
Nov 17 '18 at 22:54





On my system, emails only remain in "Outbox" until they are sent. After that they are in "Sent Items".

– Tony Dallimore
Nov 17 '18 at 22:54













To me this is dangerous. You assume subjects are unique. At the very least I would check the recipient of the sent email matched the sender of the received email. Are you assuming the reply will contain the text of the original email? This is very common but is not compulsory.

– Tony Dallimore
Nov 17 '18 at 22:59





To me this is dangerous. You assume subjects are unique. At the very least I would check the recipient of the sent email matched the sender of the received email. Are you assuming the reply will contain the text of the original email? This is very common but is not compulsory.

– Tony Dallimore
Nov 17 '18 at 22:59













Thank you for mentioning the "Re: " change to the subject. I have the outbox set on a delay. Im sure it works. Im using the same code else where.

– user241619
Nov 17 '18 at 23:03





Thank you for mentioning the "Re: " change to the subject. I have the outbox set on a delay. Im sure it works. Im using the same code else where.

– user241619
Nov 17 '18 at 23:03












2 Answers
2






active

oldest

votes


















0














One thing that jumps out at me is you are using a set command on a value type (subject, which is a string), which you don't need and should cause an error.



Dim Item, OItem As Outlook.MailItem
Dim ISub, OSub As String

For Each Item In objIFolder.Items
ISub = Right(CStr(Item.Subject), Len(Item.Subject) - 6)
ISub = CStr(ISub)
For Each OItem In objOFolder.Items
OSub = Right(CStr(OItem.Subject), Len(OItem.Subject) - 6)
ISub = CStr(OSub)
If StrComp(ISub = OSub, 1) = 0 Then
OItem.Delete
End If
Next OItem
Next Item


One other observation... This line:



ISub = CStr(OSub)


Seems to me like it will force the next condition to always be true. Unless I misunderstand, that seems like a mistake.



I also think the String conversion are unnecessary since subject is already a string.



This would be my final version:



Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder

Set objNS = GetNamespace("MAPI")
Set objFolder = objNS.Folders.GetFirst
Set objIFolder = objFolder.Folders("Inbox")
Set objOFolder = objFolder.Folders("Outbox")

Dim Item, OItem As Outlook.MailItem
Dim ISub, OSub As String

For Each Item In objIFolder.Items
ISub = Right(Item.Subject, Len(Item.Subject) - 6)
For Each OItem In objOFolder.Items
OSub = Right(OItem.Subject, Len(OItem.Subject) - 6)
If ISub = OSub Then
OItem.Delete
End If
Next OItem
Next Item





share|improve this answer
























  • I got "runtine error '-2147221233 (8004010f)' The attempted operaion faild. An object could not be found."

    – user241619
    Nov 17 '18 at 21:21











  • If you step through the code, where did the error occur? It was probably a typo... I didn't attempt to run this (I don't want to delete them, and besides, my outbox is empty)

    – Hambone
    Nov 17 '18 at 21:24











  • Set objIFolder = objFolder.Folders("Inbox") is the breaking point

    – user241619
    Nov 17 '18 at 21:33













  • Is it possible Set objFolder = objNS.Folders.GetFirst is not what you think it is? Just guessing here... maybe explicitly call out which folder you want. For example Set objFolder = objNS.Folders("youremail@yourdomain.com")

    – Hambone
    Nov 17 '18 at 21:35











  • It works elsewhere. Now that we set it explicitly, Im getting a "runtime error 5 invalid procedure call or argument" same breaking point

    – user241619
    Nov 17 '18 at 21:39





















0














Firstly, you are dimming Item and OItem as Outlook.MailItem - you can have other items in the Inbox folder (hence the Type Mismatch error), such as ReportItem or MeetingItem. Dim these variables as a generic Object.



Secondly, you are deleting items in a collection while you are looping through it. Do not do that - use a down loop (for i = Items.Count to 1 step -1).



Thirdly, do not loop through all items in a folder - this is hugely inefficient, let Outlook do the job - for the inner use Items.Find / FindNext or Items.Restrict with a query like @SQL="http://schemas.microsoft.com/mapi/proptag/0x0E1D001F" like '%some value%'.



For the outer loop, again, dd not loop, retrieve all subjects in a single call using MAPIFolder.GetTable() / Table.Columns.Add / Table.GetArray / etc. - see https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook.table?view=outlook-pia






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53355283%2floop-through-outbox-with-inbox-delete-message-with-same-subject-line-from-outbo%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    One thing that jumps out at me is you are using a set command on a value type (subject, which is a string), which you don't need and should cause an error.



    Dim Item, OItem As Outlook.MailItem
    Dim ISub, OSub As String

    For Each Item In objIFolder.Items
    ISub = Right(CStr(Item.Subject), Len(Item.Subject) - 6)
    ISub = CStr(ISub)
    For Each OItem In objOFolder.Items
    OSub = Right(CStr(OItem.Subject), Len(OItem.Subject) - 6)
    ISub = CStr(OSub)
    If StrComp(ISub = OSub, 1) = 0 Then
    OItem.Delete
    End If
    Next OItem
    Next Item


    One other observation... This line:



    ISub = CStr(OSub)


    Seems to me like it will force the next condition to always be true. Unless I misunderstand, that seems like a mistake.



    I also think the String conversion are unnecessary since subject is already a string.



    This would be my final version:



    Dim objNS As Outlook.NameSpace
    Dim objFolder As Outlook.MAPIFolder

    Set objNS = GetNamespace("MAPI")
    Set objFolder = objNS.Folders.GetFirst
    Set objIFolder = objFolder.Folders("Inbox")
    Set objOFolder = objFolder.Folders("Outbox")

    Dim Item, OItem As Outlook.MailItem
    Dim ISub, OSub As String

    For Each Item In objIFolder.Items
    ISub = Right(Item.Subject, Len(Item.Subject) - 6)
    For Each OItem In objOFolder.Items
    OSub = Right(OItem.Subject, Len(OItem.Subject) - 6)
    If ISub = OSub Then
    OItem.Delete
    End If
    Next OItem
    Next Item





    share|improve this answer
























    • I got "runtine error '-2147221233 (8004010f)' The attempted operaion faild. An object could not be found."

      – user241619
      Nov 17 '18 at 21:21











    • If you step through the code, where did the error occur? It was probably a typo... I didn't attempt to run this (I don't want to delete them, and besides, my outbox is empty)

      – Hambone
      Nov 17 '18 at 21:24











    • Set objIFolder = objFolder.Folders("Inbox") is the breaking point

      – user241619
      Nov 17 '18 at 21:33













    • Is it possible Set objFolder = objNS.Folders.GetFirst is not what you think it is? Just guessing here... maybe explicitly call out which folder you want. For example Set objFolder = objNS.Folders("youremail@yourdomain.com")

      – Hambone
      Nov 17 '18 at 21:35











    • It works elsewhere. Now that we set it explicitly, Im getting a "runtime error 5 invalid procedure call or argument" same breaking point

      – user241619
      Nov 17 '18 at 21:39


















    0














    One thing that jumps out at me is you are using a set command on a value type (subject, which is a string), which you don't need and should cause an error.



    Dim Item, OItem As Outlook.MailItem
    Dim ISub, OSub As String

    For Each Item In objIFolder.Items
    ISub = Right(CStr(Item.Subject), Len(Item.Subject) - 6)
    ISub = CStr(ISub)
    For Each OItem In objOFolder.Items
    OSub = Right(CStr(OItem.Subject), Len(OItem.Subject) - 6)
    ISub = CStr(OSub)
    If StrComp(ISub = OSub, 1) = 0 Then
    OItem.Delete
    End If
    Next OItem
    Next Item


    One other observation... This line:



    ISub = CStr(OSub)


    Seems to me like it will force the next condition to always be true. Unless I misunderstand, that seems like a mistake.



    I also think the String conversion are unnecessary since subject is already a string.



    This would be my final version:



    Dim objNS As Outlook.NameSpace
    Dim objFolder As Outlook.MAPIFolder

    Set objNS = GetNamespace("MAPI")
    Set objFolder = objNS.Folders.GetFirst
    Set objIFolder = objFolder.Folders("Inbox")
    Set objOFolder = objFolder.Folders("Outbox")

    Dim Item, OItem As Outlook.MailItem
    Dim ISub, OSub As String

    For Each Item In objIFolder.Items
    ISub = Right(Item.Subject, Len(Item.Subject) - 6)
    For Each OItem In objOFolder.Items
    OSub = Right(OItem.Subject, Len(OItem.Subject) - 6)
    If ISub = OSub Then
    OItem.Delete
    End If
    Next OItem
    Next Item





    share|improve this answer
























    • I got "runtine error '-2147221233 (8004010f)' The attempted operaion faild. An object could not be found."

      – user241619
      Nov 17 '18 at 21:21











    • If you step through the code, where did the error occur? It was probably a typo... I didn't attempt to run this (I don't want to delete them, and besides, my outbox is empty)

      – Hambone
      Nov 17 '18 at 21:24











    • Set objIFolder = objFolder.Folders("Inbox") is the breaking point

      – user241619
      Nov 17 '18 at 21:33













    • Is it possible Set objFolder = objNS.Folders.GetFirst is not what you think it is? Just guessing here... maybe explicitly call out which folder you want. For example Set objFolder = objNS.Folders("youremail@yourdomain.com")

      – Hambone
      Nov 17 '18 at 21:35











    • It works elsewhere. Now that we set it explicitly, Im getting a "runtime error 5 invalid procedure call or argument" same breaking point

      – user241619
      Nov 17 '18 at 21:39
















    0












    0








    0







    One thing that jumps out at me is you are using a set command on a value type (subject, which is a string), which you don't need and should cause an error.



    Dim Item, OItem As Outlook.MailItem
    Dim ISub, OSub As String

    For Each Item In objIFolder.Items
    ISub = Right(CStr(Item.Subject), Len(Item.Subject) - 6)
    ISub = CStr(ISub)
    For Each OItem In objOFolder.Items
    OSub = Right(CStr(OItem.Subject), Len(OItem.Subject) - 6)
    ISub = CStr(OSub)
    If StrComp(ISub = OSub, 1) = 0 Then
    OItem.Delete
    End If
    Next OItem
    Next Item


    One other observation... This line:



    ISub = CStr(OSub)


    Seems to me like it will force the next condition to always be true. Unless I misunderstand, that seems like a mistake.



    I also think the String conversion are unnecessary since subject is already a string.



    This would be my final version:



    Dim objNS As Outlook.NameSpace
    Dim objFolder As Outlook.MAPIFolder

    Set objNS = GetNamespace("MAPI")
    Set objFolder = objNS.Folders.GetFirst
    Set objIFolder = objFolder.Folders("Inbox")
    Set objOFolder = objFolder.Folders("Outbox")

    Dim Item, OItem As Outlook.MailItem
    Dim ISub, OSub As String

    For Each Item In objIFolder.Items
    ISub = Right(Item.Subject, Len(Item.Subject) - 6)
    For Each OItem In objOFolder.Items
    OSub = Right(OItem.Subject, Len(OItem.Subject) - 6)
    If ISub = OSub Then
    OItem.Delete
    End If
    Next OItem
    Next Item





    share|improve this answer













    One thing that jumps out at me is you are using a set command on a value type (subject, which is a string), which you don't need and should cause an error.



    Dim Item, OItem As Outlook.MailItem
    Dim ISub, OSub As String

    For Each Item In objIFolder.Items
    ISub = Right(CStr(Item.Subject), Len(Item.Subject) - 6)
    ISub = CStr(ISub)
    For Each OItem In objOFolder.Items
    OSub = Right(CStr(OItem.Subject), Len(OItem.Subject) - 6)
    ISub = CStr(OSub)
    If StrComp(ISub = OSub, 1) = 0 Then
    OItem.Delete
    End If
    Next OItem
    Next Item


    One other observation... This line:



    ISub = CStr(OSub)


    Seems to me like it will force the next condition to always be true. Unless I misunderstand, that seems like a mistake.



    I also think the String conversion are unnecessary since subject is already a string.



    This would be my final version:



    Dim objNS As Outlook.NameSpace
    Dim objFolder As Outlook.MAPIFolder

    Set objNS = GetNamespace("MAPI")
    Set objFolder = objNS.Folders.GetFirst
    Set objIFolder = objFolder.Folders("Inbox")
    Set objOFolder = objFolder.Folders("Outbox")

    Dim Item, OItem As Outlook.MailItem
    Dim ISub, OSub As String

    For Each Item In objIFolder.Items
    ISub = Right(Item.Subject, Len(Item.Subject) - 6)
    For Each OItem In objOFolder.Items
    OSub = Right(OItem.Subject, Len(OItem.Subject) - 6)
    If ISub = OSub Then
    OItem.Delete
    End If
    Next OItem
    Next Item






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 17 '18 at 21:11









    HamboneHambone

    9,73752747




    9,73752747













    • I got "runtine error '-2147221233 (8004010f)' The attempted operaion faild. An object could not be found."

      – user241619
      Nov 17 '18 at 21:21











    • If you step through the code, where did the error occur? It was probably a typo... I didn't attempt to run this (I don't want to delete them, and besides, my outbox is empty)

      – Hambone
      Nov 17 '18 at 21:24











    • Set objIFolder = objFolder.Folders("Inbox") is the breaking point

      – user241619
      Nov 17 '18 at 21:33













    • Is it possible Set objFolder = objNS.Folders.GetFirst is not what you think it is? Just guessing here... maybe explicitly call out which folder you want. For example Set objFolder = objNS.Folders("youremail@yourdomain.com")

      – Hambone
      Nov 17 '18 at 21:35











    • It works elsewhere. Now that we set it explicitly, Im getting a "runtime error 5 invalid procedure call or argument" same breaking point

      – user241619
      Nov 17 '18 at 21:39





















    • I got "runtine error '-2147221233 (8004010f)' The attempted operaion faild. An object could not be found."

      – user241619
      Nov 17 '18 at 21:21











    • If you step through the code, where did the error occur? It was probably a typo... I didn't attempt to run this (I don't want to delete them, and besides, my outbox is empty)

      – Hambone
      Nov 17 '18 at 21:24











    • Set objIFolder = objFolder.Folders("Inbox") is the breaking point

      – user241619
      Nov 17 '18 at 21:33













    • Is it possible Set objFolder = objNS.Folders.GetFirst is not what you think it is? Just guessing here... maybe explicitly call out which folder you want. For example Set objFolder = objNS.Folders("youremail@yourdomain.com")

      – Hambone
      Nov 17 '18 at 21:35











    • It works elsewhere. Now that we set it explicitly, Im getting a "runtime error 5 invalid procedure call or argument" same breaking point

      – user241619
      Nov 17 '18 at 21:39



















    I got "runtine error '-2147221233 (8004010f)' The attempted operaion faild. An object could not be found."

    – user241619
    Nov 17 '18 at 21:21





    I got "runtine error '-2147221233 (8004010f)' The attempted operaion faild. An object could not be found."

    – user241619
    Nov 17 '18 at 21:21













    If you step through the code, where did the error occur? It was probably a typo... I didn't attempt to run this (I don't want to delete them, and besides, my outbox is empty)

    – Hambone
    Nov 17 '18 at 21:24





    If you step through the code, where did the error occur? It was probably a typo... I didn't attempt to run this (I don't want to delete them, and besides, my outbox is empty)

    – Hambone
    Nov 17 '18 at 21:24













    Set objIFolder = objFolder.Folders("Inbox") is the breaking point

    – user241619
    Nov 17 '18 at 21:33







    Set objIFolder = objFolder.Folders("Inbox") is the breaking point

    – user241619
    Nov 17 '18 at 21:33















    Is it possible Set objFolder = objNS.Folders.GetFirst is not what you think it is? Just guessing here... maybe explicitly call out which folder you want. For example Set objFolder = objNS.Folders("youremail@yourdomain.com")

    – Hambone
    Nov 17 '18 at 21:35





    Is it possible Set objFolder = objNS.Folders.GetFirst is not what you think it is? Just guessing here... maybe explicitly call out which folder you want. For example Set objFolder = objNS.Folders("youremail@yourdomain.com")

    – Hambone
    Nov 17 '18 at 21:35













    It works elsewhere. Now that we set it explicitly, Im getting a "runtime error 5 invalid procedure call or argument" same breaking point

    – user241619
    Nov 17 '18 at 21:39







    It works elsewhere. Now that we set it explicitly, Im getting a "runtime error 5 invalid procedure call or argument" same breaking point

    – user241619
    Nov 17 '18 at 21:39















    0














    Firstly, you are dimming Item and OItem as Outlook.MailItem - you can have other items in the Inbox folder (hence the Type Mismatch error), such as ReportItem or MeetingItem. Dim these variables as a generic Object.



    Secondly, you are deleting items in a collection while you are looping through it. Do not do that - use a down loop (for i = Items.Count to 1 step -1).



    Thirdly, do not loop through all items in a folder - this is hugely inefficient, let Outlook do the job - for the inner use Items.Find / FindNext or Items.Restrict with a query like @SQL="http://schemas.microsoft.com/mapi/proptag/0x0E1D001F" like '%some value%'.



    For the outer loop, again, dd not loop, retrieve all subjects in a single call using MAPIFolder.GetTable() / Table.Columns.Add / Table.GetArray / etc. - see https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook.table?view=outlook-pia






    share|improve this answer




























      0














      Firstly, you are dimming Item and OItem as Outlook.MailItem - you can have other items in the Inbox folder (hence the Type Mismatch error), such as ReportItem or MeetingItem. Dim these variables as a generic Object.



      Secondly, you are deleting items in a collection while you are looping through it. Do not do that - use a down loop (for i = Items.Count to 1 step -1).



      Thirdly, do not loop through all items in a folder - this is hugely inefficient, let Outlook do the job - for the inner use Items.Find / FindNext or Items.Restrict with a query like @SQL="http://schemas.microsoft.com/mapi/proptag/0x0E1D001F" like '%some value%'.



      For the outer loop, again, dd not loop, retrieve all subjects in a single call using MAPIFolder.GetTable() / Table.Columns.Add / Table.GetArray / etc. - see https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook.table?view=outlook-pia






      share|improve this answer


























        0












        0








        0







        Firstly, you are dimming Item and OItem as Outlook.MailItem - you can have other items in the Inbox folder (hence the Type Mismatch error), such as ReportItem or MeetingItem. Dim these variables as a generic Object.



        Secondly, you are deleting items in a collection while you are looping through it. Do not do that - use a down loop (for i = Items.Count to 1 step -1).



        Thirdly, do not loop through all items in a folder - this is hugely inefficient, let Outlook do the job - for the inner use Items.Find / FindNext or Items.Restrict with a query like @SQL="http://schemas.microsoft.com/mapi/proptag/0x0E1D001F" like '%some value%'.



        For the outer loop, again, dd not loop, retrieve all subjects in a single call using MAPIFolder.GetTable() / Table.Columns.Add / Table.GetArray / etc. - see https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook.table?view=outlook-pia






        share|improve this answer













        Firstly, you are dimming Item and OItem as Outlook.MailItem - you can have other items in the Inbox folder (hence the Type Mismatch error), such as ReportItem or MeetingItem. Dim these variables as a generic Object.



        Secondly, you are deleting items in a collection while you are looping through it. Do not do that - use a down loop (for i = Items.Count to 1 step -1).



        Thirdly, do not loop through all items in a folder - this is hugely inefficient, let Outlook do the job - for the inner use Items.Find / FindNext or Items.Restrict with a query like @SQL="http://schemas.microsoft.com/mapi/proptag/0x0E1D001F" like '%some value%'.



        For the outer loop, again, dd not loop, retrieve all subjects in a single call using MAPIFolder.GetTable() / Table.Columns.Add / Table.GetArray / etc. - see https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook.table?view=outlook-pia







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 17 '18 at 23:11









        Dmitry StreblechenkoDmitry Streblechenko

        42.7k32760




        42.7k32760






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53355283%2floop-through-outbox-with-inbox-delete-message-with-same-subject-line-from-outbo%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            鏡平學校

            ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

            Why https connections are so slow when debugging (stepping over) in Java?