2015-12-21 49 views
0
Set myNameSpace = myOlApp.GetNamespace("MAPI") 
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox) 
Set myitems = myInbox.Items 

For Each myitem In myitems 
    If myitem.Class = olMail Then 
     If InStr(1, myitem.Subject, "Greetings") > 0 Then 
      senderemail = myitem.Sender.GetExchangeUser.PrimarySmtpAddress 
      If senderemail = "[email protected]" Then 
       Set oMail = myitem.Forward 
       oMail.Recipients.Add "[email protected]" 
       oMail.HTMLBody = "Hi" 
       oMail.Display 
      End If 
     End If 
    End If 
Next myitem 

我的代碼可以在幾個星期前正常運行。今天我再次運行並調試它我發現一旦涉及到Set oMail = myitem.Forward我得到一個outlook窗口打開並且運行時錯誤顯示應用​​程序定義或對象定義的錯誤。由於運行時錯誤導致無法轉發電子郵件'287'

如何在同一時間獲得轉發電子郵件和錯誤?首先,我只是在顯示命令後才使用Outlook窗口。也由於這個原因,我無法執行我的轉發郵件中的下一行代碼。

編輯:

而且現在我看到的是直接顯示郵件不給任何錯誤,但是一旦我用的.forward命令這是當錯誤出現。

+0

什麼版本的Outlook?有一個選項可以在信任中心設置中實現自動化。 – Sorceri

回答

0

你的代碼中很少有錯誤,所以我清理它並添加更多的代碼 - 試試看,並讓我知道。

Option Explicit 
Sub olForward() 
    Dim olApp As Outlook.Application 
    Dim olFolder As Outlook.MAPIFolder 
    Dim olInbox As Outlook.MAPIFolder 
    Dim olNameSpace As Outlook.NameSpace 
    Dim olItem As Outlook.MailItem 
    Dim olSender As String 

    Set olApp = New Outlook.Application 
    Set olNameSpace = Application.GetNamespace("MAPI") 
    Set olInbox = olNameSpace.GetDefaultFolder(olFolderInbox) 

    '// Require this procedure be called only when a message is selected 
    If Application.ActiveExplorer.Selection.Count = 0 Then 
     Exit Sub 
    End If 

    For Each olItem In Application.ActiveExplorer.Selection 
     If olItem.class = olMail Then 
      If InStr(1, olItem.Subject, "Greetings") > 0 Then 
      olSender = olItem.SenderEmailAddress 
       If olSender = "[email protected]" Then 
       Set olItem = olItem.Forward 
        olItem.Recipients.Add "Om3r <[email protected]>" 
        olItem.HTMLBody = "Hi" + olItem.HTMLBody 
        olItem.Display 
       End If 
      End If 
     End If 
    Next 
End Sub 
相關問題