2013-03-18 15 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'になるかも知りません。このコードをどこに置いていますか、どのように呼び出すのですか? –

+0

コードはモジュール内にあり、ユーザーがマクロを手動で実行するか、マクロを実行するメニューボタンをクリックすると、プロシージャが呼び出されます。 – dnLL

+0

メソッド全体のコードを投稿できますか? –

答えて

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

3

現在選択されているメッセージを意味しますか?その場合は、Application.ActiveInspector.CurrentItemではなくApplication.ActiveExplorer.Selectionコレクションを使用する必要があります。

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

+0

@Minzkraut特に4歳の質問 – dnLL

+0

@dnLL私はレビュー画面内からコメントしたので、私は実際の質問に気づいていませんでした。 – Minzkraut

関連する問題