Visual Studio Tools for Office: Using C# with Excel, Word, Outlook, and InfoPath【15】

Using document properties
DocumentProperties collection and DocumentProperty object is located in Microsoft Office 11.0 Object Library (office.dll), the object library contains all of the Office applications share objects. These objects are located Microsoft.Office.Core namespace, usually with Office namespace alias to enter your code as follows:

using Office = Microsoft.Office.Core;

Iteration DocumentProperties collection
list 5-15 shows an example of iterating the collection DocumentProperties Workbook.CustomDocumentProperties and Workbook.BuiltInDocumentProperties returned.

Listing 5-15 on the set of iterations VSTO customization DocumentProperties

Copy the code
private void ThisWorkbook_Startup(object sender, EventArgs e)
{
  Office.DocumentProperties customProps = this.
    CustomDocumentProperties as Office.DocumentProperties;

  Office.DocumentProperties builtinProps = this.
    BuiltinDocumentProperties as Office.DocumentProperties;

  foreach (Office.DocumentProperty builtinProp in builtinProps)
  {
    MessageBox.Show(String.Format(
      "{0}  {1}", builtinProp.Name, builtinProp.Value));
  }

  foreach (Office.DocumentProperty customProp in customProps)
  {
    MessageBox.Show(String.Format(
      "{0}  {1}", customProp.Name, customProp.Value));
  }
}
Copy the code

Access DocumentProperties collection DocumentProperty
To access DocumentProperties collection DocumentProperty, use the C # syntax index docProperty [object], the object returns a DocumentProperty object. Index Index parameter uses the object type. You can pass an int represents the set you want to access in DocumentProperty based index 1. Alternatively, you can pass a string name you want to access DocumentProperty representation. Like other collections, Count property returns the number of objects in the collection have DocumentProperty.

DocumentProperty object has a Name property, which returns a string containing the name of the attribute. It also has a Value property returns the property value of type object. You can return MsoDocProperties by using the Type property enumeration members to check the type of value: msoPropertyTypeBoolean, msoPropertyTypeDate, msoPropertyTypeFloat, msoPropertyTypeNumber or msoPropertyTypeString.

Listing 5-16 shows how to access DocumentProperty.

Listing 5-16 DocumentProperty use the index to access the custom VSTO

Copy the code
private void ThisWorkbook_Startup(object sender, EventArgs e)
{
  Office.DocumentProperties builtinProps = this.
    BuiltinDocumentProperties as Office.DocumentProperties;

  Office.DocumentProperty authorProp = builtinProps["Author"];

  MessageBox.Show(String.Format(
    "Property {0} is {1}", authorProp.Name, authorProp.Value));

  Office.DocumentProperty thirdProp = builtinProps[3];

  MessageBox.Show(String.Format(
    "Property {0} is {1}", thirdProp.Name, thirdProp.Value));
}
Copy the code

Add DocumentProperty
You can add custom DocumentProperty use the Add method. Add method using the parameters shown in Table 5-10.

The method of adding the parameter table set of 5-10 DocumentProperties

Listing 5-17 shows an example of adding msoPropertyTypeString types of custom DocumentProperty of. Please note, Excel will allow you to set the value to a long string, but it will be truncated to 255 characters. Fortunately, VSTO provided a method of storing large amounts of data in a document called the cached data by function developers. For more information about VSTO data caching function, see Chapter 18, "Server data plan."

Listing 5-17 add custom DocumentProperty the custom VSTO

Copy the code
private void ThisWorkbook_Startup(object sender, EventArgs e)
{
  Office.DocumentProperties props = this.
    CustomDocumentProperties as Office.DocumentProperties;

  Office.DocumentProperty prop = props.Add("My Property",
    false, Office.MsoDocProperties.msoPropertyTypeString,
    "My Value", missing);

  MessageBox.Show(String.Format(
    "Property {0} has value {1}.", prop.Name, prop.Value));
}
Copy the code

Use the Windows collection
Application.Windows property returns a Windows collection that allows you to iteration and access all the windows open in Excel. Similarly, Workbook.Windows property allows you to access a window associated with a particular workbook. These collections provide a method to arrange open windows. Windows collection method does not add a new window. Instead, it must call Workbook.NewWindow method.

Repeat Open Windows
Windows set has GetEnumerator method can be used in C # foreach keyword to iterate, as shown in Listing 5-18.

Listing 5-18 VSTO customization iteration set in Windows

