Question

I am very unfamiliar with VBA. I am out of space for rules and we do not have root access to our exchange server so I need to use a script to move emails from 22 different senders to one folder. I have found the below script and it seems to work for other's but I get a syntax error on the Sub line and can not find a way to make this work. I have read what few posts I can find on this and am really struggling understanding the syntax here.

 Sub MoveEmails()
Dim myNamespace, myInbox, myItems ', myDestFolder- NEW CHANGED MOVED TO SEPARATE      LINE BELOW
Set myNamespace = Application.GetNamespace("MAPI")
Set myInbox = myNamespace.GetDefaultFolder(6)
Set myItems = myInbox.Items
Dim myItem As Outlook.MailItem
Dim MailItem As Object
Dim sn As String

'NEW LINE BELOW
Dim myDestFolder As Folder
'here you need different kind of loop
Dim i As Integer
For i = myInbox.Items.Count To 1 Step -1   'loop goes from last to first element
sn = myInbox.Items(i).SenderName

'first possible problem
If UCase(sn) = "FLPDMINFO" Then
    Set myDestFolder = myInbox.Folders("info")

'alternatively you could check name in this way
ElseIf UCase(sn) Like "GAPDMINFO*" Then
    Set myDestFolder = myInbox.Folders("info")
ElseIf sn = "DEPDM-INFO" Then
    Set myDestFolder = myInbox.Folders("ifnfo")
End If
Set myItem = myItems.Find("[SenderName]='" & sn & "'")

'here we need to check if folder is not set
'NEW- THIS LINE IMPROVED
While TypeName(myItem) <> "Nothing" And And Not myDestFolder Is Nothing
    myItem.move myDestFolder
    Set myItem = myItems.FindNext

    i = i - 1

Wend
Set myDestFolder = Nothing
Next
End Sub

Any input would be appreciated.

Was it helpful?

Solution

You've got two 'and's towards the bottom. Change this:

While TypeName(myItem) <> "Nothing" And **And** Not myDestFolder Is Nothing

with this:

While TypeName(myItem) <> "Nothing" And Not myDestFolder Is Nothing
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top