1

I took at a look at MailItem and didn't see anything that would indicate that I could shift select items.

I have code that functions however, the Set objItem = GetCurrentItem() line only takes one mail.

I'm looking to either ForEach through a folder, or ForEach through a selection.

I tried something like

Sub ListMailsInFolder()
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder

Set objNS = GetNamespace("MAPI")
Set objFolder = objNS.Folders.GetFirst ' folders of your current account
Set objFolder = objFolder.Folders("Foldername").Folders("Subfoldername")

For Each Item In objFolder.Items

I don't have any idea what I'm doing.

Here is the code I'm trying to execute on multiple emails:

Sub HelpdeskNewTicket()
Dim helpdeskaddress As String
Dim objMail As Outlook.MailItem
Dim strbody As String
Dim oldmsg As String
Dim senderaddress As String
Dim addresstype As Integer

' Set this variable as your helpdesk e-mail address
helpdeskaddress = "danielbelamiinc@gmail.com"

Set objItem = GetCurrentItem()
Set objMail = objItem.Forward

' Sender E=mail Address
senderaddress = objItem.SenderEmailAddress

strbody = objItem.Body

objMail.To = helpdeskaddress
objMail.Subject = objItem.Subject
objMail.Body = strbody

'Automatically Send the ticket
objMail.Send

Set objItem = Nothing
Set objMail = Nothing
End Sub


Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = _
objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = _
objApp.ActiveInspector.CurrentItem
Case Else
End Select
End Function
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Daniel Williams
  • 167
  • 1
  • 10
  • Are you trying to access the selected items in Outlook? [`ActiveExplorer.Selection`](https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/application-activeexplorer-method-outlook) – PatricK Mar 07 '18 at 03:14
  • I'm trying to have a script run through each selected mail (or mail in a folder) and then forward it to an address. – Daniel Williams Mar 08 '18 at 01:59

1 Answers1

1

To loop through selection items use For...Next Statement Loop [MSDN]


Syntax

For counter = initial_value To end_value [Step step-counter]

Example on Selection Items

Option Explicit
Public Sub Example()
    Dim Item As Outlook.mailitem
    Dim i As Long

    For i = ActiveExplorer.Selection.Count To 1 Step -1
    Set Item = ActiveExplorer.Selection.Item(i)
        Debug.Print Item.Subject
        ' Call Sub
    Next
End Sub

Example on Folder Items

Option Explicit
Public Sub Example()
    Dim Inbox As Outlook.folder
    Set Inbox = Application.Session.GetDefaultFolder( _
                                    olFolderInbox _
                                    )


    Dim Items As Outlook.Items
    Set Items = Inbox.Items

    Dim i As Long
    For i = Items.Count To 1 Step -1
        DoEvents
        Debug.Print Items(i) 'Print on Immediate Window
    Next
End Sub

DoEvents MSDN & Debug.Print SO Link


Description
loop executes a given number of times, as determined by a loop counter. To use the For...Next loop, you must assign a numeric value to a counter variable. This counter is either incremented or decremented automatically with each iteration of the loop. In For statement, you specify the value that is to be assigned to the counter initially and the maximum value the counter will reach for the block of code to be executed. The Next statement marks the end of the block of code that is to execute repeatedly, and also serves as a kind of flag that indicates the counter variable is to be modified.


CurrentFolder Property

Returns or sets a MAPIFolder object that represents the current folder displayed in the explorer. Use this property to change the folder the user is viewing.

0m3r
  • 12,286
  • 15
  • 35
  • 71