Uncategorized

Beta Experience セミナーのサンプルコード (Open XML, Custom XML, Custom Taskpane のデモ)

環境:
Office Professional 2007 Beta 2
VSTO v3 JuneCTP

(→ このサンプルの Beta2TR 及び VSTO 2005 SE Beta 版は、ここ を参照してください)

こんにちは、松崎です。

2006/07/25 に実施予定の Beta Experience セミナーでご紹介するサンプルコードを Orland からアップします。
コードの詳細は、Beta Experience セミナーをご覧ください。

=================================================
Custom Taskpane の表示

public UserControl1 myControl;public Microsoft.Office.Tools.CustomTaskPane myTaskpane;private void ThisApplication_Startup(object sender, System.EventArgs e){myControl = new UserControl1();myTaskpane = this.CustomTaskPanes.Add(myControl, "My Test");myTaskpane.Visible = false;}

=================================================

OpenXML における Custom XML Part の活用

添付:sample.docx      サンプルの解説で使用している docx ファイル
添付:test1.xsn           item3.xml を作成した InfoPath のフォームテンプレート

=================================================
Word Content Control への カスタムXML Part の XML ノードのバインド追加

<w:dataBinding w:prefixMappings="xmlns:c='http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-07-18T03:20:17'" w:xpath="/c:myFields/c:Name" w:storeItemID="{3CC358D1-412F-459D-86EE-17DDD81CFED8}"/>

=================================================
OpenXML へのプログラムからのアクセス

using System;using System.Collections.Generic;using System.Text;using System.IO;using System.IO.Packaging;using System.Xml;namespace ConsoleApplication1{class Program{static void Main(string[] args){string xmlNS = @"http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-07-18T03:20:17";string xmlDocRelType = @"http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml";string officeDocRelType = @"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";PackagePart documentPart = null;PackagePart customxmlPart = null;Uri documentUri = null;Uri customxmlUri = null;// Prompt userConsole.WriteLine("Please input your name.");string paramName = Console.ReadLine();Console.WriteLine("Please input your birth (yyyy-mm-dd).");string paramBirth = Console.ReadLine();// Open the docx container as a System.IO.Packaging.Package.using (Package docPackage = Package.Open(@"D:\work\sample_test.docx", FileMode.Open, FileAccess.ReadWrite)){// find the "documentRoot" partforeach (PackageRelationship relationship in docPackage.GetRelationshipsByType(officeDocRelType)){documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);documentPart = docPackage.GetPart(documentUri);break;  // document root part will be one, so exit soon !}//find the "customXML" partforeach (PackageRelationship relationship in documentPart.GetRelationshipsByType(xmlDocRelType)){customxmlUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);if (CheckNamespace(docPackage.GetPart(customxmlUri), xmlNS) == true){customxmlPart = docPackage.GetPart(customxmlUri);break;}}// Change values !XmlDocument customxmlXml = new XmlDocument();customxmlXml.Load(customxmlPart.GetStream());XmlNamespaceManager customxmlXmlNSMgr = new XmlNamespaceManager(customxmlXml.NameTable);customxmlXmlNSMgr.AddNamespace("my", xmlNS);XmlNode nodeName = customxmlXml.SelectSingleNode("/my:myFields/my:Name", customxmlXmlNSMgr);nodeName.InnerText = paramName;XmlNode nodeBirth = customxmlXml.SelectSingleNode("/my:myFields/my:Birth", customxmlXmlNSMgr);nodeBirth.InnerText = paramBirth;// Save !customxmlXml.Save(customxmlPart.GetStream(FileMode.Create, FileAccess.Write));}}static bool CheckNamespace(PackagePart thisPart, string matchNameSpace){Stream outputStream = thisPart.GetStream(FileMode.Open, FileAccess.ReadWrite);StreamReader readStream = new StreamReader(outputStream);XmlDocument thisDoc = new XmlDocument();thisDoc.LoadXml(readStream.ReadToEnd());if (thisDoc.DocumentElement.NamespaceURI == matchNameSpace){return true;}else{return false;}}}}

=================================================
InfoPath VSTA を使った実装

Dim root, id As System.Xml.XPath.XPathNavigatorroot = Me.MainDataSource.CreateNavigator()id = root.SelectSingleNode("/my:myFields/my:field2", Me.NamespaceManager)If id.Value.Equals("") Then  id.SetValue(Guid.NewGuid().ToString())End If

=================================================

以上です

attachments.zip

Categories: Uncategorized

Tagged as:

4 replies»

  1. Hospitality mind Beta Experience セミナー (2006/07/25) デモ サンプルコード Beta Experience セミナー (2006/07/25) デモ サンプルコード (2) 〜ている (teiru) an experience of a difficulty of breathing in europe Making Sense of Japanese: What the Textbooks Don

    Like

  2. こんにちは、こだかです。 今回はVSTOのアプリケーションレベルでの実装をご紹介します。 アプリケーションレベルの実装は、WordならWordに、ExcelならExcelに機能を追加するものになり、 ドキュメントレベルの実装とは異なり、個々のドキュメントとは直接関連しません。

    Like

  3. 環境: Office Professional 2007 Beta 2 VSTO v3 JuneCTP (→ このサンプルの Beta2TR 及び VSTO 2005 SE Beta 版は、 ここ を参照してください) こんにちは、松崎です。 2006/07/25 に実施予定の Beta Experience セミナーでご紹介するサンプルコードを Orland からアップします。 コードの詳細は、Beta Experience セミナーをご覧ください。 ================================================

    Like

  4. 環境: Office Professional 2007 Beta 2 VSTO v3 JuneCTP (→ このサンプルの Beta2TR 及び VSTO 2005 SE Beta 版は、 ここ を参照してください) こんにちは、松崎です。 2006/07/25 に実施予定の Beta Experience セミナーでご紹介するサンプルコードを Orland からアップします。 コードの詳細は、Beta Experience セミナーをご覧ください。 ================================================

    Like

Leave a reply to Weddings Cancel reply