1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
public static string GetKeyCustomXmlPart()
{
try
{
string PathCopy = @"C:\TOTO";
Microsoft.Office.Interop.Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
object d = "";
//doc.Unprotect(ref d);
doc.SaveAsQuickStyleSet(PathCopy);
int Flag = 0;
Uri uritarget;
using (Package packageword = Package.Open(@"C:\TOTO.dotx", FileMode.Open, FileAccess.ReadWrite))
{
for (int i = 1; true; i++)
{
if (Flag != 1)
uritarget = new Uri("/customXml/item" + i.ToString() + ".xml", UriKind.Relative);
else
uritarget = new Uri("/customXml/itemProps" + (i - 1).ToString() + ".xml", UriKind.Relative);
if (packageword.PartExists(uritarget))
{
IEnumerator<PackagePart> relEnum = packageword.GetParts().GetEnumerator();
relEnum.MoveNext();
PackagePart sd = packageword.GetPart(uritarget);
XmlDocument mainDoc = new XmlDocument();
mainDoc.Load(sd.GetStream());
if (Flag == 1)
{
packageword.Flush();
packageword.Close();
File.Delete(@"C:\TOTO.dotx");
return mainDoc["ds:datastoreItem"].Attributes.Item(0).Value;
}
/* il faut definir votre xmlns="info" ou autre*/
else if (mainDoc.FirstChild.Attributes.Item(0).Value.Equals("info"))
Flag = 1;
}
else
break;
}
packageword.Flush();
packageword.Close();
File.Delete(@"C:\TOTO.dotx");
}
}
catch (Exception ex)
{
MessageBox.Show("Error :" + ex.TargetSite.Name + "\n" + ex.Message);
}
return String.Empty;
}
public static void AddCustomXMLPart()
{
string CustomXmlStr = "<exemple xmlns='info'></exemple>";
if (!CustomXmlKey.Equals(String.Empty))
DelCustomXMLPart();
try
{
Microsoft.Office.Interop.Word.Document ActiveDocument = Globals.ThisAddIn.Application.ActiveDocument;
Microsoft.Office.Core.CustomXMLPart CustomXml = ActiveDocument.CustomXMLParts.Add(String.Empty, new Microsoft.Office.Core.CustomXMLSchemaCollectionClass());
CustomXml.LoadXML(CustomXmlStr);
CustomXmlKey = CustomXml.Id;
}
catch (Exception ex)
{
MessageBox.Show("Error :" + ex.TargetSite.Name + "\n" + ex.Message);
}
} |