迭代多个选择/文件夹项目 [英] Iterating through multiple Selection / Folder items
问题描述
我查看了 MailItem
并没有看到任何表明我可以移动选择项的内容.
I took at a look at MailItem
and didn't see anything that would indicate that I could shift select items.
我有可以运行的代码,但是 Set objItem = GetCurrentItem()
行只需要一封邮件.
I have code that functions however, the Set objItem = GetCurrentItem()
line only takes one mail.
我希望通过文件夹查找 ForEach
,或者通过选择查找 ForEach
.
I'm looking to either ForEach
through a folder, or ForEach
through a selection.
我尝试过类似
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
我不知道我在做什么.
这是我尝试在多封电子邮件上执行的代码:
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
推荐答案
要循环选择项目,请使用 For...Next Statement Loop [MSDN]
语法
For counter = initial_value To end_value [Step step-counter]
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
文件夹项目示例
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 链接
说明
loop 执行给定的次数,由循环计数器确定.要使用 For...Next 循环,您必须为计数器变量分配一个数值.该计数器随着循环的每次迭代自动递增或递减.在 For 语句中,您指定最初分配给计数器的值以及计数器将要执行的代码块将达到的最大值.Next 语句标志着要重复执行的代码块的结束,同时也作为一种标志,指示要修改计数器变量.
返回或设置一个 MAPIFolder 对象,该对象表示资源管理器中显示的当前文件夹.使用此属性更改用户正在查看的文件夹.
这篇关于迭代多个选择/文件夹项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!