PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/Visual Studio 2008/CSVstoGetWrapperObject/GetWrapperForm.cs

#
C# | 167 lines | 98 code | 20 blank | 49 comment | 3 complexity | 3c034f682a56167a98b5f96c125eba56 MD5 | raw file
  1/************************************* Module Header **************************************\
  2* Module Name:	GetWrapperForm.cs
  3* Project:		CSVstoGetWrapperObject
  4* Copyright (c) Microsoft Corporation.
  5* 
  6* The CSVstoGetWrapperObject project demonstrates how to get a VSTO wrapper
  7* object from an existing Office COM object.
  8*
  9* This feature requires Visual Studio Tools for Office 3.0 SP1 (included in 
 10* Visual Studio 2008 SP1) for both design-time and runtime support.
 11* 
 12* This source is subject to the Microsoft Public License.
 13* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
 14* All other rights reserved.
 15* 
 16* History:
 17* * 6/13/2009 3:00 PM Jie Wang Created
 18\******************************************************************************************/
 19
 20#region Using directives
 21using System;
 22using System.Collections.Generic;
 23using System.ComponentModel;
 24using System.Data;
 25using System.Drawing;
 26using System.Linq;
 27using System.Text;
 28using System.Windows.Forms;
 29using Excel = Microsoft.Office.Interop.Excel;
 30using Microsoft.Office.Tools.Excel;
 31using Microsoft.Office.Tools.Excel.Extensions;
 32using System.Runtime.InteropServices;
 33using Microsoft.VisualStudio.Tools.Applications.Runtime;
 34#endregion
 35
 36
 37namespace CSVstoGetWrapperObject
 38{
 39    public partial class GetWrapperForm : Form
 40    {
 41        public GetWrapperForm()
 42        {
 43            InitializeComponent();
 44        }
 45
 46        private void GetWrapperForm_Load(object sender, EventArgs e)
 47        {
 48            btnRefreshWb.PerformClick();
 49        }
 50
 51        /// <summary>
 52        /// Refresh the Workbook list.
 53        /// </summary>
 54        /// <param name="sender"></param>
 55        /// <param name="e"></param>
 56        private void btnRefreshWb_Click(object sender, EventArgs e)
 57        {
 58            cboWorkbooks.Items.Clear();
 59
 60            foreach (Excel.Workbook wb in Globals.ThisAddIn.Application.Workbooks)
 61            {
 62                cboWorkbooks.Items.Add(wb);
 63            }
 64
 65            bool hasWorkbook = cboWorkbooks.Items.Count > 0;
 66
 67            cboWorksheets.Enabled = hasWorkbook;
 68            btnRefreshWs.Enabled = hasWorkbook;
 69            btnAddListObject.Enabled = hasWorkbook;
 70
 71            if (hasWorkbook)
 72            {
 73                cboWorkbooks.SelectedIndex = 0;
 74            }
 75        }
 76
 77        /// <summary>
 78        /// Workbook selection changed, refresh the Workseet list.
 79        /// </summary>
 80        /// <param name="sender"></param>
 81        /// <param name="e"></param>
 82        private void cboWorkbooks_SelectedIndexChanged(object sender, EventArgs e)
 83        {
 84            if (cboWorkbooks.SelectedItem != null)
 85            {
 86                btnRefreshWs.PerformClick();
 87            }
 88        }
 89
 90        /// <summary>
 91        /// Refresh the Worksheet list.
 92        /// </summary>
 93        /// <param name="sender"></param>
 94        /// <param name="e"></param>
 95        private void btnRefreshWs_Click(object sender, EventArgs e)
 96        {
 97            Excel.Workbook wb = (Excel.Workbook)cboWorkbooks.SelectedItem;
 98            cboWorksheets.Items.Clear();
 99
100            foreach (Excel.Worksheet ws in wb.Worksheets)
101            {
102                cboWorksheets.Items.Add(ws);
103            }
104
105            cboWorksheets.SelectedIndex = 0;
106        }
107
108        /// <summary>
109        /// Adds
110        /// </summary>
111        /// <param name="sender"></param>
112        /// <param name="e"></param>
113        private void btnAddListObject_Click(object sender, EventArgs e)
114        {
115            // This is Microsoft.Office.Interop.Excel.Worksheet (COM)
116            Excel.Worksheet ws = (Excel.Worksheet)cboWorksheets.SelectedItem;
117            ws.Activate();
118
119            // This is Microsoft.Office.Tools.Excel.Worksheet (VSTO wrapper)
120            Worksheet vstoWs = Worksheet.GetVstoObject(ws);
121
122            try
123            {
124                // Now we have the VSTO wrapper, add some VSTO objects to it...
125                // First a ListObject
126                ListObject lo = vstoWs.Controls.AddListObject(vstoWs.Range["A3", Type.Missing], "myTable");
127                // Try bind some data to the ListObject
128                lo.DataSource = GetDemoData();
129                lo.DataMember = "DemoTable";
130
131                // Now add a button.
132                Button btnVsto = vstoWs.Controls.AddButton(vstoWs.Range["A1", Type.Missing], "btnVSTO");
133                btnVsto.Text = "VSTO Button";
134                btnVsto.Width = 100;
135                btnVsto.Height = 23;
136                // Setup the button Click event handler.
137                btnVsto.Click += delegate(object s, EventArgs args)
138                {
139                    MessageBox.Show("VSTO button clicked.", "GetVstoObject demo", MessageBoxButtons.OK, MessageBoxIcon.Information);
140                };
141            }
142            catch (RuntimeException rtEx)
143            {
144                MessageBox.Show(rtEx.ToString(), "GetVstoObject demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
145            }
146        }
147
148        /// <summary>
149        /// Generates some data for ListObject databinding.
150        /// </summary>
151        /// <returns></returns>
152        private DemoData GetDemoData()
153        {
154            DemoData data = new DemoData();
155
156            data.DemoTable.Rows.Add(new object[] {null, "John", new DateTime(1978, 2, 20)});
157            data.DemoTable.Rows.Add(new object[] { null, "Eric", new DateTime(1987, 6, 12) });
158            data.DemoTable.Rows.Add(new object[] { null, "Mary", new DateTime(1980, 8, 10) });
159            data.DemoTable.Rows.Add(new object[] { null, "Mike", new DateTime(1991, 1, 9) });
160            data.DemoTable.Rows.Add(new object[] { null, "Joe", new DateTime(1983, 3, 31) });
161            data.DemoTable.Rows.Add(new object[] { null, "Lance", new DateTime(1988, 5, 11) });
162            data.DemoTable.Rows.Add(new object[] { null, "Tom", new DateTime(1970, 9, 30) });
163
164            return data;
165        }
166    }
167}