Copy email to different folder, works in 2003 but not 2007

  • Thread starter Thread starter cirrus
  • Start date Start date
Status
Not open for further replies.
C

cirrus

Hi all, I've used the code below in 2003 to copy sent items into my

Inbox (and also mark deleted items as read, but that part is OK). I've

upgraded to 2007, and now when I send an emial it throws the error

"Run-time error '-2147221241 (80040107)" and the debugger takes me to

the line "Set objItemCopy = Item.Copy".

Some odd things: The first time I send an email after opening Outlook,

it gives the error but the email is moved to the inbox. Subsequent

emails don't seem to run the code. Also, if I put a breakpoint on the

line in question and step through the function, it works without any

errors - and it continues to work for the subsequent emails, as long

as I step through using the debugger.

Thanks for your help!!

Regards,

ES

Option Explicit

Dim objInbox As Outlook.MAPIFolder

Dim WithEvents objDeletedItems As Outlook.Items

Dim WithEvents objSentItems As Outlook.Items

Private Sub Application_Startup()

Dim objNS As Outlook.NameSpace

Set objNS = Application.GetNamespace("MAPI")

'objInbox was declared with module scope

Set objInbox = objNS.GetDefaultFolder(olFolderInbox)

Dim objDeleted As Outlook.MAPIFolder

Set objDeleted = objNS.GetDefaultFolder(olFolderDeletedItems)

Set objDeletedItems = objDeleted.Items

Dim objSent As Outlook.MAPIFolder

Set objSent = objNS.GetDefaultFolder(olFolderSentMail)

Set objSentItems = objSent.Items

End Sub

Private Sub objDeletedItems_ItemAdd(ByVal Item As Object)

Item.UnRead = False

Item.Save

End Sub

Private Sub objSentItems_ItemAdd(ByVal Item As Object)

Dim objItemCopy As Object

Set objItemCopy = Item.Copy

objItemCopy.Move objInbox

End Sub
 
See if it helps to add a DoEvents() call just before you do the copy.

Also, ItemAdd fires at different stages in the receiving process in

different versions of Outlook and depending on whether any rules are

defined. I usually would set up something to let ItemAdd() finish and then

let a timer or something alert a different procedure to do the copy/move.

"cirrus" <esampson@cirrusaircraft.com> wrote in message

news:e849ab25-aceb-4ae0-9629-8e7d80632097@r34g2000vbi.googlegroups.com...
> Hi all, I've used the code below in 2003 to copy sent items into my
> Inbox (and also mark deleted items as read, but that part is OK). I've
> upgraded to 2007, and now when I send an emial it throws the error
> "Run-time error '-2147221241 (80040107)" and the debugger takes me to
> the line "Set objItemCopy = Item.Copy".

> Some odd things: The first time I send an email after opening Outlook,
> it gives the error but the email is moved to the inbox. Subsequent
> emails don't seem to run the code. Also, if I put a breakpoint on the
> line in question and step through the function, it works without any
> errors - and it continues to work for the subsequent emails, as long
> as I step through using the debugger.

> Thanks for your help!!
> Regards,
> ES

> Option Explicit
> Dim objInbox As Outlook.MAPIFolder
> Dim WithEvents objDeletedItems As Outlook.Items
> Dim WithEvents objSentItems As Outlook.Items

> Private Sub Application_Startup()

> Dim objNS As Outlook.NameSpace
> Set objNS = Application.GetNamespace("MAPI")

> 'objInbox was declared with module scope
> Set objInbox = objNS.GetDefaultFolder(olFolderInbox)

> Dim objDeleted As Outlook.MAPIFolder
> Set objDeleted = objNS.GetDefaultFolder(olFolderDeletedItems)
> Set objDeletedItems = objDeleted.Items

> Dim objSent As Outlook.MAPIFolder
> Set objSent = objNS.GetDefaultFolder(olFolderSentMail)
> Set objSentItems = objSent.Items

> End Sub

> Private Sub objDeletedItems_ItemAdd(ByVal Item As Object)

> Item.UnRead = False
> Item.Save

> End Sub

> Private Sub objSentItems_ItemAdd(ByVal Item As Object)

> Dim objItemCopy As Object
> Set objItemCopy = Item.Copy
> objItemCopy.Move objInbox

> End Sub
 
On May 14, 1:52 pm, "
<kenslo...@mvps.org> wrote:
> See if it helps to add a DoEvents() call just before you do the copy.

> Also, ItemAdd fires at different stages in the receiving process in
> different versions of Outlook and depending on whether any rules are
> defined. I usually would set up something to let ItemAdd() finish and then
> let a timer or something alert a different procedure to do the copy/move.

