vba problems

  • Thread starter Thread starter toocool
  • Start date Start date
T

toocool

Hi,

I'm having three problems with a macro in Outlook.

First of all it steps out of "for each" after two steps, whereas it
should do more... (depending on the number of messages)

The second thing is it works when I run it from VBA, but when I'm in
Outlook and press Alt+F8, ant choose the macro it says: sub or
function not defined, and it goes to VBA. I press F5, and it works
again...

And the third thing is I do not know how to copy/move messages to a
specified personal folder so I used PickFolder method,

I copy the code for convenience:

Public Sub przenies()
Dim theItem As MailItem
Set def = Outlook.Session.PickFolder
For Each theItem In Outlook.Session.GetDefaultFolder(olFolderDeletedItems).Items
theItem.Move def
Next theItem
End Sub

Should anyone be helpful with my problem I will be grateful,
regards, toocool
 
The only reason I can think of offhand why your macro won't run when called
is if VBA isn't starting. What are your security settings in Tools, Macro,
Security? If High then macros won't run unless they are signed, if Medium
you have to enable macros each time Outlook starts up.

When moving or deleting items in a collection never use a For Each or For
loop that counts up. Always use a down counting For loop or a Do loop that
tests some condition. Otherwise the loop counter is being messed with inside
the loop as you remove items from the collection. You will only remove 1/2
of the items in each pass through the loop.

Public Sub przenies()
Dim theItem As Outlook.MailItem
Dim i As Long
Dim oItems As Outlook.Items
Dim oFolder As Outlook.MAPIFolder
Dim def As Outlook.MAPIFolder
Dim oNS As Outlook.NameSpace
Dim oMoved As Outlook.MailItem

Set oNS = Application.Session
Set def = oNS.PickFolder
'instead of this you could use
'Set def = oNS.Folders.Item("myFolder")
' to delve down even further that could be
'Set def = oNS.Folders.Item("myFolder").Folders("mySubFolder")

Set oFolder = oNS.GetDefaultFolder(olFolderDeletedItems)
Set oItems = oFolder.Items

For i = oItems.Count To 1 Step -1
Set theItem = oItems.Item(i)
.Move is a function, not a sub
Set oMoved = theItem.Move(def)
Next i
End Sub

This code also doesn't account for items in Deleted Items not being mail
items. The code would error if a deleted appointment was hit. To avoid that
define theItem As Object and test for theItem.Class = olMail.
 
Hi,

I tried to pass the other Items (eg. appointment) problem and did what
You advised, however I get the error: object variable or With block
variable not set

Would You be so kind as to tell me where the problem could be. Here's
the new code:

Public Sub przenies()
Dim i As Long
Dim oItems As Outlook.Items
Dim oFolder As Outlook.MAPIFolder
Dim def As Outlook.MAPIFolder
Dim oNS As Outlook.NameSpace
Dim oMoved As Object
Dim theItem As Object
Set oNS = Application.Session
Set def = oNS.PickFolder
Set oFolder = oNS.GetDefaultFolder(olFolderDeletedItems)
Set oItems = oFolder.Items
For i = oItems.Count To 1 Step -1
If (theItem.Class = olMail) Then
Set theItem = oItems.Item(i)
Set oMoved = theItem.Move(def)
End If
Next i
End Sub

toocool
Thanx in advance
 
Please put some of the preceding thread in your posts, your interface isn't
doing that and it makes it very hard to follow a thread.

You can't test for theItem.Class before you instantiate theItem. Of course
you don't have an object at that point, or if you did it would be the
previous item which is now moved.

For i = oItems.Count To 1 Step -1
Set theItem = oItems.Item(i)
If (theItem.Class = olMail) Then
 
Back
Top