Calendar Items - Copying enmasse to another folder via VBA

A

Alan

Hi All,

I am trying to write some code that will make a copy of all extant
calendar items in a given folder to another calendar folder.

I have the code below, but Outlook doesn't seem to want to allow me to
move the collection of items to the new folder (I have added a comment
in the code just above the offending line).

Is there a simple way around this?


As an aside, am I correct in deleting the first indexed item
repeatedly in order to clear the destination calendar before
re-populating? My reasoning is that if there are 10 items in there to
start with, after deleting the first, there are only nine (indexed 1
to 9) so the code would fall over halfway through the loop if I didn't
do it that way?

Thanks,

Alan.


+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Sub CopyMainCalItemsToExchangeFolder()

Dim FolderCollection As Folders
Dim AlanMainMailFileFolders As Folders
Dim AlanCalendarItems As Items

Dim AlanExchangeFolders As Folders
Dim AlanExchangeCalendarItems As Items


Set FolderCollection = Application.GetNamespace("MAPI").Folders
Set AlanMainMailFileFolders = FolderCollection("Alan - Main Mail
File").Folders
Set AlanCalendarItems = AlanMainMailFileFolders("Calendar").Items

Set AlanExchangeFolders = FolderCollection("Mailbox - Alan
Figgins").Folders
Set AlanExchangeCalendarItems = AlanExchangeFolders("Calendar").Items

TotalItems = AlanExchangeCalendarItems.Count

For Counter = 1 To TotalItems

AlanExchangeCalendarItems(1).Delete

Next Counter




Set CopiedItems = AlanCalendarItems


' This is the line that causes the problem, with the collection of
items not supporting the move method

CopiedItems.Move AlanExchangeFolders("Calendar")

End Sub
 
S

Sue Mosher [MVP-Outlook]

You must copy each item individually:

Set objTarget = AlanExchangeFolders("Calendar")
For Each objItem in AlanCalendarItems
Set objCopy = objItem.Copy
objCopy.Move objTarget
Set objCopy = Nothing
Next

If you want to clear the destination calendar first (not sure why you want
to do that), try using a countdown loop:

TotalItems = AlanExchangeCalendarItems.Count
For Counter = TotalItems To 1 Step -1
Set objItem = AlanExchangeCalendarItems(Counter)
objItem.Delete
Next
 
A

Alan

Sue Mosher said:
You must copy each item individually:

Set objTarget = AlanExchangeFolders("Calendar")
For Each objItem in AlanCalendarItems
Set objCopy = objItem.Copy
objCopy.Move objTarget
Set objCopy = Nothing
Next

Hi Sue,

Sorry to bother you again, but it still isn't working for me.

When I run the code, I get an error on the line that reads
"ObjCopy.Move ObjTarget".

The error is as follows:

----------------

Run Time Error '-2147024809 (80070057)':

Could not complete the operation. One or more parameter values are
not valid.

----------------

This seems to me to imply that the destination calendar folder cannot
accept one of the items in the source calendar folder? But if so, how
can that be?
If you want to clear the destination calendar first (not sure why
you want to do that), try using a countdown loop:

TotalItems = AlanExchangeCalendarItems.Count
For Counter = TotalItems To 1 Step -1
Set objItem = AlanExchangeCalendarItems(Counter)
objItem.Delete
Next

I was figuring that if I did not clear out all the existing items,
then I would end up with duplicate entries when the code runs?


Thanks again for your support and assistance,

Alan.
 
S

Sue Mosher [MVP-Outlook]

You can test that you have a valid folder:

If Not objTarget Is Nothing Then
' continue

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
A

Alan

Sue Mosher said:
You can test that you have a valid folder:

If Not objTarget Is Nothing Then
' continue

Hi Sue,

I added that line (and an End If) and it passed the test, but still
bombed out as before.

When it stops, I hit 'debug' and hovered the mouse over ObjTarget
which = "Calendar" (as expected).

I also hovered over ObjCopy and that = "Mr X - Catch Up" (name changed
to protect the innocent).

Therefore, it *appears* that the destination folder and the item are
both valid.

In the locals window at that point, ObjTarget is shown as
"Variant/Object/MAPIFolder" and its parent name is, as expected,
"Mailbox - Alan Figgins", so it definately seems to have the correct
calendar folder.

It's class is "olFolder" and the DefaultItemType is
"olAppointmentItem"


Now, interestingly, when I tried to check the ObjCopy in the locals
window, something odd happened.

It was collapsed, so I clicked on the + button to expand the object
and see the details, and immediately I got the Outlook security
dialogue box warning me that:

A program is trying to access e-mail addresses you have stored in
Outlook. Do you want to allow this?

I clicked 'Yes' and then it returned me to the locals window with an
expanded ObjCopy.


At that point everything appears okay. ObjCopy class =
"olAppointment".

The only possibility under there seems to be a line that reads:

MAPIOBJECT <Unsupported object type>



Any ideas what could be the problem?

Thanks for your continuing support,

Alan.
 
S

Sue Mosher [MVP-Outlook]

How odd. Do you have write access to the destination folder? I'd test your
code here, but you didn't include it in your latest message.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
A

Alan

Sue Mosher said:
How odd. Do you have write access to the destination folder? I'd
test your code here, but you didn't include it in your latest
message.

Hi Sue,

Yes - I definately have write access to that destination folder
(confirmed manually both by creating a new appointment and dragging
and dropping an item from the source folder used in the code. Is it
possible that I could have write access manually but not in VBA?

I have posted the full code as it now exists just below.

Thanks,

Alan.


+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Sub Copy_Main_Cal_Items_To_Exchange()

' Declarations

Dim FolderCollection As Folders
Dim AlanMainMailFileFolders As Folders
Dim AlanCalendarItems As Items

Dim AlanExchangeFolders As Folders
Dim AlanExchangeCalendarItems As Items


' Set up objects to be used

Set FolderCollection = Application.GetNamespace("MAPI").Folders
Set AlanMainMailFileFolders = FolderCollection("Alan - Main Mail
File").Folders
Set AlanCalendarItems = AlanMainMailFileFolders("Calendar").Items

Set AlanExchangeFolders = FolderCollection("Mailbox - Alan
Figgins").Folders
Set AlanExchangeCalendarItems = AlanExchangeFolders("Calendar").Items


' Clear out any existing items in the exchange
' calendar folder to avoid duplication

TotalItems = AlanExchangeCalendarItems.Count

For Counter = TotalItems To 1 Step -1

AlanExchangeCalendarItems(Counter).Delete

Next Counter


' Transfer the PST calendar folder items to the
' exchange calendar folder

Set objTarget = AlanExchangeFolders("Calendar")

If Not objTarget Is Nothing Then

For Each ObjItem In AlanCalendarItems

Set ObjCopy = ObjItem.Copy

' This next line is the one that causes the error
' message (see below - bottom)

ObjCopy.Move objTarget

Set ObjCopy = Nothing

Next

End If

End Sub


+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Error Message:

Run Time Error '-2147024809 (80070057)':

Could not complete the operation. One or more parameter values are
not valid.
 
S

Sue Mosher [MVP-Outlook]

UI and VBA use the same permissions. That "parameter values" message comes
up in a number of different situations -- you'll see several articles in the
MSKB. So it's hard to know whether it's a red herring or actually relevant.

What Outlook version? If OL2003, are you using Cached Exchange mode?

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
A

Alan

Sue Mosher said:
UI and VBA use the same permissions. That "parameter values" message
comes up in a number of different situations -- you'll see several
articles in the MSKB. So it's hard to know whether it's a red
herring or actually relevant.

What Outlook version? If OL2003, are you using Cached Exchange mode?

Hi Sue,

I am running Outlook 2000 SP-3 (9.0.0.6627) in C/W mode with Security
Update.

My PC is running Win 2000 Pro SP-4 (5.00.2195)

I have all updates (per Windows Update and Office Update) installed
including critical, general, and driver updates.

The exchange server is Exchange 2000, but I am guessing that this is a
client issue.


Hope that helps!

Alan.
 
A

Alan

Re: Code error upon execution (6 April 2004 - 08:41am):


{Bump}


Please do tell me if it is better form to re-start a new thread?

Thanks,

Alan.
 
S

Sue Mosher [MVP-Outlook]

Did you check the KB? As I said, there are a number of articles regarding
that error message.
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



----- Original Message -----
From: "Alan" <[email protected]>
Newsgroups: microsoft.public.outlook.program_vba
Sent: Monday, April 05, 2004 4:41 PM
Subject: Re: Calendar Items - Copying enmasse to another folder via VBA
 
A

Alan

Sue Mosher said:
Did you check the KB? As I said, there are a number of articles
regarding that error message.

I did indeed - I should have mentioned that before for which I
apologise.

Searching on the error code, I got 25 hits, so I added "Outlook" as a
term and got 8 hits.

None of those 8 or the wider 25 seem to offer any assistance.


I then tried searching the MSKB on the following:

parameter values vba valid

That gave 25 hits, but again none seem to offer any specific
assistance.


There is a list of error codes that includes (80070057) at:

http://support.microsoft.com/default.aspx?scid=kb;en-us;186063

However, this just tells you that the code means that, "One or more
arguments are invalid".


If we then return to the code, the line that bombs out is:

ObjCopy.Move objTarget


Since ObjCopy and ObjTarget are both validated objects (as confirmed
by the locals window), it seems that the only possibilities are that:

1) It is invalid to move ObjCopy at all; OR

2) It is valid to move ObjCopy, but it is invalid to move it
into ObjTarget.


I can manually move that item into that target folder using my mouse
so neither *appears* to be the case (?!)


This is really quite strange?

Alan.
 
S

Sue Mosher [MVP-Outlook]

Did you try:

Set objMovedItem = ObjCopy.Move objTarget

Other things I'd try -- testing whether you can change a property on objCopy
and save it without errors.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
A

Alan

Sue Mosher said:
Did you try:

Set objMovedItem = ObjCopy.Move objTarget

Hi Sue,

I tried that, but it would not compile - I am guessing that perhaps it
is permissiable to apply a method to an object, but not create a
'methodised' object (if that makes any kind of sense)?
Other things I'd try -- testing whether you can change a property on
objCopy and save it without errors.

Okay - I added the following code:


For Each ObjItem In AlanTestCalendarItems

Set ObjCopy = ObjItem.Copy

ObjCopy.Text = ObjCopy.Text & "Test text appended"

ObjCopy.Save

Set ObjCopy = Nothing

Next


However, it bombs out with an 'object does not support this property
or method' error on the line:

ObjCopy.Text = ObjCopy.Text & "Test text appended"


I am guessing here that since the object is already held on the
clipboard (assuming that the concept of a clipboard is really valid in
a VBA context), that it's properties cannot be editable until the item
is re-created in some location (such as ObjTarget in my case).


Does my reasoning seem correct above?

Thank you again so very much for sticking with me through this. If
nothing else, I am having to think more clearly about certain
conceptual topics with respect to object oriented coding that I had
previously not considered.

Regards,

Alan.
 
A

Alan

Thank you again so very much for sticking with me through this. If
nothing else, I am having to think more clearly about certain
conceptual topics with respect to object oriented coding that I had
previously not considered.

Hi Again,

I know I should have tried this sooner, but believe me - you don't
need to add to my bruised forehead!

I tried running the code below, which basically tries to do
exactly the same thing, except that instead of copying items from a
PST calendar folder to an Exchange calendar folder, it copies them
from one PST calendar folder to another PST calendar folder.

That works fine exactly as intended!

Therefore, it seems that the issue must be related to the destination
target folder being on the exchange server.


Testing in parts, the code that clears out the *exchange* folder works
fine on its own, so that confirms that:

1) I have the correct destination target folder referenced on the
exchange server
2) I have permission to (at least) delete items in that folder

With respect to permissions, I can happily create, edit, delete, and
copy in items manually to the folder outside of VBA so I am fairly
sure that it is not a permissions issue per se.


Could this perhaps be a scope issue such that the item on the
'clipboard' is on a PST clipboard, that is not visible in the Exchange
context?


Thanks,

Alan.


+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Sub Copy_TestSource_Cal_Items_To_TestDestination()

' Declarations

Dim FolderCollection As Folders
Dim AlanMainMailFileFolders As Folders
Dim AlanCalendarItems As Items

Dim AlanExchangeFolders As Folders
Dim AlanExchangeCalendarItems As Items


' Set up objects to be used

Set FolderCollection = Application.GetNamespace("MAPI").Folders
Set AlanMainMailFileFolders = FolderCollection("Alan - Main Mail
File").Folders
Set AlanCalendarFolders = AlanMainMailFileFolders("Calendar").Folders

Set AlanTestSourceCalendarItems =
AlanCalendarFolders("TestSource").Items

Set AlanTestDestinationCalendarItems =
AlanCalendarFolders("TestDestination").Items


' Clear out any existing items in the TestDestination calendar

TotalItems = AlanTestDestinationCalendarItems.Count

For Counter = TotalItems To 1 Step -1

AlanTestDestinationCalendarItems(Counter).Delete

Next Counter


' Transfer the TestSource calendar items to the TestDestination
calendar

Set objTarget = AlanCalendarFolders("TestDestination")

If Not objTarget Is Nothing Then

For Each ObjItem In AlanTestSourceCalendarItems

Set ObjCopy = ObjItem.Copy

ObjCopy.Move objTarget

Set ObjCopy = Nothing

Next

End If

End Sub
 
S

Sue Mosher [MVP-Outlook]

What kind of compile error did you get? That's a perfectly legitimate use of
Move.

The other error you got means just what it says: Outlook items have no Text
property.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top