private static CustomXMLPart GetCustomXmlPart(Document doc, string xmlPartIdName, bool createNew = true, bool clearContent = true) { string xmlPartId = GetCustomProperty(doc, xmlPartIdName); CustomXMLPart xmlPart = null; if (string.IsNullOrEmpty(xmlPartId)) { if (createNew) { xmlPart = doc.CustomXMLParts.Add(); } } else { xmlPart = doc.CustomXMLParts.SelectByID(xmlPartId); if (xmlPart == null) { if (createNew) { xmlPart = doc.CustomXMLParts.Add(); } } else if (clearContent) { xmlPart.Delete(); xmlPart = doc.CustomXMLParts.Add(); } } return(xmlPart); }
public void Delete() { if (!FindAndSetIdByKey()) { throw new KeyNotFoundException(); } CustomXMLPart xmlPart = _document.CustomXMLParts.SelectByID(_id); xmlPart.Delete(); }
public void Delete_ValidXmlPart_RemovesIt() { // Arrange using (var workbook = this.ExcelApplication.Workbooks.Add()) { CustomXMLPart cxp1 = workbook.CustomXMLParts.Add(); cxp1.LoadXML("<discounts><discount>0.10</discount></discounts>"); // Act cxp1.Delete(); // Assert Assert.Pass(); } }
public static void RemapCustomXMLPart(CustomXMLPart xmlPart) { using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.Filter = "XML Files (*.xml)|*.xml|All files (*.*)|*.*"; openFileDialog.RestoreDirectory = true; { if (openFileDialog.ShowDialog() == DialogResult.OK) { XDocument tempXML = XDocument.Load(openFileDialog.FileName); tempXML.Root.Name = Globals.ThisAddIn.DEFAULT_NAMESPACE + tempXML.Root.Name.LocalName; xmlPart.Delete(); Globals.ThisAddIn.currentDocument.CustomXMLParts.Add(tempXML.ToString()); } } } }
/// <summary> /// Add custom xml part into active document /// </summary> /// <param name="customXmlPartContent"></param> /// <param name="partId"></param> /// <returns></returns> private string AddCustomXmlPart(string customXmlPartContent, string partId) { string id = string.Empty; try { ProtectLevel protectLevel = CurrentTemplateInfo.ProtectLevel; if (protectLevel == ProtectLevel.Bookmark) { UnprotectBookmark(); } if (protectLevel == ProtectLevel.Document) { UnprotectDocument(); } if (!string.IsNullOrEmpty(partId)) { CustomXMLPart oldXmlPart = CommonProfile.ActiveDoc.CustomXMLParts.SelectByID(partId); if (oldXmlPart != null) { oldXmlPart.Delete(); } } CustomXMLPart xmlPart = CommonProfile.ActiveDoc.CustomXMLParts.Add(); xmlPart.LoadXML(customXmlPartContent); id = xmlPart.Id; if (protectLevel == ProtectLevel.Bookmark) { ProtectBookmark(); } if (protectLevel == ProtectLevel.Document) { ProtectDocument(); } } catch { } return(id); }
public static void DoUpgrade(Word.Document doc) { string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()"; int sel = doc.Application.Selection.Range.Start; Globals.Chem4WordV3.DisableDocumentEvents(doc); try { string extension = doc.FullName.Split('.').Last(); string guid = Guid.NewGuid().ToString("N"); string timestamp = DateTime.Now.ToString("yyyyMMdd-HHmmss", CultureInfo.InvariantCulture); string destination = Path.Combine(Globals.Chem4WordV3.AddInInfo.ProductAppDataPath, "Backups", $"Chem4Word-{timestamp}-{guid}.{extension}"); File.Copy(doc.FullName, destination); } catch (Exception ex) { // Nothing much we can do here :-( Debug.WriteLine(ex.Message); } List <UpgradeTarget> targets = CollectData(doc); int upgradedCCs = 0; int upgradedXml = 0; foreach (var target in targets) { if (target.ContentControls.Count > 0) { upgradedXml++; upgradedCCs += target.ContentControls.Count; } foreach (var cci in target.ContentControls) { foreach (Word.ContentControl cc in doc.ContentControls) { if (cc.ID.Equals(cci.Id)) { if (cci.Type.Equals("2D")) { cc.LockContents = false; cc.Title = Constants.ContentControlTitle; cc.Tag = target.Model.CustomXmlPartGuid; cc.LockContents = true; } else { cc.LockContents = false; cc.Range.Delete(); int start = cc.Range.Start; cc.Delete(); doc.Application.Selection.SetRange(start - 1, start - 1); bool isFormula = false; string source; string text = CustomRibbon.GetInlineText(target.Model, cci.Type, ref isFormula, out source); Word.ContentControl ccn = CustomRibbon.Insert1D(doc.Application, doc, text, isFormula, $"{cci.Type}:{target.Model.CustomXmlPartGuid}"); ccn.LockContents = true; } } } } CMLConverter converter = new CMLConverter(); CustomXMLPart cxml = doc.CustomXMLParts.SelectByID(target.CxmlPartId); cxml.Delete(); doc.CustomXMLParts.Add(converter.Export(target.Model)); } EraseChemistryZones(doc); Globals.Chem4WordV3.EnableDocumentEvents(doc); doc.Application.Selection.SetRange(sel, sel); if (upgradedCCs + upgradedXml > 0) { Globals.Chem4WordV3.Telemetry.Write(module, "Information", $"Upgraded {upgradedCCs} Chemistry Objects for {upgradedXml} Structures"); UserInteractions.AlertUser($"Upgrade Completed{Environment.NewLine}{Environment.NewLine}Upgraded {upgradedCCs} Chemistry Objects for {upgradedXml} Structures"); } }
internal void AddPictureContentControl(Utility _utility) { Microsoft.Office.Tools.Word.Document vstoDocument = Globals.Factory.GetVstoObject(this.Application.ActiveDocument); Microsoft.Office.Interop.Word.Selection selection = this.Application.Selection; if (selection != null && selection.Range != null) { WdSelectionType sel = selection.Type; // inlineshape // if (sel == WdSelectionType.wdSelectionInlineShape) { // InlineShapes shape = vstoDocument.InlineShapes; //shape[0]. MemoryStream ms = new MemoryStream(_utility.BitmapBytes); Image _drawnimage = Image.FromStream(ms); //_drawnimage.Save("temp.jpg"); // Word.ContentControl contentControl = Globals.ThisAddIn.Application.ActiveDocument.SelectContentControlsByTitle(_utility.TagName)[0]; //foreach (Word.ContentControl contentControl in vstoDocument.Content.ContentControls) { // WdContentControlType type = contentControl.Type; //if (type == WdContentControlType.wdContentControlPicture && contentControl.Tag.Equals(_utility.TagName)) { //object missing = Type.Missing; //Microsoft.Office.Interop.Word.ContentControl contentControl = vstoDocument.ContentControls.Add(WdContentControlType.wdContentControlPicture, // ref missing); //contentControl.Range.InlineShapes.AddPicture("temp.jpg", ref missing, ref missing, // ref missing); //contentControl.Title = "violet"; Microsoft.Office.Interop.Word.ContentControl cp2 = selection.Range.ParentContentControl; if (cp2 != null)//update { CustomXMLPart xmlpart = vstoDocument.CustomXMLParts.SelectByID(cp2.Tag); xmlpart.Delete(); cp2.Delete(); } CustomXMLPart cp = vstoDocument.CustomXMLParts.Add(_utility.xml); PictureContentControl piccontrol = vstoDocument.Controls.AddPictureContentControl(selection.Range, Guid.NewGuid().ToString()); piccontrol.Image = _drawnimage;// ScaleImage(_drawnimage, 200, 150);//Save(new Bitmap(returnImage), 270, 180, 0); piccontrol.Title = "violet"; piccontrol.Tag = cp.Id; } } // vstoDocument.Save(); ms.Flush(); ms.Close(); } } }
public static void DoUpgrade(Word.Document doc) { string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()"; int sel = doc.Application.Selection.Range.Start; Globals.Chem4WordV3.DisableDocumentEvents(doc); List <UpgradeTarget> targets = CollectData(doc); int upgradedCCs = 0; int upgradedXml = 0; foreach (var target in targets) { if (target.ContentControls.Count > 0) { upgradedXml++; upgradedCCs += target.ContentControls.Count; } foreach (var cci in target.ContentControls) { foreach (Word.ContentControl cc in doc.ContentControls) { if (cc.ID.Equals(cci.Id)) { if (cci.Type.Equals("2D")) { cc.LockContents = false; cc.Title = Constants.ContentControlTitle; cc.Tag = target.Model.CustomXmlPartGuid; cc.LockContents = true; } else { cc.LockContents = false; cc.Range.Delete(); int start = cc.Range.Start; cc.Delete(); doc.Application.Selection.SetRange(start - 1, start - 1); bool isFormula = false; string source; string text = CustomRibbon.GetInlineText(target.Model, cci.Type, ref isFormula, out source); Word.ContentControl ccn = CustomRibbon.Insert1D(doc.Application, doc, text, isFormula, $"{cci.Type}:{target.Model.CustomXmlPartGuid}"); ccn.LockContents = true; } } } } CMLConverter converter = new CMLConverter(); CustomXMLPart cxml = doc.CustomXMLParts.SelectByID(target.CxmlPartId); cxml.Delete(); doc.CustomXMLParts.Add(converter.Export(target.Model)); } EraseChemistryZones(doc); Globals.Chem4WordV3.EnableDocumentEvents(doc); doc.Application.Selection.SetRange(sel, sel); if (upgradedCCs + upgradedXml > 0) { Globals.Chem4WordV3.Telemetry.Write(module, "Information", $"Upgraded {upgradedCCs} Chemistry Objects for {upgradedXml} Structures"); UserInteractions.AlertUser($"Upgrade Completed{Environment.NewLine}{Environment.NewLine}Upgraded {upgradedCCs} Chemistry Objects for {upgradedXml} Structures"); } }