Jerry Nixon @Work: December 2008

Jerry Nixon on Windows

Friday, December 19, 2008

Word 2007 & VSTO 3/2008 & adding a Custom Task Pane (no, not Custom Actions Pane)

Why didn't the Microsoft Visual Studio Tools for Office 3 for Microsoft Visual Studio 2008 call them Custom Action Panes like Excel? Maybe because they have a knack for naming things. Who knows. But Globals.ThisAddIn.CustomTaskPanes is the collection you'll need.

Document-specific or Global

Because I am using a Word Add In instead of a Document-type project, I should assign the task pane to a specific document - the user might have more than one open and only desire my task pane on the current one.

There should be only one

In a Word Add In, if you add a custom task pane more than once, you actually get more than one task pane! Isn't that amazing? So we will need to first check to make sure the task pane has not already been added.

Choose your own title

When you add a custom task pane you can give it a title. Wonderful. In Excel you're stuck with "document actions" - lame. I talked with the VSTO product team and they confirmed that there's no option to rename it.

Remember task pane orientation

Most Office users and, subsequently, most developers don't even know a task pane can be dragged to other parts of Office - including a horizontal disposition on the bottom or top of the document. Handle it.

Here's the Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;
using Microsoft.Office.Tools.Word.Extensions;
using System.Windows.Forms;
using Microsoft.Office.Tools;

public partial class ThisAddIn
{
public void ShowTaskPane()
{
// what word doc gets this task pane?

Microsoft.Office.Interop.Word.Window _Window;
_Window = this.Application.ActiveDocument.ActiveWindow;

// only one per document

foreach (CustomTaskPane _Item in CustomTaskPanes)
if (_Item.Window == _Window)
{
// make sure you can see it

_Item.Visible = true;
return;
}

// what control will we show in the task pane?

UserControl _Control;
_Control = new CustomPanes.Test();

// what will we call our task pane?

string _Title;
_Title = "My Special Title";

// add the task pane

CustomTaskPane _CustomTaskPane;
_CustomTaskPane =
CustomTaskPanes.Add(_Control, _Title, _Window);

// restrict orientation

_CustomTaskPane.DockPositionRestrict =
Microsoft.Office.Core.MsoCTPDockPositionRestrict
.msoCTPDockPositionRestrictNoHorizontal;

// make sure you can see the new task pane

_CustomTaskPane.Visible = true;
}
}

Tuesday, December 9, 2008

Validate XML/XSD using XmlReaderSettings

In the .Net Framework 3.5, the XmlValidationReader was deprecated changing the syntax used to validate XML against an XSD schema. If you use it you will get this message: "Use XmlReader created by XmlReader.Create() method using appropriate XmlReaderSettings instead. http://go.microsoft.com/fwlink/?linkid=14202".

 

I put together a simple reusable method to demonstrate the new approach.

 

using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Schema;

/// <summary>
/// Validate XML against XSD
/// </summary>
/// <param name="xml">raw XML string</param>
/// <param name="xsd">raw XSD string</param>
/// <returns>bool validation result</returns>
static bool ValidateXml(string xml, string xsd)
{
try
{
// build XSD schema

StringReader _XsdStream;
_XsdStream = new StringReader(xsd);

XmlSchema _XmlSchema;
_XmlSchema = XmlSchema.Read(_XsdStream, null);

// build settings (this replaces XmlValidatingReader)

XmlReaderSettings _XmlReaderSettings;
_XmlReaderSettings = new XmlReaderSettings()
{
ValidationType = ValidationType.Schema
};
_XmlReaderSettings.Schemas.Add(_XmlSchema);

// build XML reader

StringReader _XmlStream;
_XmlStream = new StringReader(xml);

XmlReader _XmlReader;
_XmlReader = XmlReader.Create(_XmlStream, _XmlReaderSettings);

// validate

using (_XmlReader)
{
while (_XmlReader.Read())
;
}

// validation succeeded

return true;
}
catch
{
// validation failed

return false;
}
}

Friday, December 5, 2008

Intel's new Core i7 Extreme Processor

Intel's new super-processor is the Core i7. It is 4-core and 8-thread at 3.2Ghz. It has "Turbo Boost Technology" for physics and AI in gaming (with supported hardware). Like its predecessor it is hyper-threaded. Unlike its predecessor it can be over-clocked.

The Core i7 can encode video 79% faster than the Intel Core 2 Extreme Qx9770. It's computational power is 25% faster. However, given its power and head ratings - don't expect these hummers in laptops any time soon.