Joshua Patterson
Joshua Patterson

Reputation: 473

Change Outlook Inbox VBA code is operating on

I wrote a VBA script, and it currently only operates on the first inbox open in outlook.

How do I specify which Inbox it operates on? ie, the Inbox of another account.

I assume somewhere in here -

Sub MoveAgedMail()

Dim objOutlook As Outlook.Application
Dim objNamespace As Outlook.NameSpace
Dim objSourceFolder As Outlook.MAPIFolder
Dim objDestFolder As Outlook.MAPIFolder
Dim objVariant As Variant
Dim lngMovedItems As Long
Dim intCount As Integer
Dim intDateDiff As Integer
Dim strDestFolder As String

Upvotes: 0

Views: 325

Answers (1)

0m3r
0m3r

Reputation: 12499

ie, the inbox of another account.

Assuming your talking about shared Inbox

VBA Example Code will be

Option Explicit
Sub OpenShareInbox()
    Dim olNameSpace As Outlook.NameSpace
    Dim olRec As Outlook.Recipient
    Dim olFolder As Outlook.Folder

    Set olNameSpace = Application.GetNamespace("MAPI")
    Set olRec = olNameSpace.CreateRecipient("[email protected]") '// Owner's email address
    Set olFolder = olNameSpace.GetSharedDefaultFolder(olRec, olFolderInbox)

    MsgBox olRec.Name '// Owner Name

    olFolder.Display '// Open Inbox
End Sub

Edit:

Here is another Example - Open your immediate window and print Subject line

Option Explicit
Sub OpenShareInbox()
    Dim olNameSpace As Outlook.NameSpace
    Dim olRec As Outlook.Recipient
    Dim olFolder As Outlook.Folder

    Dim olItem As Outlook.MailItem

    Set olNameSpace = Application.GetNamespace("MAPI")
    Set olRec = olNameSpace.CreateRecipient("[email protected]") '// Owner's email address
    Set olFolder = olNameSpace.GetSharedDefaultFolder(olRec, olFolderInbox) '// Inbox


    For Each olItem In olFolder.Items
        Debug.Print olItem.Subject
    Next

End Sub

Or Forward Mail Item with Subject line "Report"

For Each olItem In olFolder.Items
    If olFolder.DefaultItemType = olMailItem Then
        If olItem.Class = olMail Then
            If olItem.Subject = "Report" Then
                Set olItem = olItem.Forward
                olItem.Subject = "APPENDED SUBJECT - " + olItem.Subject + ""
                olItem.Recipients.Add "Om3r <[email protected]>"
                olItem.Display
    '            olItem.Send
            End If
        End If
    End If
Next

See NameSpace.GetSharedDefaultFolder Method (Outlook)on MSDN

Upvotes: 1

Related Questions