> >

> http://www.slovaktech.com

>


Thanks Ken. Adding a DoEvents before the copy didn't help, but adding

one between the copy and the move appears to prevent the issue.

I'm wondering if the copy is not blocking, so in the next line it's

attempting to move the copied object before it exists. Is there any

way to make the copy statement blocking? I'm somewhat suprised that it

doesn't appear to behave that way by default. Otherwise, it seems like

there is no way to tell if you need to insert DoEvents procedures

between any two given operations other than trial and error, if it

isn't guaranteed that the first line will finish before the next line

executes.

If you have time, would you mind sketching out how you would implement

your solution of setting a timer in ItemAdd to launch a different

procedure to do the copy/move? I'm curious if one would run into the

same problem and have to insert a DoEvents() between the copy and the

move.

Thanks,

ES


> "cirrus" <esamp...@cirrusaircraft.com> wrote in message

> news:e849ab25-aceb-4ae0-9629-8e7d80632097@r34g2000vbi.googlegroups.com...

>
> > Hi all, I've used the code below in 2003 to copy sent items into my
> > Inbox (and also mark deleted items as read, but that part is OK). I've
> > upgraded to 2007, and now when I send an emial it throws the error
> > "Run-time error '-2147221241 (80040107)" and the debugger takes me to
> > the line "Set objItemCopy = Item.Copy".

>
> > Some odd things: The first time I send an email after opening Outlook,
> > it gives the error but the email is moved to the inbox. Subsequent
> > emails don't seem to run the code. Also, if I put a breakpoint on the
> > line in question and step through the function, it works without any
> > errors - and it continues to work for the subsequent emails, as long
> > as I step through using the debugger.

>
> > Thanks for your help!!
> > Regards,
> > ES

>
> > Option Explicit
> > Dim objInbox As Outlook.MAPIFolder
> > Dim WithEvents objDeletedItems As Outlook.Items
> > Dim WithEvents objSentItems As Outlook.Items

>
> > Private Sub Application_Startup()

>
> >    Dim objNS As Outlook.NameSpace
> >    Set objNS = Application.GetNamespace("MAPI")

>
> >    'objInbox was declared with module scope
> >    Set objInbox = objNS.GetDefaultFolder(olFolderInbox)

>
> >    Dim objDeleted As Outlook.MAPIFolder
> >    Set objDeleted = objNS.GetDefaultFolder(olFolderDeletedItems)
> >    Set objDeletedItems = objDeleted.Items

>
> >    Dim objSent As Outlook.MAPIFolder
> >    Set objSent = objNS.GetDefaultFolder(olFolderSentMail)
> >    Set objSentItems = objSent.Items

>
> > End Sub

>
> > Private Sub objDeletedItems_ItemAdd(ByVal Item As Object)

>
> >    Item.UnRead = False
> >    Item.Save

>
> > End Sub

>
> > Private Sub objSentItems_ItemAdd(ByVal Item As Object)

>
> >    Dim objItemCopy As Object
> >    Set objItemCopy = Item.Copy
> >    objItemCopy.Move objInbox

>
> > End Sub-


 
It's not a blocking problem, just sometimes Outlook needs a little time, or

the message pump needs priming. I've seen it happen with Word, Excel, PPT

and other applications also. It happens less often in managed code, because

that's slower. But I have seen it there also.

It's also a matter of trial and error, plus experience. There aren't really

any hard and fast rules.

If DoEvents() solved it then the timer wouldn't fix things most likely.

For a timer in VBA I'd use a Win32 system timer, and set it up and work with

it using Win32 API calls. If you have VB6 installed you can also stick a

timer control from that onto a VBA UserForm and do the timer that way.

"cirrus" <esampson@cirrusaircraft.com> wrote in message

news:9a220e29-f8bc-4eae-b296-f0c01039918b@x6g2000vbg.googlegroups.com...

<snip
Thanks Ken. Adding a DoEvents before the copy didn't help, but adding

one between the copy and the move appears to prevent the issue.

I'm wondering if the copy is not blocking, so in the next line it's

attempting to move the copied object before it exists. Is there any

way to make the copy statement blocking? I'm somewhat suprised that it

doesn't appear to behave that way by default. Otherwise, it seems like

there is no way to tell if you need to insert DoEvents procedures

between any two given operations other than trial and error, if it

isn't guaranteed that the first line will finish before the next line

executes.

If you have time, would you mind sketching out how you would implement

