2017-08-03 28 views
2

我想將所選項目保存在Outlook中。使用下面的代碼,我可以保存該項目,但它只保存第一個項目而不是所選項目。如何在Outlook VBA中保存所選項目

我需要更改什麼才能保存所選項目?

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 

回答

0

下面的行不會沒有錯誤

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

另外,你在呼喚Exit sub,這意味着只能不斷得到處理一個項目上運行。

絕對沒有理由循環瀏覽文件夾中的所有項目。將環路更改爲以下內容

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 
相關問題