다음을 통해 공유


방법: Outlook 세션 간에 사용자 지정 도구 모음의 위치 정보 유지

이 예제에서는 Outlook 사용자 지정 도구 모음의 위치를 저장하고 복원하는 한 가지 방법을 보여 줍니다. 이 코드는 도구 모음을 만들고 사용자가 Outlook을 종료할 때 사용자 설정 파일에 해당 위치를 저장합니다. Outlook을 다시 시작하면 도구 모음이 다시 만들어지고 저장된 설정을 기반으로 하여 해당 위치가 설정됩니다.

적용 대상: 이 항목의 정보는 Outlook 2007의 응용 프로그램 수준 프로젝트에 적용됩니다. 자세한 내용은 Office 응용 프로그램 및 프로젝트 형식에 따라 사용 가능한 기능을 참조하십시오.

예제

Dim commandBar As Office.CommandBar
Dim WithEvents button As Office.CommandBarButton

Const TOOLBARNAME As String = "ExampleBar"
Const REGROOT As String = "Software\YourCompany\YourApp"

Dim WithEvents explorerEvents As Outlook.ExplorerEvents_10_Event
Dim explorer As Outlook.Explorer

Private Sub ThisAddIn_Startup(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.Startup
    explorer = Me.Application.ActiveExplorer()

    If (explorer IsNot Nothing) Then

        commandBar = Me.Application.ActiveExplorer.CommandBars.Add( _
            TOOLBARNAME, _
            Office.MsoBarPosition.msoBarFloating, _
            False, _
            True)

        button = TryCast(commandBar.Controls.Add( _
            Office.MsoControlType.msoControlButton, _
            System.Type.Missing, System.Type.Missing, _
                1, True), Office.CommandBarButton)

        button.Style = _
            Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption
        button.Caption = "Button 1"
        button.Tag = "newButton"

        LoadCommandBarSettings()

    End If
End Sub

Private Sub SaveCommandBarSettings()
    MySettings.Default("CommandBarTop") = commandBar.Top
    MySettings.Default("CommandBarLeft") = commandBar.Left
    MySettings.Default("CommandBarVisible") = commandBar.Visible
    MySettings.Default("CommandBarPosition") = CInt(commandBar.Position)
    MySettings.Default("CommandBarRowIndex") = commandBar.RowIndex
    MySettings.Default.Save()
End Sub

Private Sub LoadCommandBarSettings()
    Dim position As Microsoft.Office.Core.MsoBarPosition = _
        CType(MySettings.Default("CommandBarPosition"),  _
            Microsoft.Office.Core.MsoBarPosition)

    Dim rowIndex As Integer = _
        CInt(MySettings.Default("CommandBarRowIndex"))

    commandBar.RowIndex = rowIndex

    Dim top As Integer = _
        CInt(MySettings.Default("CommandBarTop"))

    commandBar.Top = _
        CInt(IIf(top <> 0, top, _
        System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height / 2))

    Dim left As Integer = _
        CInt(MySettings.Default("CommandBarLeft"))

    commandBar.Left = _
        CInt(IIf(left <> 0, left, _
        System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / 2))

    Dim visible As Boolean = _
        CBool(MySettings.Default("CommandBarVisible"))

    commandBar.Visible = visible

End Sub

Private Sub Click(ByVal ctrl As  _
    Microsoft.Office.Core.CommandBarButton, _
    ByRef cancelDefault As Boolean) Handles Button.Click
    System.Windows.Forms.MessageBox.Show("Hello World!")
End Sub

Sub ThisAddIn_Close() Handles explorerEvents.Close

    SaveCommandBarSettings()
End Sub
        Office.CommandBar commandBar;
        Office.CommandBarButton button;

        private const string TOOLBARNAME = "ExampleBar";
        private Outlook.Explorer explorer;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            explorer = this.Application.ActiveExplorer();

            if (explorer != null)
            {
                ((Outlook.ExplorerEvents_10_Event)explorer).Close +=
new Microsoft.Office.Interop.Outlook.ExplorerEvents_10_CloseEventHandler(ThisAddIn_Close);

                commandBar = this.Application.ActiveExplorer().CommandBars.Add(TOOLBARNAME,
                    Office.MsoBarPosition.msoBarFloating, false, true);

                button = commandBar.Controls.Add(
                    Office.MsoControlType.msoControlButton,
                    System.Type.Missing, System.Type.Missing,
                    1, true) as Office.CommandBarButton;

                button.Style =
                    Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;
                button.Caption = "Button 1";
                button.Tag = "newButton";

                button.Click += new Microsoft.Office.Core.
                    _CommandBarButtonEvents_ClickEventHandler(button_Click);

                LoadCommandBarSettings();
            }
        }

        private void LoadCommandBarSettings()
        {
            Microsoft.Office.Core.MsoBarPosition position =
               (Microsoft.Office.Core.MsoBarPosition)Properties.Settings
               .Default["CommandBarPosition"];

            commandBar.Position = position;

            int rowIndex =
                Convert.ToInt32(
                Properties.Settings.Default["CommandBarRowIndex"]);

            commandBar.RowIndex = rowIndex;

            int top =
                Convert.ToInt32(
                Properties.Settings.Default["CommandBarTop"]);

            commandBar.Top =
                top != 0 ? top : System.Windows.Forms.Screen.PrimaryScreen.
                Bounds.Height / 2;

            int left =
                Convert.ToInt32(
                Properties.Settings.Default["CommandBarLeft"]);

            commandBar.Left =
                left != 0 ? left : System.Windows.Forms.Screen.PrimaryScreen.
                Bounds.Width / 2;

            bool visible = Convert.ToBoolean(
                Properties.Settings.Default["CommandBarVisible"]);

            commandBar.Visible = visible;
        }

        private void SaveCommandBarSettings()
        {
            Properties.Settings.Default["CommandBarTop"] = commandBar.Top;
            Properties.Settings.Default["CommandBarLeft"] = commandBar.Left;
            Properties.Settings.Default["CommandBarVisible"] =
                commandBar.Visible;
            Properties.Settings.Default["CommandBarPosition"] =
                (int)commandBar.Position;
            Properties.Settings.Default["CommandBarRowIndex"] =
                commandBar.RowIndex;
            Properties.Settings.Default.Save();
        }

        void button_Click(Microsoft.Office.Core.CommandBarButton Ctrl,
            ref bool CancelDefault)
        {
            System.Windows.Forms.MessageBox.Show("Hello world!");
        }

        void ThisAddIn_Close()
        {
            SaveCommandBarSettings();
        }

코드 컴파일

이 예제에는 다음 사항이 필요합니다.

  • 다섯 가지 사용자 설정. 프로젝트의 속성(C#의 경우) 또는 내 프로젝트(Visual Basic의 경우) 폴더에서 Settings.settings 아이콘을 두 번 클릭하고 다음과 같은 사용자 범위 속성을 추가합니다.

    • CommandBarTop, type int, 기본값 = 0

    • CommandBarLeft, type int, 기본값 = 0

    • CommandBarVisible, type boolean, 기본값 = true

    • CommandBarPosition, type int, 기본값 = 4

    • CommandBarRowIndex, type int, 기본값 = 1

참고 항목

작업

방법: Office 도구 모음 만들기

방법: Excel에서 바로 가기 메뉴에 명령 추가

기타 리소스

Outlook 개체 모델 개요

Office UI 사용자 지정

Office 솔루션 디자인 및 만들기