0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【C#】Outlook予定表取得

Last updated at Posted at 2019-06-25
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

using Microsoft.Office.Interop.Outlook;

namespace WindowsFormsApp1
{
    
    public partial class Form1 : Form
    {
        static bool flag = true;

        /*** Form1.cs ***/
        public Form1()
        {
            InitializeComponent();
            new Thread(new ThreadStart(GetMouseButton)).Start();
            this.ShowInTaskbar = false;
        }

        public void GetMouseButton()
        {
            while (flag is true)
            {
                if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left)
                {
                    getText();
                    //Console.WriteLine("マウスのXBUTTON2が押されています。");
                }
                //Thread.Sleep(100);
            }
        }
                
        public delegate void Delegate();

        public void getText()
        {
            try
            {
                if (InvokeRequired)
                {
                    Invoke(new Delegate(getText));
                    return;
                }

                Point cp = this.PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y));
                //Console.WriteLine(cp.X.ToString() + " " + cp.Y.ToString());
                if ((cp.X < 0 || this.Width < cp.X ) && (cp.Y < 0 || this.Height < cp.Y) )
                {
                    this.Location = new Point(Cursor.Position.X + 10, Cursor.Position.Y + 15);
                }

                Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application();
                NameSpace ns = outlook.GetNamespace("MAPI");
                MAPIFolder oFolder = ns.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);

                textBox1.Text = "";
                if (outlook.ActiveExplorer().Selection.Count is 1)
                {
                    Object selObject = outlook.ActiveExplorer().Selection[1];
                    if (selObject is AppointmentItem)
                    {
                        AppointmentItem oAppoint = (selObject as AppointmentItem);
                        textBox1.Text += "開催:" + oAppoint.Organizer + "\r\n";
                        textBox1.Text += "件名:" + oAppoint.Subject + "\r\n";
                        textBox1.Text += "開始:" + oAppoint.Start.ToString("yyyy/MM/dd HH:mm:ss") + "\r\n";
                        textBox1.Text += "終了:" + oAppoint.End.ToString("yyyy/MM/dd HH:mm:ss") + "\r\n";
                        textBox1.Text += "場所:" + oAppoint.Location + "\r\n";
                        Clipboard.SetText("開催:" + oAppoint.Organizer + "\n" 
                                        + "件名:" + oAppoint.Subject + "\n" 
                                        + "開始:" + oAppoint.Start.ToString("yyyy/MM/dd HH:mm:ss") + "\n"
                                        + "終了:" + oAppoint.End.ToString("yyyy/MM/dd HH:mm:ss") + "\n"
                                        + "場所:" + oAppoint.Location + "\n");
                    }
                    else
                    {
                    }
                }
            }
            catch (System.Exception ex)
            {
            }
        }

        private void notifyIcon1_MouseDoubleClick_1(object sender, MouseEventArgs e)
        {
            // フォームを表示する
            this.Visible = true;
            // 現在の状態が最小化の状態であれば通常の状態に戻す
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.WindowState = FormWindowState.Normal;
            }
            // フォームをアクティブにする
            new Thread(new ThreadStart(GetMouseButton)).Start();
            this.Activate();
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                flag = false;
            }
            else
            {
                flag = true;
                new Thread(new ThreadStart(GetMouseButton)).Start();
            }
        }

        private void Form1_FormClosing_1(object sender, FormClosingEventArgs e)
        {
            flag = false;
        }
    }
}

##参考

MAPI over HTTP を使用すると、Outlook 2010、2013、2016 で Exchange に正しく接続できない
 ↑関係あるかわからないけど
[C#] Outlookの予定表から予定を取得する
方法: プログラムによって現在の Outlook アイテムを確認します。
【C#入門】Timerで処理を一定間隔で繰り返す方法
現在どのマウスボタンが押されているか調べる
マウスの位置を取得し続ける(C#)
チェックボックスがチェックされているか取得する (C#プログラミング)
C#を使って最小化した時にタスクトレイに格納する
C#: タスクトレイに常駐するアプリの作り方
タスクバーにフォームを表示しないようにする
画面座標をクライアント座標(コントロール上の座標)に変換する

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?