your solution of setting a timer in ItemAdd to launch a different

procedure to do the copy/move? I'm curious if one would run into the

same problem and have to insert a DoEvents() between the copy and the

move.

Thanks,

ES
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
O Copy email content and paste into new Word Document using a different font Using Outlook 1
M "Attachment Detacher for Outlook" add in, does it update the server copy of the email? Using Outlook 1
Z Copy specific email body text Outlook VBA and Custom Forms 0
B Need to Copy an email to a subfolder Outlook VBA and Custom Forms 2
Z VBA to convert email to task, insert text of email in task notes, and attach copy of original email Outlook VBA and Custom Forms 4
R Sending email copy (*.msg file) of sent email if subject line contains specific string. Outlook VBA and Custom Forms 1
K ind specific Subject line from outlook and copy the content of the email body to exce Outlook VBA and Custom Forms 0
Stilgar Relsik Create a rule to copy text from an email and paste it in the subject line. Using Outlook 1
R Macro to copy email to excel - Runtime Error 91 Object Variable Not Set Outlook VBA and Custom Forms 11
Diane Poremsky Use a macro to copy data in Outlook email to Excel workbook Using Outlook 0
G VBA Copy draft email to a new email - attachments not copided Using Outlook 7
C Copy email to excel runtime error 5020 Using Outlook 5
I Copy email from folder to folder - FAILS Using Outlook 5
Q Why can't I copy image with embedded hyperlink from email to Word Using Outlook 0
Diane Poremsky Use a macro to copy data in Outlook email to Excel workbook Using Outlook 0
L Copy email body fields to excel Using Outlook 0
C Copy Cell value from Excel and paste into current email Outlook VBA and Custom Forms 10
D How to copy yahoo email folders to hard drive Using Outlook 2
J Copy email from preview pane with headers Outlook VBA and Custom Forms 4
Mary B VBscript: Need to copy every email to a folder & mark that copy as read Outlook VBA and Custom Forms 5
Mary B Outlook 2013: Rule for copying new email to folder & marking that copy as read Using Outlook 1
S VBA to identify a specific email and copy the attachment from it to the network Outlook VBA and Custom Forms 4
L Outlook 2007 Copy Email Address in To Field Using Outlook 11
Aussie Looking for Outlook macro to Copy Recipient Names into Email Body Outlook VBA and Custom Forms 3
A Macro to copy email body to new email Outlook VBA and Custom Forms 5
P Copy email address from outlook 2013/2010 Using Outlook 1
M Copy "To" Address Value from a Newly Created Email to the "To" Address Field of Custom Form Using Outlook 4
R Macro to copy email address Using Outlook 6
A Copying cc email addresses to Excel - does not copy the <joe.bloggs@isp.com> Using Outlook 1
R how to copy a list of email contacts and paste them only as names (not names + email address) Using Outlook 12
B Mark copy of sent email as read - VBA? Outlook VBA and Custom Forms 3
R Sending a copy of an incoming email to a network folder Outlook VBA and Custom Forms 1
K Copy Entire Email Content - Paste into new Task Outlook VBA and Custom Forms 2
S Using copy paste to grab email addresses from the TO: address fiel Using Outlook 14
C Copy from one Profile to another Using Outlook 0
C Outlook 365 Copy/Save Emails in Folder Outside Outlook to Show Date Sender Recipient Subject in Header Using Outlook 0
D Copy Appointment Body to Task Body Outlook VBA and Custom Forms 0
M copy field value to custom field Outlook VBA and Custom Forms 0
O In Agenda-view - How to copy an existing item months ahead or back? Using Outlook 0
C Move or copy from field to field Outlook VBA and Custom Forms 0
O Outlook 365 - How to create / copy a new contact from an existing one? Using Outlook 5
S Copy Tasks/Reminders from Shared Mailbox to Personal Tasks/Reminders Outlook VBA and Custom Forms 0
A Cannot copy this folder because it may contain private items Using Outlook 0
C Copy Move item won't work Outlook VBA and Custom Forms 2
Commodore Move turns into "copy" Using Outlook 3
C Copy Outlook contact field value to another field Outlook VBA and Custom Forms 1
J Copy to calendar function no longer working in outlook 365 Using Outlook 5
F Copy and replace not update contact in another pst Using Outlook 0
B Outlook Business Contact Manager with SQL to Excel, User Defined Fields in BCM don't sync in SQL. Can I use VBA code to copy 1 field to another? BCM (Business Contact Manager) 0
Commodore Folders always closed in move/copy items dialog box Using Outlook 3

Similar threads

Back
Top