Stuart Cox’s Tech Punch

Entries categorized as ‘Word’

Attach a Custom XML Schema to a Word Document Programmatically

August 27, 2008 · Leave a Comment

If you find the need to add a custom XML schema to a Word document or other Office document programmatically, this function might come in handy.  This function uses the Word InterOp assemblies to attach a schema to a document.

using System;
using System.Xml;
using System.Xml.Schema;

using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
...

/// <summary>
/// Attaches a custom xml schema to a document
/// </summary>
/// <param name="document">The Word document to modify</param>
/// <param name="schemaUrl">The url to the schema</param>
/// <param name="schemaNamespaceUri">The schema namespace defined in the schema</param>
/// <param name="schemaAlias">The alias used for the schema</param>
public static void AttachCustomXmlSchema(Word.Document document, string schemaUrl,
            string schemaNamespaceUri, string schemaAlias)
{
    try
    {
        // Objects to use when passing strings as ref to word
        object schemaNamespaceUriObj = schemaNamespaceUri;
        object schemaAliasObj = schemaAlias;

        // Find the custom xml namespace (if we have one declared)
        Word.XMLNamespace customXmlNamespace = null;
        try
        {
            // Find the custom xml namespace
            customXmlNamespace = document.Application.XMLNamespaces.get_Item(ref
                        schemaNamespaceUriObj);
        }
        catch { };

        // Did we find it the namespace?
        if (customXmlNamespace == null)
        {
            // We didn't find it
            // Add the custom xml schema to the available namespaces
            customXmlNamespace = document.Application.XMLNamespaces.Add(schemaUrl,
                        ref schemaNamespaceUriObj, ref schemaAliasObj, true);
        }

        // Attach the custom xml schema to the document
        object documentObj = document;
        customXmlNamespace.AttachToDocument(ref documentObj);
    }
    catch (Exception ex)
    {
        string attachFailed = string.Format("Unable to attach the {0} xml schema",
                schemaAlias);
        throw new ApplicationException(attachFailed);
    }
}

 

Share This Post


del.icio.us it! digg it! reddit! technorati! yahoo!

Categories: C# · Office Business Applications (OBA) · VSTO · Word

What is OBA anyway?

August 20, 2008 · Leave a Comment

OBA (Office Business Applications) is using Microsoft Office products and related applications to put your applications in the hands of users where they spend most of their time every day, in Office applications.  It’s a ubiquitous term, but at the heart of it, OBA solutions deliver information and functionality from a variety of systems to the user where they need it most.

OBA’s can expose data from custom applications and ERP systems, or even merged data from multiple systems, and deliver custom UI’s and custom automation interfaces available directly in Word, Excel, PowerPoint or Outlook.  You can use VSTO tools to build Office Add-Ins and templates that can inject data from these applications into their documents, or simply to display inside these applications as users work. 

With Visual Studio 2008 and VSTO, Microsoft has made it easy to surface your data inside the Office suite in a variety of ways.  The flexibility is stunning, and will spin your head a bit.  The new toolset, and the integration of Office and Visual Studio, Microsoft has given us the power to build some amazing applications.

When you build OBA’s, you’ll be leveraging the following tools:

Microsoft Office SharePoint Server 2007 (MOSS)

  • Web Site Provisioning
  • Custom Lists and Document Libraries
  • Content Types
  • Business Data Catalog
  • Forms Server
  • Excel Services
  • BI Dashboards and KPI’s
  • Custom Features and Solutions
    • Custom Content Types
    • Custom Lists
    • Workflow
    • Event Receivers
    • Custom web parts

Office 2007

  • Document Information Panels
  • Custom Ribbon Add-In’s
  • Custom Task and Action Panes
  • Custom Add-Ins
  • Code-Behind Templates
  • Custom XML Parts
  • Word Custom Controls
  • Excel List Databinding
  • Excel User-Defined Functions

Visual Studio 2008 and the .Net Framework

  • SharePoint Project Types
  • Office Project Types
  • Integrated Debugging and UI Support
  • Click-Once Deployment

Here’s a list of resources to get you going

Categories: .Net · Excel · MOSS 2007 · Microsoft Office · Office Business Applications (OBA) · Outlook · PowerPoint · SharePoint · VSTO · Visual Studio · WSS 3.0 · Word