'VBA ERROR = runtime error 13 type mismatch

trying to move list of emails I have on excel , from shared folder to private folder

if the subject name matches [range("b"&i).value= via.cstr(olmail.subject)] then the email should move from the public folder to the private folder ("dup")

I am getting error on running the code runtime error 13 type mismatch

if I hover the mouse over olmail.item it shows object variable or with block variable not set

as the subject names are mixed that is alphabets and numbers I tried converting range("b"&i).value to string and olmail.subject to string still I get error.

below is the code

sub need_move
dim ol as outlook.application
set ol = new outlook.application

dim olns as outlook.namespace
set olns= ol.getnamespace("mapi")

dim recip as outlook.recipient
set recipe = olns.createrecipient("[email protected]")

dim fldr as outlook.mapifolder
set fldr = olns.getshareddefauldfolder(recip,olfolderinbox)

dim mydestfolder as outlook.folder
set mydestfolder = olns.getdefaultfolder(olfolderinbox).folders("dup")

dim I as long 
dim olmail as outlook.mailitem
 for each olmail in fldr.items

for I = 2 to 3

if via.cstr(range("b"&i).value= via.cstr(olmail.subject) then
 
olmail.move mydestfolder
else
endif
next
next

exit sub
end sub



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source