Reputation: 9
I am have been looking for ways on how to create a visual basic script for outlook and most of the suggestions have been from this website. Unfortunately none of them meet my exact need.
Reason for the script:
There is a shared mailbox which a few of my colleagues have access to and are meant to check however at times this can get ignored. I would like to create a script which checks for unread emails in this shared mailbox and emails specific contacts if the folder contains more than 10 emails.
What I have tried so far:
Option Explicit
Private Sub Main()
Dim olMAPI As Outlook.NameSpace
Dim Folder As Outlook.MAPIFolder
Const FOLDER_TO_OPEN = "Mailbox - John Doe" 'Modify as appropriate
Set olMAPI = GetObject("", "Outlook.Application") _
.GetNamespa ce("MAPI")
Call PrintFolderNames(olMAPI.Folders(FOLDER_TO_OPEN), "->")
Set olMAPI = Nothing
End Sub
Sub PrintFolderNames(tempfolder As Outlook.MAPIFolder, a$)
Dim i As Integer
If tempfolder.Folders.Count Then
Debug.Print a$ & " " & tempfolder.Name & " ";
Debug.Print tempfolder.UnReadItemCount
For i = 1 To tempfolder.Folders.Count
Call PrintFolderNames(tempfolder.Folders(i), a$ & "->")
Next i
Else
Debug.Print a$ & " " & tempfolder.Name & " ";
Debug.Print tempfolder.UnReadItemCount
End If
End Sub
I have tried to create a script first which checks for unread emails in a specific folder but this usually fails with syntax/run time errors. I have tried:
But this came back with a compile error then a syntax error.
I have also tried:
Const olFolderInbox = 6
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
objNamespace.Logon "Default Outlook Profile", , False, True
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
Set colItems = objFolder.Items
Wscript.Echo colItems.Count
objOutlook.Quit
Upvotes: 0
Views: 7552
Reputation: 12745
You must have outlook installed to use this code:
Const olFolderInbox = 6
Const olMailItem = 0
dim objOutlook
call checkForUnreadMails
sub checkForUnreadMails()
dim objFolder, objNamespace
'get running outlook application or open outlook
Set objOutlook = GetObject(, "Outlook.Application")
If objOutlook Is Nothing Then
Set objOutlook = CreateObject("Outlook.Application")
End If
Set objNamespace = objOutlook.GetNamespace("MAPI")
'get inbox folder
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
'send mail if more than 10 mails are unread
if objFolder.UnReadItemCount > 10 then
sendMail "your@mail.address"
end if
'send mail if more folder contains more than 10 mails
'if objFolder.Items.Count > 10 then
' sendMail "your@mail.address"
'end if
end sub
sub sendMail(address)
dim oItem
Set oItem = objOutlook.CreateItem(olMailItem)
With oItem
.To = address
.Subject = "Unread mails!"
.Body = "You have too many unread mails!"
.send
End With
end sub
Upvotes: 1