MailItem.move

  • Thread starter Thread starter James H
  • Start date Start date
J

James H

I have another question also. I wanted to use a macro to
scan a folder for messages older than four days and move
them to another folder (unfortunately, the rules wizard
doesn't appear to support dynamic dates). In the macro
pasted below, it chokes on .Move(myOldComm) with a type
mismatch error. I don't understand. The receiver of the
message is a MailItem, and myOldComm is a MAPIFolder.

Thanks for your advice.

Sub MoveCaseComms()
Dim App As Object, NS As Object, myInbox As
MAPIFolder, myNewComm As MAPIFolder, myOldComm As
MAPIFolder

' x = MsgBox("This macro is not yet functional.",
vbOKOnly)
' Exit Sub

Set App = CreateObject("Outlook.Application")
Set NS = App.GetNamespace("MAPI")

Set myInbox = NS.GetDefaultFolder(olFolderInbox) '
folderid should be olfolderdeleteditems, ...inbox, etc.

Set myNewComm = myInbox.Folders("New case
communications")
Set myOldComm = myInbox.Folders("Old communications
(open cases)")

For i = 1 To myNewComm.Items.Count
' On Error GoTo EndTask
' Debug.Print i, mynewcomm.Items(i).CreationTime
With myNewComm.Items(i)
If Date - .CreationTime > 4 Then
.Move (myOldComm)
i = i - 1 ' otherwise we'll skip some
End If
End With
GoTo DoNext
EndTask:
Exit Sub
DoNext:
Next i

End Sub
 
James, I don't think you can use an if statement within a
with loop. I have used the following with success:

set MyItems = myNewComm.Items
for each MyMsg in MyItems
If Date - MyMsg.CreationTime > 4 Then MyMsg.Move
(myOldComm)
next

Do the names of your folders match the code?

Regards

Mark
 
Tried your alternative -- now the Move method throws
an "Object required" error. In the locals window I can
verify that myOldComm is a MAPIFolder with the expected
item count, and that MyMsg is a MailItem.

I don't understand why this isn't working.

The macro in its current state:

Sub MoveCaseComms()
Dim App As Object, NS As Object, myInbox As
MAPIFolder, myNewComm As MAPIFolder, myOldComm As
MAPIFolder
Dim newItems As Variant, oneItem As MailItem

Set App = CreateObject("Outlook.Application")
Set NS = App.GetNamespace("MAPI")

Set myInbox = NS.GetDefaultFolder(olFolderInbox)
Set myNewComm = myInbox.Folders("New case
communications")
Set myOldComm = myInbox.Folders("Old communications
(open cases)")
Set newItems = myNewComm.Items

For Each oneItem In newItems
If Date - oneItem.CreationTime <> 0 Then
oneItem.Move (myOldComm)
Next

End Sub
 
An If statement can certainly be used within any type of loop.

Move behaves differently depending on whether you are using the
Outlook editor or Word as the editor.

It's also a function and returns a reference to the new item. In
addition, if moving or deleting items in a collection use a count down
loop or the loop counter gets confused:
For i = Items.Count To 1 Step -1
 
Back
Top