2017-03-21 7 views
0

Using Events with Automationは、オートメーションでOutlookイベントを使用する方法を説明しています。私はしかし、私のプロジェクトにそのコードを実装する方法を理解していない。Outlookイベントをオートメーションで使用する

私はInspector.BeforeMinimize Event (Outlook)

で説明したようにInspectorBeforeMinimizeEventメソッドを使用したいときはいつでも、ユーザーの移動/サイズ変更さ/最大/そしてMessageBoxがポップアップする必要があり、手動でインスペクタを最小限に抑えることができます。

次のコードは、テストのためのものである:

Imports Microsoft.Office.Interop 

Public Class Form1 

    Public WithEvents myIns As Outlook.Inspector 

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load 

     Process.Start("Outlook.exe") 
     Threading.Thread.Sleep(3000) 
     Dim olApp As New Outlook.Application 

     Dim myMailItem As Outlook.MailItem 
     myMailItem = CType(olApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem) 
     myMailItem.Subject = "Hello" 
     myMailItem.To = "[email protected]" 
     myMailItem.Body = "Hi there..." 

     Dim myIns As Outlook.Inspector 
     myIns = myMailItem.GetInspector 

     myIns.Display(False) 

     Dim myWord As Word.Document 
     myWord = CType(myIns.WordEditor, Word.Document) 

     Dim mySel As Word.Selection 
     mySel = myWord.Application.Selection 

     Threading.Thread.Sleep(10000) 

     'Following line just for testing. Normally following line doesnt exist in my original code. The real scenario is the user minimizes the inspector manually. 
     myIns.WindowState = Outlook.OlWindowState.olMinimized 

     myWord.InlineShapes.AddPicture(FileName:="C:\Example.png", LinkToFile:=False, SaveWithDocument:=True, Range:=mySel.GoTo(What:=Word.WdGoToItem.wdGoToLine, Which:=Word.WdGoToDirection.wdGoToLast, Count:=-4)) 

     'myMailItem.Send() 

    End Sub 

End Class 

イベントコード:

Private Sub myIns_BeforeMinimize(Cancel As Boolean) 
    MessageBox.Show("You are minimizing this inspector.", "Title", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly) 
End Sub 
+0

があなたの投稿をvandalizeないでください:

あなたのコードは次のようになります。 Stack Exchangeネットワーク上に掲載することで、SEがそのコンテンツを配布する権利(CC BY-SA 3.0ライセンス)(https://creativecommons.org/licenses/by-sa/3.0 /))。 SEのポリシーによって、どんな破壊行為も元に戻ります。この投稿とアカウントとの関連付けを解除する場合は、[解約リクエストの正しいルートは何ですか?](https://meta.stackoverflow.com/q/323395)を参照してください。 – Bugs

答えて

0

これはあなたが追加する必要が動作するように取得するには毎回ActivateのハンドラとMailItemがアクティブになるあなたはWindowStateを設定します:

Private Sub myIns_Activate() Handles myIns.Activate 
    myIns.WindowState = Outlook.OlWindowState.olMinimized 
End Sub 

私はまた、これらの行を削除します:それはDim olApp As New Outlook.Applicationで扱われるようあなたはProcess.Startを必要としない

Process.Start("Outlook.exe") 
Threading.Thread.Sleep(3000) 

Dim myIns As Outlook.Inspector 

Threading.Thread.Sleep(10000) 

Public WithEvents myIns As Outlook.Inspectorで処理されるので、Dim myIns As Outlook.Inspectorも必要ありません。私はThreading.Thread.Sleep行がテスト目的のためにあることを感謝します。

Imports Microsoft.Office.Interop 

Public Class Form1 

    Public WithEvents myIns As Outlook.Inspector 

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load 

     Dim olApp As New Outlook.Application 

     Dim myMailItem As Outlook.MailItem = CType(olApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem) 
     myMailItem.Subject = "Hello" 
     myMailItem.To = "[email protected]" 
     myMailItem.Body = "Hi there..." 

     myIns = myMailItem.GetInspector 
     myIns.Display(False) 

     Dim myWord As Word.Document 
     myWord = CType(myIns.WordEditor, Word.Document) 

     Dim mySel As Word.Selection 
     mySel = myWord.Application.Selection 

     myWord.InlineShapes.AddPicture(FileName:="C:\Example.png", LinkToFile:=False, SaveWithDocument:=True, Range:=mySel.GoTo(What:=Word.WdGoToItem.wdGoToLine, Which:=Word.WdGoToDirection.wdGoToLast, Count:=-4)) 

     myMailItem.Send() 

    End Sub 

    Private Sub myIns_Activate() Handles myIns.Activate 
     myIns.WindowState = Outlook.OlWindowState.olMinimized 
    End Sub 

End Class 
0

クラスレベルでインスペクタオブジェクトの変数を宣言し、.GetInspectorメソッドの戻りに設定し、その後、実際のイベントにコードを置く:

Public WithEvents myIns As Outlook.Inspector 

Private Sub myIns_BeforeMinimize(Cancel As Boolean) 
    'Your code here 
End Sub 
関連する問題