Novice needs help with an error.

  • Thread starter Thread starter Steve Roberts
  • Start date Start date
S

Steve Roberts

When I run the code below I receive the following error. ( I realize that
the code is very sloppy. I have copied pieces from several programming
examples and have not gone back to make everything uniform and give credits)

"The operation failed. An Object could not be found"

The Debug Stops at: olTempItem.Move myfolder.Folders(olNewFolder.Name).

The idea is that you select the folder you want to archive and the program
will create a .pst file for each subfolder and move items older than X to
the folder.

Thanks in advance for any suggestions you may have.

Steve

Sub ArchivePublicFolders()

Dim olNewFolder As Outlook.MAPIFolder
Dim olTempItem As Object
Dim myNS As Outlook.NameSpace
Dim myOlApp As New Outlook.Application
Dim CurrentFolder As Outlook.MAPIFolder
Dim olApp As Outlook.Application
Dim olSession As Outlook.NameSpace
Dim olStartFolder As Outlook.MAPIFolder


Set olApp = Application
Set olSession = olApp.GetNamespace("MAPI")
Set CurrentFolder = olSession.PickFolder
Set olNewFolder = CurrentFolder
Set olTempItem = CurrentFolder.Items
Set myOlApp = CreateObject("Outlook.Application")
Set MyNameSpace = myOlApp.GetNamespace("MAPI")
Set myfolder = MyNameSpace.GetDefaultFolder(olFolderInbox)

