diff --git a/NoteHighlightAddin/AddIn.cs b/NoteHighlightAddin/AddIn.cs index 847038d..f9c1dcf 100644 --- a/NoteHighlightAddin/AddIn.cs +++ b/NoteHighlightAddin/AddIn.cs @@ -138,11 +138,18 @@ public void OnStartupComplete(ref Array custom) //public async Task AddInButtonClicked(IRibbonControl control) public void AddInButtonClicked(IRibbonControl control) { - tag = control.Tag; + try + { + tag = control.Tag; - Thread t = new Thread(new ThreadStart(ShowForm)); - t.SetApartmentState(ApartmentState.STA); - t.Start(); + Thread t = new Thread(new ThreadStart(ShowForm)); + t.SetApartmentState(ApartmentState.STA); + t.Start(); + } + catch (Exception e) + { + MessageBox.Show("Exception from AddInButtonClicked: "+ e.ToString()); + } //t.Join(5000); @@ -151,49 +158,56 @@ public void AddInButtonClicked(IRibbonControl control) private void ShowForm() { - string outFileName = Guid.NewGuid().ToString(); - - //try - //{ - //ProcessHelper processHelper = new ProcessHelper("NoteHighLightForm.exe", new string[] { control.Tag, outFileName }); - //processHelper.IsWaitForInputIdle = true; - //processHelper.ProcessStart(); + try + { + string outFileName = Guid.NewGuid().ToString(); - //CodeForm form = new CodeForm(tag, outFileName); - //form.ShowDialog(); + //try + //{ + //ProcessHelper processHelper = new ProcessHelper("NoteHighLightForm.exe", new string[] { control.Tag, outFileName }); + //processHelper.IsWaitForInputIdle = true; + //processHelper.ProcessStart(); - //TestForm t = new TestForm(); - var pageNode = GetPageNode(); - string selectedText = ""; - XElement outline = null; - bool selectedTextFormated = false; + //CodeForm form = new CodeForm(tag, outFileName); + //form.ShowDialog(); - if (pageNode != null) - { - var existingPageId = pageNode.Attribute("ID").Value; - selectedText = GetSelectedText(existingPageId, out selectedTextFormated); + //TestForm t = new TestForm(); + var pageNode = GetPageNode(); + string selectedText = ""; + XElement outline = null; + bool selectedTextFormated = false; - if (selectedText.Trim() != "") + if (pageNode != null) { - outline = GetOutline(existingPageId); + var existingPageId = pageNode.Attribute("ID").Value; + selectedText = GetSelectedText(existingPageId, out selectedTextFormated); + + if (selectedText.Trim() != "") + { + outline = GetOutline(existingPageId); + } } - } MainForm form = new MainForm(tag, outFileName, selectedText); - System.Windows.Forms.Application.Run(form); - //} - //catch (Exception ex) - //{ - // MessageBox.Show("Error executing NoteHighLightForm.exe:" + ex.Message); - // return; - //} + System.Windows.Forms.Application.Run(form); + //} + //catch (Exception ex) + //{ + // MessageBox.Show("Error executing NoteHighLightForm.exe:" + ex.Message); + // return; + //} - string fileName = Path.Combine(Path.GetTempPath(), outFileName + ".html"); + string fileName = Path.Combine(Path.GetTempPath(), outFileName + ".html"); - if (File.Exists(fileName)) + if (File.Exists(fileName)) + { + InsertHighLightCodeToCurrentSide(fileName, form.Parameters, outline, selectedTextFormated); + } + } + catch (Exception e) { - InsertHighLightCodeToCurrentSide(fileName, form.Parameters, outline, selectedTextFormated); + MessageBox.Show("Exception from ShowForm: " + e.ToString()); } } @@ -231,24 +245,31 @@ public IStream GetImage(string imageName) /// private void InsertHighLightCodeToCurrentSide(string fileName, HighLightParameter parameters, XElement outline, bool selectedTextFormated) { - // Trace.TraceInformation(System.Reflection.MethodBase.GetCurrentMethod().Name); - string htmlContent = File.ReadAllText(fileName, Encoding.UTF8); + try + { + // Trace.TraceInformation(System.Reflection.MethodBase.GetCurrentMethod().Name); + string htmlContent = File.ReadAllText(fileName, Encoding.UTF8); - var pageNode = GetPageNode(); + var pageNode = GetPageNode(); - if (pageNode != null) - { - var existingPageId = pageNode.Attribute("ID").Value; - string[] position=null; - if (outline == null) + if (pageNode != null) { - position = GetMousePointPosition(existingPageId); - } + var existingPageId = pageNode.Attribute("ID").Value; + string[] position = null; + if (outline == null) + { + position = GetMousePointPosition(existingPageId); + } - var page = InsertHighLightCode(htmlContent, position, parameters, outline, selectedTextFormated); - page.Root.SetAttributeValue("ID", existingPageId); + var page = InsertHighLightCode(htmlContent, position, parameters, outline, selectedTextFormated); + page.Root.SetAttributeValue("ID", existingPageId); - OneNoteApplication.UpdatePageContent(page.ToString(), DateTime.MinValue); + OneNoteApplication.UpdatePageContent(page.ToString(), DateTime.MinValue); + } + } + catch (Exception e) + { + MessageBox.Show("Exception from InsertHighLightCodeToCurrentSide: "+e.ToString()); } } @@ -402,6 +423,7 @@ public XDocument InsertHighLightCode(string htmlContent, string[] position, High XElement cell2 = new XElement(ns + "Cell"); cell2.Add(new XAttribute("shadingColor", colorString)); + string defaultStyle = ""; var arrayLine = htmlContent.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); @@ -412,6 +434,8 @@ public XDocument InsertHighLightCode(string htmlContent, string[] position, High if(item.StartsWith("
", GenerateHighlightContent.GenerateHighLight.Config.OutputArguments["Font"].Value) +
// itemLine.Replace("'", "'") + "