simple outlook VBA help

Associate
Joined
8 Mar 2007
Posts
2,176
Location
between here and there
hey guys,

I have a camera at work that records for 30sec whenever it detects movement and then sends me an email.

The problem is that during the day it sends me about 500 mails that i really don't need and there's no way to time the motion detection on the camera it's self.

So, I've written the code below but I'm having issue moving the mail to a different folder once completed.

Code:
Public WithEvents myOlItems As Outlook.Items

Public Sub Application_Startup()

   ' Reference the items in the Inbox. Because myOlItems is declared
   ' "WithEvents" the ItemAdd event will fire below.
   Set myOlItems = Outlook.Session.GetDefaultFolder(olFolderInbox).Items

End Sub


Private Sub myOlItems_ItemAdd(ByVal Item As Object)

    MsgBox "mail has arrived"

   ' If it's currently not between 9:00 A.M. and 5:00 P.M.
   If Time() > #7:30:00 AM# Or Time() < #5:30:00 PM# Then
   
        MsgBox "mail arrived within office hours"

      ' Check to make sure it is an Outlook mail message, otherwise
      ' subsequent code will probably fail depending on what type
      ' of item it is.
      If TypeName(Item) = "MailItem" Then
      
            MsgBox "testing to see if mail is from camera"
        
            If Item.SenderEmailAddress = "[email protected]" Then
            
            MsgBox "mail from [email protected]"
            
            myfolder = "Back Door Camera"

         ' Move message to folder and mark as read.
         MailItem.Move myfolder
         
         
         MailItem.markas Read
   
            End If

      End If

   End If

MsgBox "end of VBA script"

End Sub

Can anyone tell me how to get get the mails moved to a different folder and mark it as read??

I've tried googling bu it hasn't helped.

Cheers in advance!!
 
In order to use folders other than the default I think you need to bind to the folder

Add this function into your code:

Code:
Public Function GetFolder(strFolderPath As String) As MAPIFolder
  ' strFolderPath needs to be something like
  '   "Public Folders\All Public Folders\Company\Sales" or
  '   "Personal Folders\Inbox\My Folder"

  Dim objApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Dim colFolders As Outlook.Folders
  Dim objFolder As Outlook.MAPIFolder
  Dim arrFolders() As String
  Dim I As Long
  On Error Resume Next

  strFolderPath = Replace(strFolderPath, "/", "\")
  arrFolders() = Split(strFolderPath, "\")
  Set objApp = Application
  Set objNS = objApp.GetNamespace("MAPI")
  Set objFolder = objNS.Folders.Item(arrFolders(0))
  If Not objFolder Is Nothing Then
    For I = 1 To UBound(arrFolders)
      Set colFolders = objFolder.Folders
      Set objFolder = Nothing
      Set objFolder = colFolders.Item(arrFolders(I))
      If objFolder Is Nothing Then
        Exit For
      End If
    Next
  End If

  Set GetFolder = objFolder
  Set colFolders = Nothing
  Set objNS = Nothing
  Set objApp = Nothing
End Function

Then in your code you can use this function to work with any folder you want:

Code:
Note:

'The path argument needs to be in quotation marks and exactly match the folder hierarchy that the user sees in the Folder List. An easy way to see the path string is to display the Web toolbar (View | Toolbars) and look in its Address box.
'Omit the "Outlook:\\" prefix if you copy the path string for use with GetFolder().

    Set SrcFolder = GetFolder("Mailbox - Display Name\Inbox")
    Set DestFolder = GetFolder("Mailbox - Display Name\Inbox\Subfolder")

The code to move an item to a folder would then just be Item.Move DestFolder

By teh way I highly suggest you mark the item as read just before you move it :)

I assume your message boxes are verifying that the code is generally solid it's just the move command that fails?
 
Back
Top Bottom