Beta Experience セミナー (2006/07/25) デモ サンプルコード
環境:
Office Professional 2007 Beta 2
VSTO v3 JuneCTP
(→ このサンプルの Beta2TR 及び VSTO 2005 SE Beta 版は、ここ を参照してください)
こんにちは、松崎です。
2006/07/25 に実施予定の Beta Experience セミナーでご紹介するサンプルコードを Orland からアップします。
コードの詳細は、Beta Experience セミナーをご覧ください。
=================================================
カスタムタスクペインの表示
/** リボンなどから表示の切り替えをおこなう場合などは、下記の Visible の値を状態に応じて変更してください **/
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 における カスタム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 user
Console.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" part
foreach (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" part
foreach (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.XPathNavigator
root = Me.MainDataSource.CreateNavigator()
id = root.SelectSingleNode("/my:myFields/my:field2", Me.NamespaceManager)
If id.Value.Equals("") Then
id.SetValue(Guid.NewGuid().ToString())
End If
=================================================
以上です