2013-03-18 79 views
8

工作,我想獲得主動打開的MailItem(不管它是一個新的郵件或接收郵件)。當用戶運行我的宏時,我需要向該郵件添加一些內容。我使用的是Outlook 2003和VBA。當前打開電子郵件

我找到了這個:How do you get a reference to the mail item in the current open window in Outlook using VBA?它不起作用,因爲TypeName(Application.ActiveWindow)被設置爲空。我也試過Set Mail = Application.ActiveInspector.currentItem,但它也不起作用。

一定有什麼東西我不明白有關ActiveInspector事情。

按照要求,這是程序/宏位於專用模塊,當一個菜單按鈕,用戶點擊Application_Startup()方法添加了名爲:

Sub myMacro() 
    Dim NewMail As Outlook.MailItem 
    Set NewMail = Application.ActiveInspector.currentItem 
End Sub 
+0

如果沒有選擇任何內容,那麼確實'ActiveInspector'將是'Nothing'。不過,我不知道'ActiveWindow'怎麼可能是'Nothing'。你把這個代碼放在哪裏,你怎麼調用它? – 2013-03-18 12:48:33

+0

代碼位於模塊中,當用戶手動運行宏或單擊運行宏的菜單按鈕時,會調用該過程。 – dnLL 2013-03-18 13:09:14

+0

你可以發佈整個方法的代碼嗎? – 2013-03-18 14:29:20

回答

13

我不知道到底你的代碼有什麼問題。但是,首先,你並沒有確認一個新的,可編輯的電子郵件甚至是開放的。下面的概念證明完全符合我認爲你想要做的事情:在正在編寫的活動電子郵件中插入一些文本。如果這是不可能的,它會顯示一個消息框來解釋原因。

該插入如果Word被用作電子郵件編輯器(其將ALWAYS be the case in Outlook 2010+)文本只會工作的部分。如果不是,您將不得不直接解析和更新Body或HTMLBody文本。

Sub InsertText() 
    Dim myText As String 
    myText = "Hello world" 

    Dim NewMail As MailItem, oInspector As Inspector 
    Set oInspector = Application.ActiveInspector 
    If oInspector Is Nothing Then 
     MsgBox "No active inspector" 
    Else 
     Set NewMail = oInspector.CurrentItem 
     If NewMail.Sent Then 
      MsgBox "This is not an editable email" 
     Else 
      If oInspector.IsWordMail Then 
       ' Hurray. We can use the rich Word object model, with access 
       ' the caret and everything. 
       Dim oDoc As Object, oWrdApp As Object, oSelection As Object 
       Set oDoc = oInspector.WordEditor 
       Set oWrdApp = oDoc.Application 
       Set oSelection = oWrdApp.Selection 
       oSelection.InsertAfter myText 
       oSelection.Collapse 0 
       Set oSelection = Nothing 
       Set oWrdApp = Nothing 
       Set oDoc = Nothing 
      Else 
       ' No object model to work with. Must manipulate raw text. 
       Select Case NewMail.BodyFormat 
        Case olFormatPlain, olFormatRichText, olFormatUnspecified 
         NewMail.Body = NewMail.Body & myText 
        Case olFormatHTML 
         NewMail.HTMLBody = NewMail.HTMLBody & "<p>" & myText & "</p>" 
       End Select 
      End If 
     End If 
    End If 
End Sub 
+0

它的工作原理。在程序結束時重置oInspector是否是一個好習慣(比如'Set oInspector = Nothing')? – dnLL 2013-03-18 18:56:15

3

您是不是要選擇當前所選的訊息?在這種情況下,你需要使用Application.ActiveExplorer.Selection收藏,不Application.ActiveInspector.CurrentItem。

0
  ' 
      Dim myOlExp As Outlook.Explorer 
      Dim myOlSel As Outlook.Selection 

      Set myOlExp = Application.ActiveExplorer 
      Set myOlSel = myOlExp.Selection 
      'MsgBox myOlSel.item(1) 


      Dim selectedFolder As Outlook.MAPIFolder 
       Set selectedFolder = myOlExp.CurrentFolder 
      Dim itemMessage As String 
       itemMessage = "Item is unknown." 


      Dim expMessage As String 
      expMessage = "Your current folder is " & selectedFolder.Name & "." & vbCrLf 

      If myOlSel.Count > 0 Then 

          Dim selObject As Object 
          Set selObject = myOlSel.item(1) 

          If (TypeOf selObject Is Outlook.mailItem) Then 
           Dim mailItem As Outlook.mailItem 
           Set mailItem = selObject 
           itemMessage = "The item is an e-mail message." & " The subject is " & mailItem.Subject & "." 
           mailItem.Display (False) 

          ElseIf (TypeOf selObject Is Outlook.contactItem) Then 
           Dim contactItem As Outlook.contactItem 
           Set contactItem = selObject 
           itemMessage = "The item is a contact." & " The full name is " & contactItem.Subject & "." 
           contactItem.Display (False) 

          ElseIf (TypeOf selObject Is Outlook.AppointmentItem) Then 
           Dim apptItem As Outlook.AppointmentItem 
           Set apptItem = selObject 
           itemMessage = "The item is an appointment." & apptItem.Subject & "." 

          ElseIf (TypeOf selObject Is Outlook.taskItem) Then 
           Dim taskItem As Outlook.taskItem 
           Set taskItem = selObject 
           itemMessage = "The item is a task." & " The body is " & taskItem.Body & "." 
          ElseIf (TypeOf selObject Is Outlook.meetingItem) Then 
           Dim meetingItem As Outlook.meetingItem 
           Set meetingItem = selObject 
           itemMessage = "The item is a meeting item. " & "The subject is " & meetingItem.Subject & "." 
          End If 
         End If 
         expMessage = expMessage & itemMessage 
        MsgBox (expMessage) 
      End Sub 
+3

你應該添加一些文字來解釋你的答案相反,它僅僅是一個代碼塊。 – Minzkraut 2017-04-11 06:11:57

+0

@Minzkraut特別是對於一個4歲的問題 – dnLL 2017-04-12 16:46:30

+0

@dnLL我沒有意識到實際的問題,因爲我在評論屏幕上發表了評論。 – Minzkraut 2017-04-13 14:49:51

相關問題