Copy the code
private void ThisWorkbook_Startup(object sender, EventArgs e)
{
  Excel.Workbooks workbooks = this.Application.Workbooks;

  Excel.Workbook workbook1 = workbooks.Add(missing);
  Excel.Workbook workbook2 = workbooks.Add(missing);

  for (int i = 0; i < 10; i++)
  {
    workbook1.NewWindow();
    workbook2.NewWindow();
  }

  foreach (Excel.Window window in workbook1.Windows)
  {
    MessageBox.Show(String.Format(
      "Workbook1 Window: {0}", window.Caption));
  }

  foreach (Excel.Window window in this.Application.Windows)
  {
    MessageBox.Show(String.Format(
      "Application Window: {0}", window.Caption));
  }
}
Copy the code

In the collection window
to access the Windows collection window, you can use a method called get_Item returns a window. get_Item method object of type Index parameter. You can pass a string representing the Window title, or you can pass to the Windows collection-based indexing. You can use the Count property to check for the number of items from the given set. Listing 5-19 shows the title by getting a window-based indexing and transfer window.

Listing 5-19 using get_Item Gets a collection of windows from Windows VSTO Custom

Copy the code
private void ThisWorkbook_Startup(object sender, EventArgs e)
{
  string caption = "";
  Excel.Windows windows = this.Windows;

  if (windows.Count >= 1)
  {
    Excel.Window window = windows.get_Item(1);
    caption = window.Caption as string;
    MessageBox.Show(caption);
  }

  if (!String.IsNullOrEmpty(caption))
  {
    Excel.Window window2 = windows.get_Item(caption);
    string caption2 = window2.Caption as string;
    MessageBox.Show(caption2);
  }
}
Copy the code

Layout window
Excel window arrangements with various methods and synchronization of these windows, so that when a scrolling window, another window will scroll. Arrangement method may be arranged to set the window tiling, horizontal, vertical or cascade. This method also allows you the same workbook two or more windows simultaneously displayed, so that if a scrolling window, other windows rolling the same number. Table 5-11 shows the optional parameter passed to the arrangement method.

Optional parameters Table 5-11 arrangement method

The same workbook CompareSideBySideWith method lets you synchronize two windows or scrolling display window shows two different workbooks. This method accepts a string representing the title of the window with the currently active window for comparison. Window you want to compare with the current active window must be a member in order to secure the set you are using Windows, so you should use Application.Windows collection, because it contains all the open windows.

如清单5-20所示,重要的是激活要安排其窗口的工作簿。如果不这样做,将安排活动工作簿的窗口,而不是与Windows集合关联的工作簿。清单5-20还说明了Activate方法和Activate事件与Workbook对象冲突的问题。为了使编译器不要抱怨和IntelliSense工作,我们将Workbook转换为Excel._Workbook界面,让编译器知道我们想要的方法而不是事件。

清单5-20 安排和同步Windows的VSTO定制

 

Copy the code
private void ThisWorkbook_Startup(object sender, EventArgs e)
{
  Excel.Workbooks workbooks = this.Application.Workbooks;

  Excel.Workbook workbook1 = workbooks.Add(missing);
  Excel.Workbook workbook2 = workbooks.Add(missing);

  Excel.Window workbook1Window = workbook1.NewWindow();
  workbook2.NewWindow();

  ((Excel._Workbook)workbook1).Activate();

  workbook1.Windows.Arrange( Excel.XlArrangeStyle.xlArrangeStyleTiled, true, true, true);

  MessageBox.Show(String.Format( "Workbook {0} has its windows arranged tiled.", workbook1.Name));

  ((Excel._Workbook)workbook2).Activate();

  this.Application.Windows.CompareSideBySideWith( workbook1Window.Caption);

  MessageBox.Show(String.Format( "The windows {0} and {1} are synchronized", this.Application.ActiveWindow.Caption, workbook1Window.Caption));
}
Copy the code

 

 

操作窗口对象
Window对象表示一个Excel窗口。您可以使用Window对象来定位与工作簿关联的窗口。您还可以使用Window对象设置工作簿的显示设置,例如是否显示网格线和标题。

定位窗口
Window对象允许您定位和更改Excel在窗口中显示工作簿的方式。 Window具有XlWindowState类型的WindowState属性,可用于将窗口设置为xlMaximized,xlMinimized或xlNormal。

当WindowState设置为xlNormal时,可以使用Left,Top,Width和Height属性来定位窗口。这些属性是表示点的双重值,而不是屏幕像素。您可以使用Window的PointsToScreenPixelsX和PointsToScreenPixelsY方法将点转换为像素。

