+ Reply to Thread
Results 1 to 4 of 4

Problem clearing variables

Hybrid View

  1. #1
    Registered User
    Join Date
    02-06-2009
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2007
    Posts
    46

    Problem clearing variables

    I have a module with a public variable set like so:
    Public Myfolder as Outlook.MAPIFolder
    When I try to clear it out after looping through my array and finding a match it doesn't clear
      For aa = LBound(aEmails) To UBound(aEmails)
        On Error Resume Next
            pos1 = Application.WorksheetFunction.Find(aEmails(aa, cName), sEmail)
            If Err <> 0 Then
                'insert code here to move items with no box found to a generic box.
                'Set oFolder = Application.GetNamespace("MAPI")
                'Set MyFolder = oFolder.Folders("Generic")
                'oMail.Move MyFolder
                Err = 0
            Else
             Set oFolder = Application.GetNamespace("MAPI")
             Set Myfolder = oFolder.Folders(aEmails(aa, cFoldr))
             
              
              oMail.Move Myfolder
              Sheets("Report").Select
              Do Until ActiveCell = ""
                ActiveCell.Offset(1, 0).Select
              Loop
              ActiveCell = sFromName
              ActiveCell.Offset(0, 1) = sSubject
              ActiveCell.Offset(0, 2) = aEmails(aa, cName)
              ActiveCell.Offset(0, 3) = aEmails(aa, cFoldr)
              ActiveCell.Offset(0, 4) = Now
              ActiveCell.Select
              Set oFolder = Empty
              Set Myfolder = Empty
              
              GoTo Endloop:
              
              
            End If
        'On Error GoTo 0
        Set oFolder = Empty
        Set Myfolder = Empty
      Next aa
      
    Endloop:
    I am coming up blank. Any help would be appreciated!

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Problem clearing variables

    Hell bobaftt,

    From what I see, this code will produce a runtime error '424' Object Required. You can not set an Object variable to Empty. You can set it to Nothing if you want to release the object's resources from memory.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    02-06-2009
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2007
    Posts
    46

    Re: Problem clearing variables

     'Option Explicit
    Option Compare Text
    Public StopTime
    
    Public dTime As Date
    Sub ReadInbox()
    Dim aEmails()
    ReDim aEmails(0)
    If StopTime = "" Or StopTime = False Then
    
    
      Set appOL = CreateObject("Outlook.Application")
      Set oSpace = appOL.GetNamespace("MAPI")
      Set oFolder = oSpace.GetDefaultFolder(olFolderInbox)
      Set oItems = oFolder.Items
      oItems.Sort "Received", True
      
      'This defines the size of the array
      Sheets("Emaillist").Activate
      lastrow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
      ReDim aEmails(LBound(aEmails) To lastrow - 1, 0 To 1)
        
      'this populates the array
      For x = 1 To lastrow
        aa = x - 1
        aEmails(aa, cName) = UCase(Cells(x, xName))
        aEmails(aa, cFoldr) = UCase(Cells(x, xFoldr))
      Next x
      
      On Error Resume Next
      'This says for every mail item call this other routine
      For Each oMail In oItems
        If oMail.Subject Like "*" Then
          Call bodyFind(oMail, aEmails)
        End If
      Next
      
        'This makes the macro run every 30 seconds
        Application.OnTime Now + TimeValue("00:00:30"), "ReadInbox"
        'Application.OnTime Now + TimeValue("00:0:30"), "Savefile"
    Else
        StopTime = ""
    End If
    End Sub
    
    Sub bodyFind(msg As Outlook.MailItem, aEmails)
    Dim data1(), data2(), data3()
    Dim RowCount As Long
    
      sToName = msg.To
      sFromName = msg.SenderName
      sSubject = msg.Subject
      sBody = msg.Body
      sCC = msg.cc
      
      sEmail = sToName & " " & sFromName & " " & sCC & " " & sFromName & " " & sSubject & " " & sBody
      sEmail = UCase(sEmail)
    
      
      
      For aa = LBound(aEmails) To UBound(aEmails)
        On Error Resume Next
            'This does a find function on sEmail when it finds it is supposed to
            'move the email to the folder name from the array
            pos1 = Application.WorksheetFunction.Find(aEmails(aa, cName), sEmail)
            If Err <> 0 Then
    
                Err = 0
            Else
            'here is where it identifies which folder to put the email in
             Dim Myfolder   As Outlook.MAPIFolder
             Set oFolder = Application.GetNamespace("MAPI")
             Set Myfolder = oFolder.Folders(aEmails(aa, cFoldr))
             
              'this moves the email (or it did until we went from 250 rules/folders to 650
              oMail.Move Myfolder
              Sheets("Report").Select
              Do Until ActiveCell = ""
                ActiveCell.Offset(1, 0).Select
              Loop
              ActiveCell = sFromName
              ActiveCell.Offset(0, 1) = sSubject
              ActiveCell.Offset(0, 2) = aEmails(aa, cName)
              ActiveCell.Offset(0, 3) = aEmails(aa, cFoldr)
              ActiveCell.Offset(0, 4) = Now
              ActiveCell.Select
              Set oFolder = Empty
              Set Myfolder = Nothing
              
              GoTo Endloop:
              
              
            End If
        'On Error GoTo 0
        Set oFolder = Empty
        Set Myfolder = Nothing
    
      Next aa
      
    Endloop:
    
    End Sub
    
    Sub EndTimes()
    StopTime = True
    End Sub
    Here is all my code

  4. #4
    Registered User
    Join Date
    02-06-2009
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2007
    Posts
    46

    Re: Problem clearing variables

    and I have been tweaking how it clears the variable myfolder all day and it works intermittently.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1