vba 将所选项目更改为下一个或上一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/966887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the selected item to next or previous
提问by K Richard
I wrote code below that is very similar to the accepted answer here. It marks emails and meeting responses as read and moves them to the archive.
我在下面编写了与此处接受的答案非常相似的代码。它将电子邮件和会议回复标记为已读并将它们移至存档。
I sort my mail by newest on top. After using the macro, the selection defaults to the next email down (the older one). I want it to move to the next email up (the newer one). If I reverse the sorting order of emails, then it works how I want it to. I've dedicated too much time to this to just reverse the sorting order of my emails.
我按最新排序我的邮件。使用宏后,选择默认为下一封电子邮件(较旧的)。我希望它移动到下一封电子邮件(较新的)。如果我颠倒电子邮件的排序顺序,那么它会按照我想要的方式工作。我为此花费了太多时间来颠倒我的电子邮件的排序顺序。
I tried to set a MailItem to Application.ActiveExplorer.CurrentFolder.Items.GetNext then use MailItem.Display. This opens rather than changes selection, doesn't know current selection, can't figure out what's considered "next".
我尝试将 MailItem 设置为 Application.ActiveExplorer.CurrentFolder.Items.GetNext 然后使用 MailItem.Display。这将打开而不是更改选择,不知道当前选择,无法弄清楚什么被认为是“下一步”。
I tried setting the Application.ActiveExplorer.Selection.Item property. I've been through (MSDN and Expertsexchange) looking for a solution.
我尝试设置 Application.ActiveExplorer.Selection.Item 属性。我已经通过(MSDN 和 Expert sexchange)寻找解决方案。
Sub MoveToArchive()
On Error Resume Next
Dim objFolder As Outlook.MAPIFolder
Dim objNS As Outlook.NameSpace, objItem As Outlook.MailItem
Dim objMRItem As Outlook.MeetingItem
Set objNS = Application.GetNamespace("MAPI")
Set objFolder = objNS.Folders("Archive").Folders("Inbox")
If Application.ActiveExplorer.Selection.Count = 0 Then
Exit Sub
End If
For Each objItem In Application.ActiveExplorer.Selection
If objFolder.DefaultItemType = olMailItem Then
If objItem.Class = olMail Then
objItem.UnRead = False
objItem.Move objFolder
End If
End If
Next
For Each objMRItem In Application.ActiveExplorer.Selection
If objFolder.DefaultItemType = olMailItem Then
If objItem.Class = olMeetingResponsePositive Or olMeetingResponseNegative Or olMeetingResponseTentative Then
objMRItem.UnRead = False
objMRItem.Move objFolder
End If
End If
Next
Set objItem = Nothing
Set objMRItem = Nothing
Set objFolder = Nothing
Set objNS = Nothing
End Sub
回答by 76mel
Outlook provides no way to programmatically select a particular item in the Explorer window. So you would be able to do it this way. The only way I think that you could do it would be to programmatically press the previous key on the tool bar or menu. or redesgin the app to archive in the inspector etc.
Outlook 无法以编程方式在资源管理器窗口中选择特定项目。所以你可以这样做。我认为您可以这样做的唯一方法是以编程方式按下工具栏或菜单上的上一个键。或重新设计应用程序以在检查器等中存档。
回答by user1980321
Insert into your code one of the two lines of SendKeys code from one of the following two macros to cause Outlook to change the selection in the Outlook Item List pane to the message above or below the active message. The email message item you are moving up or down from must be active in the Item List pane in the Outlook application before the SendKeys code executes. If necessary add a time delay in your code to allow time for the appropriate message to become active in Outlook before the SendKeys code executes. These macros could also be added to the ribbon in Outlook 2010 to create up and down arrow buttons to navigate in the Outlook Item List Pane.
将来自以下两个宏之一的两行 SendKeys 代码之一插入到您的代码中,以使 Outlook 将 Outlook 项目列表窗格中的选择更改为活动邮件上方或下方的邮件。在执行 SendKeys 代码之前,您要向上或向下移动的电子邮件项目必须在 Outlook 应用程序的“项目列表”窗格中处于活动状态。如有必要,请在代码中添加一个时间延迟,以便在 SendKeys 代码执行之前有时间让相应的消息在 Outlook 中变为活动状态。这些宏也可以添加到 Outlook 2010 中的功能区,以创建向上和向下箭头按钮以在 Outlook 项目列表窗格中导航。
-- Sub MoveUp()
-- 子 MoveUp()
SendKeys "{UP}": DoEvents
SendKeys“{UP}”:DoEvents
End Sub
结束子
Sub MoveDown()
子下移()
SendKeys "{Down}": DoEvents
SendKeys“{Down}”:DoEvents
End Sub
结束子
回答by Joao
I was able to achieve a similar effect by using the SendKeys function.
通过使用 SendKeys 函数,我能够达到类似的效果。
I wanted to return to the first message in the explorer, after doing some manipulation of the selected items.
在对所选项目进行一些操作后,我想返回到浏览器中的第一条消息。
I just added the following line after the manipulation is done:
操作完成后,我刚刚添加了以下行:
SendKeys "{HOME}"
This works for me, on this particular case but can be modified to be used in other context.
这对我有用,在这种特殊情况下,但可以修改以在其他上下文中使用。
回答by user2463073
Use clearselection
and addtoselection
like this
使用clearselection
并addtoselection
喜欢这个
Call e.ClearSelection
For Each itt In e.CurrentFolder.Items
'MsgBox CStr(itt)
If (e.IsItemSelectableInView(itt)) Then
If itt.FullName = it.FullName Then
Call e.AddToSelection(itt)
End If
End If
Next