How to save selected item in Outlook VBA

本秂侑毒 提交于 2019-12-14 02:03:34

问题


I want to save the selected item in Outlook. With the below code, I am able to save the item but it saves only the 1st item and not the selected item.

What do I need to change in order to save the selected item?

Dim oOlApp As Object, objNmSpc As Object, ofldr As Object
Dim myCopiedItem As Outlook.MailItem
Dim myNewFolder  As String

Set oOlApp = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then
    Set oOlApp = CreateObject("Outlook.Application")
End If
Err.Clear

Set objNmSpc = oOlApp.GetNamespace("MAPI")
Set ofldr = objNmSpc.PickFolder
If Not ofldr Is Nothing Then MsgBox ofldr

Dim oItem As Object
For Each oItem In ofldr.Items
    If oOlApp.ActiveExplorer.Selection.Item(1) = True Then
        oItem.SaveAs Sheet1.Range("V5").Value & oItem.Subject & ".msg", olMSG
        match.Offset(, 7).Value = oItem.SenderName & "-" & oItem.Subject & "-" & oItem.ReceivedTime
        match.Offset(, 8).Value = VBA.Environ("Username") & "- " & VBA.Now
        Exit Sub
    End If
Next oItem

回答1:


The following line would not run without an error

If oOlApp.ActiveExplorer.Selection.Item(1) = True Then

Plus you are calling Exit sub, which means only one item can ever get processed.

There is absolutely no reason to loop through all items in a folder. Change the loop to the following

For Each oItem In oOlApp.ActiveExplorer.Selection
        oItem.SaveAs Sheet1.Range("V5").Value & oItem.Subject & ".msg", olMSG
        match.Offset(, 7).Value = oItem.SenderName & "-" & oItem.Subject & "-" & oItem.ReceivedTime
        match.Offset(, 8).Value = VBA.Environ("Username") & "- " & VBA.Now
Next oItem


来源:https://stackoverflow.com/questions/45485283/how-to-save-selected-item-in-outlook-vba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!