PageRenderTime 101ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/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. #region Using directives
  20. using System;
  21. using System.Collections.Generic;
  22. using System.ComponentModel;
  23. using System.Data;
  24. using System.Drawing;
  25. using System.Linq;
  26. using System.Text;
  27. using System.Windows.Forms;
  28. using Excel = Microsoft.Office.Interop.Excel;
  29. using Microsoft.Office.Tools.Excel;
  30. using Microsoft.Office.Tools.Excel.Extensions;
  31. using System.Runtime.InteropServices;
  32. using Microsoft.VisualStudio.Tools.Applications.Runtime;
  33. #endregion
  34. namespace CSVstoGetWrapperObject
  35. {
  36. public partial class GetWrapperForm : Form
  37. {
  38. public GetWrapperForm()
  39. {
  40. InitializeComponent();
  41. }
  42. private void GetWrapperForm_Load(object sender, EventArgs e)
  43. {
  44. btnRefreshWb.PerformClick();
  45. }
  46. /// <summary>
  47. /// Refresh the Workbook list.
  48. /// </summary>
  49. /// <param name="sender"></param>
  50. /// <param name="e"></param>
  51. private void btnRefreshWb_Click(object sender, EventArgs e)
  52. {
  53. cboWorkbooks.Items.Clear();
  54. foreach (Excel.Workbook wb in Globals.ThisAddIn.Application.Workbooks)
  55. {
  56. cboWorkbooks.Items.Add(wb);
  57. }
  58. bool hasWorkbook = cboWorkbooks.Items.Count > 0;
  59. cboWorksheets.Enabled = hasWorkbook;
  60. btnRefreshWs.Enabled = hasWorkbook;
  61. btnAddListObject.Enabled = hasWorkbook;
  62. if (hasWorkbook)
  63. {
  64. cboWorkbooks.SelectedIndex = 0;
  65. }
  66. }
  67. /// <summary>
  68. /// Workbook selection changed, refresh the Workseet list.
  69. /// </summary>
  70. /// <param name="sender"></param>
  71. /// <param name="e"></param>
  72. private void cboWorkbooks_SelectedIndexChanged(object sender, EventArgs e)
  73. {
  74. if (cboWorkbooks.SelectedItem != null)
  75. {
  76. btnRefreshWs.PerformClick();
  77. }
  78. }
  79. /// <summary>
  80. /// Refresh the Worksheet list.
  81. /// </summary>
  82. /// <param name="sender"></param>
  83. /// <param name="e"></param>
  84. private void btnRefreshWs_Click(object sender, EventArgs e)
  85. {
  86. Excel.Workbook wb = (Excel.Workbook)cboWorkbooks.SelectedItem;
  87. cboWorksheets.Items.Clear();
  88. foreach (Excel.Worksheet ws in wb.Worksheets)
  89. {
  90. cboWorksheets.Items.Add(ws);
  91. }
  92. cboWorksheets.SelectedIndex = 0;
  93. }
  94. /// <summary>
  95. /// Adds
  96. /// </summary>
  97. /// <param name="sender"></param>
  98. /// <param name="e"></param>
  99. private void btnAddListObject_Click(object sender, EventArgs e)
  100. {
  101. // This is Microsoft.Office.Interop.Excel.Worksheet (COM)
  102. Excel.Worksheet ws = (Excel.Worksheet)cboWorksheets.SelectedItem;
  103. ws.Activate();
  104. // This is Microsoft.Office.Tools.Excel.Worksheet (VSTO wrapper)
  105. Worksheet vstoWs = Worksheet.GetVstoObject(ws);
  106. try
  107. {
  108. // Now we have the VSTO wrapper, add some VSTO objects to it...
  109. // First a ListObject
  110. ListObject lo = vstoWs.Controls.AddListObject(vstoWs.Range["A3", Type.Missing], "myTable");
  111. // Try bind some data to the ListObject
  112. lo.DataSource = GetDemoData();
  113. lo.DataMember = "DemoTable";
  114. // Now add a button.
  115. Button btnVsto = vstoWs.Controls.AddButton(vstoWs.Range["A1", Type.Missing], "btnVSTO");
  116. btnVsto.Text = "VSTO Button";
  117. btnVsto.Width = 100;
  118. btnVsto.Height = 23;
  119. // Setup the button Click event handler.
  120. btnVsto.Click += delegate(object s, EventArgs args)
  121. {
  122. MessageBox.Show("VSTO button clicked.", "GetVstoObject demo", MessageBoxButtons.OK, MessageBoxIcon.Information);
  123. };
  124. }
  125. catch (RuntimeException rtEx)
  126. {
  127. MessageBox.Show(rtEx.ToString(), "GetVstoObject demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
  128. }
  129. }
  130. /// <summary>
  131. /// Generates some data for ListObject databinding.
  132. /// </summary>
  133. /// <returns></returns>
  134. private DemoData GetDemoData()
  135. {
  136. DemoData data = new DemoData();
  137. data.DemoTable.Rows.Add(new object[] {null, "John", new DateTime(1978, 2, 20)});
  138. data.DemoTable.Rows.Add(new object[] { null, "Eric", new DateTime(1987, 6, 12) });
  139. data.DemoTable.Rows.Add(new object[] { null, "Mary", new DateTime(1980, 8, 10) });
  140. data.DemoTable.Rows.Add(new object[] { null, "Mike", new DateTime(1991, 1, 9) });
  141. data.DemoTable.Rows.Add(new object[] { null, "Joe", new DateTime(1983, 3, 31) });
  142. data.DemoTable.Rows.Add(new object[] { null, "Lance", new DateTime(1988, 5, 11) });
  143. data.DemoTable.Rows.Add(new object[] { null, "Tom", new DateTime(1970, 9, 30) });
  144. return data;
  145. }
  146. }
  147. }