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"); } }
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"); } }