显示与窗口关联的设置
许多其他属性允许您控制窗口的显示。表5-12列出了一些最常用的。

表5-12 控制窗口显示的窗口属性

 

清单5-21显示了使用许多这些属性的示例。 请注意,我们添加对System.Drawing.dll的引用,以便我们可以使用ColorTranslator对象来设置GridlineColor属性。 ColorTranslator对象提供了一个名为ToOle的方法,该方法采用System.Drawing颜色,并将其转换为Ole颜色格式,即Office方法和属性所需的颜色格式。

清单5-21  控制窗口显示选项的VSTO自定义

Copy the code
private void ThisWorkbook_Startup(object sender, EventArgs e)
{
  Excel.Window win = this.NewWindow();

  win.WindowState = Excel.XlWindowState.xlNormal;
  win.Width = 200;
  win.Height = 200;
  win.Top = 8;
  win.Left = 8;
  win.DisplayGridlines = true;
  win.DisplayHeadings = false;
  win.DisplayHorizontalScrollBar = false;
  win.DisplayVerticalScrollBar = false;
  win.DisplayWorkbookTabs = false;
  win.EnableResize = false;

  win.GridlineColor = System.Drawing.ColorTranslator.
    ToOle(System.Drawing.Color.Blue);

  win.ScrollColumn = 10;
  win.ScrollRow = 20;
  win.Visible = true;
  win.Zoom = 150;
}
Copy the code

 

 

使用名称集合和名称对象
“名称”集合表示工作簿中已经给出名称的一组范围,以便可以通过公式中的名称或访问“名称”集合的代码访问该范围。 用户可以使用名称框创建和编辑名称,如图5-2所示,或者使用“插入”菜单中的“名称”菜单。 此外,名称有时由Excel的功能自动创建。 例如,当用户定义自定义打印区域时,Excel会创建名为Print_Area的命名范围。

迭代名称集合
Names集合有一个GetEnumerator方法,可以使用C#中的foreach关键字进行迭代。 例如,以下代码片段迭代与工作簿相关联的“Names”集合,并显示每个Name对象的名称以及以标准格式引用的范围的地址(例如“= Sheet1!$ A $ 5”)。

foreach (Excel.Name name in workbook.Names)
  {
    Console.WriteLine(String.Format(
      "{0} refers to {1}", name.Name, name.RefersTo));
  }

访问名称集合中的名称
要访问Names集合中的Name,您可以使用一个名为Item的方法,该方法具有三个可选参数,如表5-13所示。

表5-13  Item方法的可选参数

清单5-22显示了一些创建名称的代码,然后以多种方式访问它。 它使用Add方法创建名称,该方法使用名称对象的名称和新创建的名称引用的标准格式地址字符串(例如“= Sheet1!$ A $ 5”)。

清单5-22  创建名称对象并访问它的VSTO自定义

 

Copy the code
private void ThisWorkbook_Startup(object sender, EventArgs e)
{
  Excel.Names names = this.Names;

  names.Add("MyName", "=Sheet1!$A$5", missing, missing,
    missing, missing, missing, missing, missing,
    missing, missing);

  Excel.Name name1 = names.Item(missing, missing,
    "=Sheet1!$A$5");

  MessageBox.Show(String.Format(
    "Name: {0} RefersTo: {1} RefersToR1C1: {2} Count: {3}",
    name1.Name, name1.RefersTo, name1.RefersToR1C1,
    name1.RefersToRange.Cells.Count));

  Excel.Name name2 = names.Item("MyName", missing, missing);

  MessageBox.Show(String.Format(
    "Name: {0} RefersTo: {1} RefersToR1C1: {2} Count: {3}",
    name2.Name, name2.RefersTo, name2.RefersToR1C1,
    name2.RefersToRange.Cells.Count));
}
Copy the code

Name Object
Given a Name object, you would normally use several properties. Name Returns the name as a string. RefersTo standard format property returns the name as a string address references. RefersToR1C1 return "row and column" address format as a reference string name (e.g. "= Sheet1! R26C9"). Most importantly, RefersToRange property returns a name assigned to the range of cells Excel range object representation.

From the "Define Name" dialog box and "Name box" drop-down list to hide the name, Visible property can be set to false. To remove a name, use the Delete method.

Guess you like

Origin www.cnblogs.com/qiys/p/11367307.html