Consulting

Results 1 to 2 of 2

Thread: vba timer outlook

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Regular
    Joined
    Sep 2011
    Posts
    6
    Location

    vba timer outlook

    the code runs one time, it checks all active mailboxes in the junk folder and move these items to their inbox. problem is i can run it only once or activate by itemadd
    but only on the default inbox. script is made for multiple users with not all the same mailboxes

    question: can i run it on schedule or time base ? ( sleep does not work ! )
    or how program the itemadd function on multiple ( and different users ) mailboxes by array i think ?

    Option Explicit
    
    Public foldernaam As String
    Private WithEvents Items As Outlook.Items
    
    Private Sub Application_Startup()
    Sleep 5
    ShowActiveMailboxes
    Dim objApp As Outlook.Application
    Dim objNamespace As Outlook.NameSpace
    Dim inboxFol As Object  'Outlook.Folder
    Set objApp = Outlook.Application
    Set objNamespace = objApp.GetNamespace("MAPI")
    Set Items = objNamespace.GetDefaultFolder(olFolderJunk).Items
    Dim Items2 As Outlook.Items
    Dim mail As Object
    Dim olNS As Outlook.NameSpace
    Dim subfolder As Outlook.MAPIFolder
    Dim i As Long
    Dim Item3 As Object
    Set olNS = Application.GetNamespace("MAPI")
    Set mail = olNS.GetDefaultFolder(olFolderJunk)
    Set Items2 = mail.Items
    Set subfolder = olNS.GetDefaultFolder(olFolderInbox)
    For i = Items2.count To 1 Step -1
        DoEvents
        'MsgBox Items2.Count
        Set Item3 = Items2(i)
        Item3.Subject = "[Possible Spam] " & Item3.Subject
        Item3.Save
        Item3.Move subfolder
    Next
    End Sub
    
    Private Sub Items_ItemAdd(ByVal objItem As Object)
    Sleep 4
    On Error GoTo ShowError
    Dim objApp As Outlook.Application
    Dim objNamespace As Outlook.NameSpace
    Dim inboxFol As Object  'Outlook.Folder
    Set objApp = Outlook.Application
    Set objNamespace = objApp.GetNamespace("MAPI")
    Set Items = objNamespace.GetDefaultFolder(olFolderJunk).Items
    Dim Items2 As Outlook.Items
    Dim mail As Object
    Dim olNS As Outlook.NameSpace
    Dim subfolder As Outlook.MAPIFolder
    Dim i As Long
    Dim Item3 As Object
    Set olNS = Application.GetNamespace("MAPI")
    Set mail = olNS.GetDefaultFolder(olFolderJunk)
    Set Items2 = mail.Items
    Set subfolder = olNS.GetDefaultFolder(olFolderInbox)
    For i = Items2.count To 1 Step -1
        DoEvents
        'MsgBox Items2.Count
        Set Item3 = Items2(i)
        Item3.Subject = "[Possible Spam] " & Item3.Subject
        Item3.Save
        Item3.Move subfolder
    Next
    'ShowActiveMailboxes
    Exit Sub
    'chec shared boxes
    ShowError:
    MsgBox Err.Number & " - " & Err.Description
    End Sub
        
    Public Sub Sleep(ByVal SleepSeconds As Single)
    Dim Tmr As Single
    Tmr = Timer
    Do While Tmr + SleepSeconds > Timer
        DoEvents
    Loop
    End Sub
    
    Public Sub ShowActiveMailboxes()
    Dim olApp As Outlook.Application
    Set olApp = Outlook.Application
    Dim ns As Outlook.NameSpace
    Set ns = olApp.GetNamespace("MAPI")
    Dim fldr As Outlook.MAPIFolder
    For Each fldr In ns.Folders
       If fldr.DefaultItemType = olMailItem Then
          'MsgBox (fldr.Name)
          foldernaam = fldr
              If foldernaam Like "*Archi*" Then
                  'sla mailbox over
                  ElseIf foldernaam = "No Reply" Then
                  'sla over
                  ElseIf foldernaam Like "*IT Supp*" Then
                  'sla over
                 Else
                 'MsgBox (foldernaam)
                Call extra
             End If
        End If
    Next
    'Sleep 120
    'Call Application_Startup
    End Sub
    
    Public Sub extra()
    'MsgBox ("check unread mail in ") & foldernaam
    Dim objOutlook As Outlook.Application
    Dim objNamespace As Outlook.NameSpace
    Dim objFolder As Outlook.Folder
    Dim subfolder As Outlook.Folder
    Dim objMailbox As Outlook.Recipient
    'Dim lngUnread As Long
    Dim Items2 As Outlook.Items
    Dim mail As Object
    Dim x As Long
    Dim Item3 As Object
    Set objOutlook = Outlook.Application
    Set objNamespace = objOutlook.GetNamespace("MAPI")
    Set objMailbox = objNamespace.CreateRecipient(foldernaam)
    Set objFolder = objNamespace.GetSharedDefaultFolder(objMailbox, olFolderJunk)
    Set subfolder = objNamespace.GetSharedDefaultFolder(objMailbox, olFolderInbox)
    Set mail = objFolder
    Set Items2 = mail.Items
    'lngUnread = objFolder.UnReadItemCount
    'MsgBox "There are " & lngUnread & " unread emails in the Shared Mailbox.", vbInformation
    'MsgBox "aantal mails check !!!!!!! " & foldernaam & " " & (Items2.count)
    For x = Items2.count To 1 Step -1
         DoEvents
         'MsgBox Items2.Count
          Set Item3 = Items2(x)
          Item3.Subject = "[Possible Spam] " & Item3.Subject
          Item3.Save
          Item3.Move subfolder
    Next
    End Sub
    Last edited by kriszty; 02-08-2023 at 12:37 PM.

Posting Permissions

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