2017-10-10 364 views
1

我在Outlook中有2個帳戶,一個是我的個人,另一個是共享的。我想閱讀或閱讀我的共享郵箱中的未讀郵件。我有一個正在使用我的電子郵件收件箱的代碼,但沒有與我的共享電子郵件組一起工作。如何使用Excel VBA讀取Outlook中的共享電子郵件中的電子郵件

它顯示錯誤enter image description here

我的代碼如下:

Sub OutlookTesting() 
Dim folders As Outlook.folders 
Dim Folder As Outlook.MAPIFolder 
Dim iRow As Integer 
Dim Pst_Folder_Name 
Dim MailboxName 
Dim UnRow As Integer 
Dim RESS As Outlook.Recipient 
Dim Flag As Integer 


'Mailbox or PST Main Folder Name (As how it is displayed in your Outlook Session) 
MailboxName = "[email protected]" 'Mailbox Folder or PST Folder Name (As how it is displayed in your Outlook Session) 
Pst_Folder_Name = "Inbox" 

' subfolder name 
Dim subFolderName As String 
subFolderName = "XYZ" 

Set Folder = Outlook.Session.folders(MailboxName).folders(Pst_Folder_Name) 
If Folder = "" Then 
    MsgBox "Invalid Data in Input" 
    GoTo end_lbl1: 
End If 

'Rad Through each Mail and export the details to Excel for Email Archival 

For iRow = 1 To Folder.Items.Count 
    If (Folder.Items(iRow).UnRead) Then 
     Flag = 0 
     Set Res = Folder.Items(iRow).Recipients 
      For Each RESS In Res 
       If RESS.Name = "ABCD" Or RESS.Name = "PQRS" Then 
        Flag = 1 
       End If 
      Next 
      If Flag = 1 Then 
        Folder.Items(iRow).UnRead = True 
        Else: Folder.Items(iRow).UnRead = False 
       End If 
    End If 
Next iRow 
MsgBox "Outlook Mails Extracted to Excel" 
end_lbl1: 
End Sub 
+0

爲什麼在'Else'之後有''''列? – Pac0

+0

在附註中,我建議使用'Return'或'End Sub'來代替末尾的 'GoTo'和標籤,以保證清晰度和可維護性。 – Pac0

+0

此代碼在個人帳戶中工作,但不在共享帳戶中。因此,在共享帳戶中運行需要進行任何更改。 –

回答

2

嗨,你可以用下面的代碼嘗試(我有編輯你上面貼的代碼),並刪除根據不同尋常的代碼你需要。

Sub OutlookTesting() 
Dim folders As Outlook.folders 
Dim folder As Outlook.MAPIFolder 
Dim iRow As Integer 
Dim Pst_Folder_Name 
Dim MailboxName 
Dim UnRow As Integer 
Dim RESS As Outlook.Recipient 
Dim Flag As Integer 
Dim olApp As Outlook.Application 
Dim olNS As Outlook.Namespace 
Dim olfldr As Outlook.MAPIFolder 
Dim foldername As Outlook.MAPIFolder 
Dim sharedemail As Outlook.Recipient 


Set olApp = New Outlook.Application 
Set olNS = olApp.GetNamespace("MAPI") 
Set sharedemail = olNS.CreateRecipient("[email protected]") 
Set olfldr = olNS.GetSharedDefaultFolder(sharedemail, olFolderInbox) 


Set folder = olfldr 

If folder = "" Then 
    MsgBox "Invalid Data in Input" 
    GoTo end_lbl1: 
End If 

'Rad Through each Mail and export the details to Excel for Email Archival 

For iRow = 1 To folder.Items.Count 
    If (folder.Items(iRow).UnRead) Then 
     Flag = 0 
     Set Res = folder.Items(iRow).Recipients 
      For Each RESS In Res 
       If RESS.Name = "XYZ" Or RESS.Name = "ABC" Then 
        Flag = 1 
       End If 
      Next 
      If Flag = 1 Then 
        folder.Items(iRow).UnRead = True 
        Else: folder.Items(iRow).UnRead = False 
       End If 
    End If 
Next iRow 
MsgBox "Outlook Mails Extracted to Excel" 
end_lbl1: 
End Sub 
相關問題