Set myRestrictItems = olTempItem.Restrict("[ReceivedTime] <
'11/30/2003'")

MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst" 'Add or
Connect to .pst file
Set objFolder = MyNameSpace.Folders.GetLast 'Set objFolder to last
folder
objFolder.Name = olNewFolder.Name ' change the name that the user
sees in outlook


For Each olTempItem In myRestrictItems
olTempItem.Move myfolder.Folders(olNewFolder.Name)
Next

For Each olNewFolder In CurrentFolder.Folders
If olNewFolder.Name <> "Deleted Items" Then
ProcessFolder olNewFolder
MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst"
End If
' MsgBox "sub folder change"
MsgBox olNewFolder.Name
Next
End Sub
 
Try something like:

set fldNew = myfolder.Folders(olNewFolder.Name)
For Each olTempItem In myRestrictItems
olTempItem.Move (fldNew)
Next
 
Don't try to move or delete items in a For Each loop. You'll move only half of them. You can use a countdown loop instead:

intCount = myRestrictItems.Count
For i = intCount to 1 Step -1
Set olTempItem = myRestrictItems(i)
Set objMovedItem = olTempItem.Move(fldNew)
Next

Steve, how are you instantiating myFolder and olNewFolder?
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers



Jeff Rush said:
Try something like:

set fldNew = myfolder.Folders(olNewFolder.Name)
For Each olTempItem In myRestrictItems
olTempItem.Move (fldNew)
Next


Steve Roberts said:
When I run the code below I receive the following error. ( I realize that
the code is very sloppy. I have copied pieces from several programming
examples and have not gone back to make everything uniform and give credits)

"The operation failed. An Object could not be found"

The Debug Stops at: olTempItem.Move myfolder.Folders(olNewFolder.Name).

The idea is that you select the folder you want to archive and the program
will create a .pst file for each subfolder and move items older than X to
the folder.

Thanks in advance for any suggestions you may have.

Steve

Sub ArchivePublicFolders()

Dim olNewFolder As Outlook.MAPIFolder
Dim olTempItem As Object
Dim myNS As Outlook.NameSpace
Dim myOlApp As New Outlook.Application
Dim CurrentFolder As Outlook.MAPIFolder
Dim olApp As Outlook.Application
Dim olSession As Outlook.NameSpace
Dim olStartFolder As Outlook.MAPIFolder


Set olApp = Application
Set olSession = olApp.GetNamespace("MAPI")
Set CurrentFolder = olSession.PickFolder
Set olNewFolder = CurrentFolder
Set olTempItem = CurrentFolder.Items
Set myOlApp = CreateObject("Outlook.Application")
Set MyNameSpace = myOlApp.GetNamespace("MAPI")
Set myfolder = MyNameSpace.GetDefaultFolder(olFolderInbox)

Set myRestrictItems = olTempItem.Restrict("[ReceivedTime] <
'11/30/2003'")

MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst" 'Add or
Connect to .pst file
Set objFolder = MyNameSpace.Folders.GetLast 'Set objFolder to last
folder
objFolder.Name = olNewFolder.Name ' change the name that the user
sees in outlook


For Each olTempItem In myRestrictItems
olTempItem.Move myfolder.Folders(olNewFolder.Name)
Next

For Each olNewFolder In CurrentFolder.Folders
If olNewFolder.Name <> "Deleted Items" Then
ProcessFolder olNewFolder
MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst"
End If
' MsgBox "sub folder change"
MsgBox olNewFolder.Name
Next
End Sub
 
It now gives the same error but on the line: set fldNew =
myfolder.Folders(olNewFolder.Name) from your code. Is it possible that the
myfolder piece from my code is setup wrong?

Thanks

Steve

Jeff Rush said:
Try something like:

set fldNew = myfolder.Folders(olNewFolder.Name)
For Each olTempItem In myRestrictItems
olTempItem.Move (fldNew)
Next


Steve Roberts said:
When I run the code below I receive the following error. ( I realize that
the code is very sloppy. I have copied pieces from several programming
examples and have not gone back to make everything uniform and give credits)

"The operation failed. An Object could not be found"

The Debug Stops at: olTempItem.Move myfolder.Folders(olNewFolder.Name).

The idea is that you select the folder you want to archive and the program
will create a .pst file for each subfolder and move items older than X to
the folder.

Thanks in advance for any suggestions you may have.

Steve

Sub ArchivePublicFolders()

Dim olNewFolder As Outlook.MAPIFolder
Dim olTempItem As Object
Dim myNS As Outlook.NameSpace
Dim myOlApp As New Outlook.Application
Dim CurrentFolder As Outlook.MAPIFolder
Dim olApp As Outlook.Application
Dim olSession As Outlook.NameSpace
Dim olStartFolder As Outlook.MAPIFolder


Set olApp = Application
Set olSession = olApp.GetNamespace("MAPI")
Set CurrentFolder = olSession.PickFolder
Set olNewFolder = CurrentFolder
Set olTempItem = CurrentFolder.Items
Set myOlApp = CreateObject("Outlook.Application")
Set MyNameSpace = myOlApp.GetNamespace("MAPI")
Set myfolder = MyNameSpace.GetDefaultFolder(olFolderInbox)

Set myRestrictItems = olTempItem.Restrict("[ReceivedTime] <
'11/30/2003'")

MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst" 'Add or
Connect to .pst file
Set objFolder = MyNameSpace.Folders.GetLast 'Set objFolder to last
folder
objFolder.Name = olNewFolder.Name ' change the name that the user
sees in outlook


For Each olTempItem In myRestrictItems
olTempItem.Move myfolder.Folders(olNewFolder.Name)
Next

For Each olNewFolder In CurrentFolder.Folders
If olNewFolder.Name <> "Deleted Items" Then
ProcessFolder olNewFolder
MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst"
End If
' MsgBox "sub folder change"
MsgBox olNewFolder.Name
Next
End Sub
 
Thanks For the response Sue.

Here are the Set commands as the loop is started.

Set myfolder = MyNameSpace.GetDefaultFolder(olFolderInbox)
Set olNewFolder = CurrentFolder
Set CurrentFolder = olSession.PickFolder

Further down you see the following which changes the folder and creates .pst
file with

For Each olNewFolder In CurrentFolder.Folders
If olNewFolder.Name <> "Deleted Items" Then
ProcessFolder olNewFolder
MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst"
End If
Next


Don't try to move or delete items in a For Each loop. You'll move only half
of them. You can use a countdown loop instead:

intCount = myRestrictItems.Count
For i = intCount to 1 Step -1
Set olTempItem = myRestrictItems(i)
Set objMovedItem = olTempItem.Move(fldNew)
Next

Steve, how are you instantiating myFolder and olNewFolder?
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers



Jeff Rush said:
Try something like:

set fldNew = myfolder.Folders(olNewFolder.Name)
For Each olTempItem In myRestrictItems
olTempItem.Move (fldNew)
Next


Steve Roberts said:
When I run the code below I receive the following error. ( I realize that
the code is very sloppy. I have copied pieces from several programming
examples and have not gone back to make everything uniform and give credits)

"The operation failed. An Object could not be found"

The Debug Stops at: olTempItem.Move myfolder.Folders(olNewFolder.Name).

The idea is that you select the folder you want to archive and the program
will create a .pst file for each subfolder and move items older than X to
the folder.

Thanks in advance for any suggestions you may have.

Steve

Sub ArchivePublicFolders()

Dim olNewFolder As Outlook.MAPIFolder
Dim olTempItem As Object
Dim myNS As Outlook.NameSpace
Dim myOlApp As New Outlook.Application
Dim CurrentFolder As Outlook.MAPIFolder
Dim olApp As Outlook.Application
Dim olSession As Outlook.NameSpace
Dim olStartFolder As Outlook.MAPIFolder


Set olApp = Application
Set olSession = olApp.GetNamespace("MAPI")
Set CurrentFolder = olSession.PickFolder
Set olNewFolder = CurrentFolder
Set olTempItem = CurrentFolder.Items
Set myOlApp = CreateObject("Outlook.Application")
Set MyNameSpace = myOlApp.GetNamespace("MAPI")
Set myfolder = MyNameSpace.GetDefaultFolder(olFolderInbox)

Set myRestrictItems = olTempItem.Restrict("[ReceivedTime] <
'11/30/2003'")

MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst" 'Add or
Connect to .pst file
Set objFolder = MyNameSpace.Folders.GetLast 'Set objFolder to last
folder
objFolder.Name = olNewFolder.Name ' change the name that the user
sees in outlook


For Each olTempItem In myRestrictItems
olTempItem.Move myfolder.Folders(olNewFolder.Name)
Next

For Each olNewFolder In CurrentFolder.Folders
If olNewFolder.Name <> "Deleted Items" Then
ProcessFolder olNewFolder
MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst"
End If
' MsgBox "sub folder change"
MsgBox olNewFolder.Name
Next
End Sub
 
This is the problem statement, right?

olTempItem.Move myfolder.Folders(olNewFolder.Name)

The way you have it written, it's impossible to know whether the problem is that there's no olTempItem to be moved or whether there's no folder to move it to. You should always get each Outlook object separately and test for its existence before you use it.

My guess is that there is no such folder. myfolder appears to be the Inbox, and I don't know that there's any reason to expect that there's a folder by that name in the Inbox.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers


Steve Roberts said:
Thanks For the response Sue.

Here are the Set commands as the loop is started.

Set myfolder = MyNameSpace.GetDefaultFolder(olFolderInbox)
Set olNewFolder = CurrentFolder
Set CurrentFolder = olSession.PickFolder

Further down you see the following which changes the folder and creates .pst
file with

For Each olNewFolder In CurrentFolder.Folders
If olNewFolder.Name <> "Deleted Items" Then
ProcessFolder olNewFolder
MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst"
End If
Next
Steve Roberts said:
When I run the code below I receive the following error. ( I realize that
the code is very sloppy. I have copied pieces from several programming
examples and have not gone back to make everything uniform and give credits)

"The operation failed. An Object could not be found"

The Debug Stops at: olTempItem.Move myfolder.Folders(olNewFolder.Name).

The idea is that you select the folder you want to archive and the program
will create a .pst file for each subfolder and move items older than X to
the folder.

Thanks in advance for any suggestions you may have.

Steve

Sub ArchivePublicFolders()

Dim olNewFolder As Outlook.MAPIFolder
Dim olTempItem As Object
Dim myNS As Outlook.NameSpace
Dim myOlApp As New Outlook.Application
Dim CurrentFolder As Outlook.MAPIFolder
Dim olApp As Outlook.Application
Dim olSession As Outlook.NameSpace
Dim olStartFolder As Outlook.MAPIFolder


Set olApp = Application
Set olSession = olApp.GetNamespace("MAPI")
Set CurrentFolder = olSession.PickFolder
Set olNewFolder = CurrentFolder
Set olTempItem = CurrentFolder.Items
Set myOlApp = CreateObject("Outlook.Application")
Set MyNameSpace = myOlApp.GetNamespace("MAPI")
Set myfolder = MyNameSpace.GetDefaultFolder(olFolderInbox)

Set myRestrictItems = olTempItem.Restrict("[ReceivedTime] <
'11/30/2003'")

MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst" 'Add or
Connect to .pst file
Set objFolder = MyNameSpace.Folders.GetLast 'Set objFolder to last
folder
objFolder.Name = olNewFolder.Name ' change the name that the user
sees in outlook


For Each olTempItem In myRestrictItems
olTempItem.Move myfolder.Folders(olNewFolder.Name)
Next

For Each olNewFolder In CurrentFolder.Folders
If olNewFolder.Name <> "Deleted Items" Then
ProcessFolder olNewFolder
MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst"
End If
' MsgBox "sub folder change"
MsgBox olNewFolder.Name
Next
End Sub
 
Sue,

You were right. When I stepped through the entire process I noticed a number
of mistakes and items that were not referenced correctly.

This is what I came up with so far. I need to clean it up but it works
pretty well.

Thanks for the help.

Steve

#######################################################
Sub ArchivePublicFolders()

Dim olNewFolder As Outlook.MAPIFolder
Dim olTempItem As Object
Dim CurrentFolder As Outlook.MAPIFolder
Dim olApp As Outlook.Application
Dim MyNameSpace As Outlook.NameSpace
Dim fldNew As Outlook.MAPIFolder

Set olApp = CreateObject("Outlook.Application")
Set MyNameSpace = olApp.GetNamespace("MAPI")
Set CurrentFolder = MyNameSpace.PickFolder
Set olNewFolder = CurrentFolder
MsgBox CurrentFolder.FolderPath


For Each olNewFolder In CurrentFolder.Folders
If olNewFolder.Name <> "Deleted Items" Then
MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst"
Set objFolder = MyNameSpace.Folders.GetLast 'Set objFolder to last
folder
objFolder.Name = olNewFolder.Name ' change the name that the user
sees in outlook

MsgBox (olNewFolder.Name)

ProcessFolder olNewFolder 'Call Process Folder to actually perform
the move

End If

Next
End Sub
########################################################
Sub ProcessFolder(CurrentFolder As Outlook.MAPIFolder)
Dim i As Long
Dim olNewFolder As Outlook.MAPIFolder
Dim objFolder As Outlook.MAPIFolder
Dim olTempItem As Object
Dim myNS As Outlook.NameSpace
Dim myOlApp As New Outlook.Application
Dim fldNew As Outlook.MAPIFolder

Set olNewFolder = CurrentFolder
Set olTempItem = CurrentFolder.Items

MsgBox "Process folder" & CurrentFolder
Set myOlApp = CreateObject("Outlook.Application")
Set MyNameSpace = myOlApp.GetNamespace("MAPI")
Set myNS = myOlApp.GetNamespace("MAPI")
Set myFolder = MyNameSpace.GetDefaultFolder(olFolderInbox)
Set myRestrictItems = olTempItem.Restrict("[ReceivedTime] < '11/30/2003'")
Set fldNew = myNS.Folders(CurrentFolder.Name)

MsgBox fldNew
intCount = myRestrictItems.Count
For i = intCount To 1 Step -1
Set olTempItem = myRestrictItems(i)
MsgBox fldNew
Set objMovedItem = olTempItem.Move(fldNew)
Next
End Sub

This is the problem statement, right?

olTempItem.Move myfolder.Folders(olNewFolder.Name)

The way you have it written, it's impossible to know whether the problem is
that there's no olTempItem to be moved or whether there's no folder to move
it to. You should always get each Outlook object separately and test for its
existence before you use it.

My guess is that there is no such folder. myfolder appears to be the Inbox,
and I don't know that there's any reason to expect that there's a folder by
that name in the Inbox.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers


Steve Roberts said:
Thanks For the response Sue.

Here are the Set commands as the loop is started.

Set myfolder = MyNameSpace.GetDefaultFolder(olFolderInbox)
Set olNewFolder = CurrentFolder
Set CurrentFolder = olSession.PickFolder

Further down you see the following which changes the folder and creates ..pst
file with

For Each olNewFolder In CurrentFolder.Folders
If olNewFolder.Name <> "Deleted Items" Then
ProcessFolder olNewFolder
MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst"
End If
Next
Steve Roberts said:
When I run the code below I receive the following error. ( I realize that
the code is very sloppy. I have copied pieces from several programming
examples and have not gone back to make everything uniform and give credits)

"The operation failed. An Object could not be found"

The Debug Stops at: olTempItem.Move myfolder.Folders(olNewFolder.Name).

The idea is that you select the folder you want to archive and the program
will create a .pst file for each subfolder and move items older than X to
the folder.

Thanks in advance for any suggestions you may have.

Steve

Sub ArchivePublicFolders()

Dim olNewFolder As Outlook.MAPIFolder
Dim olTempItem As Object
Dim myNS As Outlook.NameSpace
Dim myOlApp As New Outlook.Application
Dim CurrentFolder As Outlook.MAPIFolder
Dim olApp As Outlook.Application
Dim olSession As Outlook.NameSpace
Dim olStartFolder As Outlook.MAPIFolder


Set olApp = Application
Set olSession = olApp.GetNamespace("MAPI")
Set CurrentFolder = olSession.PickFolder
Set olNewFolder = CurrentFolder
Set olTempItem = CurrentFolder.Items
Set myOlApp = CreateObject("Outlook.Application")
Set MyNameSpace = myOlApp.GetNamespace("MAPI")
Set myfolder = MyNameSpace.GetDefaultFolder(olFolderInbox)

Set myRestrictItems = olTempItem.Restrict("[ReceivedTime] <
'11/30/2003'")

MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst" 'Add or
Connect to .pst file
Set objFolder = MyNameSpace.Folders.GetLast 'Set objFolder to last
folder
objFolder.Name = olNewFolder.Name ' change the name that the user
sees in outlook


For Each olTempItem In myRestrictItems
olTempItem.Move myfolder.Folders(olNewFolder.Name)
Next

For Each olNewFolder In CurrentFolder.Folders
If olNewFolder.Name <> "Deleted Items" Then
ProcessFolder olNewFolder
MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst"
End If
' MsgBox "sub folder change"
MsgBox olNewFolder.Name
Next
End Sub
 
Opps! one last bug! The following code runs some of the time, maybe 3-5 sub
folders worth. Then I notice that the folder name in outlook stops changing
from Personal folder to Whatever the current folder name should be. When I
look at the .pst folder properties it shows the correct name but what is
displayed is still personal folder. After I close and reopen outlook the
folder has the correct name. It looks like it just is not refreshing the
folder name after it gets so far into the tree. Is there a way to force a
refresh of the name?

MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst" 'add a
..pst file with the name of the current folder
Set objFolder = MyNameSpace.Folders.GetLast 'Set
objFolder to last folder
objFolder.Name = olNewFolder.Name 'Change the last folder
display name to the same as the name of the .pst file


This is the problem statement, right?

olTempItem.Move myfolder.Folders(olNewFolder.Name)

The way you have it written, it's impossible to know whether the problem is
that there's no olTempItem to be moved or whether there's no folder to move
it to. You should always get each Outlook object separately and test for its
existence before you use it.

My guess is that there is no such folder. myfolder appears to be the Inbox,
and I don't know that there's any reason to expect that there's a folder by
that name in the Inbox.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers


Steve Roberts said:
Thanks For the response Sue.

Here are the Set commands as the loop is started.

Set myfolder = MyNameSpace.GetDefaultFolder(olFolderInbox)
Set olNewFolder = CurrentFolder
Set CurrentFolder = olSession.PickFolder

Further down you see the following which changes the folder and creates ..pst
file with

For Each olNewFolder In CurrentFolder.Folders
If olNewFolder.Name <> "Deleted Items" Then
ProcessFolder olNewFolder
MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst"
End If
Next
Steve Roberts said:
When I run the code below I receive the following error. ( I realize that
the code is very sloppy. I have copied pieces from several programming
examples and have not gone back to make everything uniform and give credits)

"The operation failed. An Object could not be found"

The Debug Stops at: olTempItem.Move myfolder.Folders(olNewFolder.Name).

The idea is that you select the folder you want to archive and the program
will create a .pst file for each subfolder and move items older than X to
the folder.

Thanks in advance for any suggestions you may have.

Steve

Sub ArchivePublicFolders()

Dim olNewFolder As Outlook.MAPIFolder
Dim olTempItem As Object
Dim myNS As Outlook.NameSpace
Dim myOlApp As New Outlook.Application
Dim CurrentFolder As Outlook.MAPIFolder
Dim olApp As Outlook.Application
Dim olSession As Outlook.NameSpace
Dim olStartFolder As Outlook.MAPIFolder


Set olApp = Application
Set olSession = olApp.GetNamespace("MAPI")
Set CurrentFolder = olSession.PickFolder
Set olNewFolder = CurrentFolder
Set olTempItem = CurrentFolder.Items
Set myOlApp = CreateObject("Outlook.Application")
Set MyNameSpace = myOlApp.GetNamespace("MAPI")
Set myfolder = MyNameSpace.GetDefaultFolder(olFolderInbox)

Set myRestrictItems = olTempItem.Restrict("[ReceivedTime] <
'11/30/2003'")

MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst" 'Add or
Connect to .pst file
Set objFolder = MyNameSpace.Folders.GetLast 'Set objFolder to last
folder
objFolder.Name = olNewFolder.Name ' change the name that the user
sees in outlook


For Each olTempItem In myRestrictItems
olTempItem.Move myfolder.Folders(olNewFolder.Name)
Next

For Each olNewFolder In CurrentFolder.Folders
If olNewFolder.Name <> "Deleted Items" Then
ProcessFolder olNewFolder
MyNameSpace.AddStore "c:\" & olNewFolder.Name & ".pst"
End If
' MsgBox "sub folder change"
MsgBox olNewFolder.Name
Next
End Sub
 
Back
Top