private void ExportContent_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            RadMenuItem mi = e.OriginalSource as RadMenuItem;

            string menuheader = mi.Header.ToString();
            string menutag = mi.Tag.ToString();

            // close the menu
            RadContextMenu m = (RadContextMenu)(sender);
            m.IsOpen = false;

            if (menutag == "Word")
            {
                
                // take the doc, strip out the content controls and save as a regular word doc
                Globals.ThisAddIn.ProcessingStart("Save as Static Word Doc");

                //Check we have a name - TODO check name doesn't exist already
                if (tbVersionName.Text == "")
                {
                    MessageBox.Show("Please enter a name for the document");
                    tbVersionName.Focus();
                    return;
                }

                // always save first
                try
                {
                    //Always fails cause the handler returns an error to stop the normal save
                    Globals.ThisAddIn.Application.ActiveDocument.Save();
                }
                catch (Exception)
                {
                }


                // take out the Id
                Globals.ThisAddIn.DeleteDocId(_doc);

                // create a copy
                string filenamenoext = System.IO.Path.GetFileNameWithoutExtension(_filename);

                // save this to a scratch file
                string filename = AxiomIRISRibbon.Utility.SaveTempFile(filenamenoext);
                _doc.SaveAs2(filename, Word.WdSaveFormat.wdFormatXMLDocument);

                // open it!
                Word.Document newdoc = Globals.ThisAddIn.Application.Documents.Add(filename, Visible: true);

                // if attached then remove content controls
                if (_attachedmode)
                {
                    // remove content controls
                    Globals.ThisAddIn.ProcessingUpdate("Remove Content Controls");

                    object start = newdoc.Content.Start;
                    object end = newdoc.Content.End;
                    Word.Range r = newdoc.Range(ref start, ref end);

                    foreach (Word.ContentControl cc in r.ContentControls)
                    {
                        string tag = cc.Tag;
                        if (tag != null && tag != "" && cc.Tag.Contains('|'))
                        {
                            string[] taga = cc.Tag.Split('|');

                            if (taga.Length > 1 && ((taga[0] == "Concept" && taga[1] != "") || (taga[0] == "Element" && taga[1] != "")))
                            {
                                Word.Range ccr = cc.Range;
                                cc.LockContentControl = false;
                                cc.LockContents = false;
                                cc.Delete(false);
                            }
                        }
                    }

                   
                    // Switch of the element highliting
                    // Need to select somewhere editable!
                    newdoc.Characters.Last.Select();

                    try
                    {
                        Word.Style s = newdoc.Styles["ContentControl"];
                        if (s.Shading.BackgroundPatternColor != Word.WdColor.wdColorAutomatic)
                        {
                            s.Shading.BackgroundPatternColor = Word.WdColor.wdColorAutomatic;
                        }
                    }
                    catch (Exception)
                    {
                    }
                }

                // ** Add back in the doc to the original doc ** so we don't break the original!
                if (_attachedmode)
                {
                    Globals.ThisAddIn.AddDocId(_doc, "Contract", _versionid);
                }
                else
                {
                    Globals.ThisAddIn.AddDocId(_doc, "UAContract", _versionid);
                }

                newdoc.Activate();

                Globals.ThisAddIn.ProcessingStop("End");
            }


            else if (menutag == "PDF")
            {

                // take the doc, strip out the content controls and save as a regular word doc
                Globals.ThisAddIn.ProcessingStart("Save as Static Word Doc");

                //Check we have a name - TODO check name doesn't exist already
                if (tbVersionName.Text == "")
                {
                    MessageBox.Show("Please enter a name for the document");
                    tbVersionName.Focus();
                    return;
                }

                // always save first
                try
                {
                    //Always fails cause the handler returns an error to stop the normal save
                    Globals.ThisAddIn.Application.ActiveDocument.Save();
                }
                catch (Exception)
                {
                }


                // take out the Id
                Globals.ThisAddIn.DeleteDocId(_doc);

                // create a copy
                string filenamenoext = System.IO.Path.GetFileNameWithoutExtension(_filename);

                // save this to a scratch file
                string filename = AxiomIRISRibbon.Utility.SaveTempFile(filenamenoext);
                _doc.SaveAs2(filename, Word.WdSaveFormat.wdFormatXMLDocument);

                // open it!
                Word.Document newdoc = Globals.ThisAddIn.Application.Documents.Add(filename, Visible: true);

                // if attached then remove content controls
                if (_attachedmode)
                {
                    // remove content controls
                    Globals.ThisAddIn.ProcessingUpdate("Remove Content Controls");

                    object start = newdoc.Content.Start;
                    object end = newdoc.Content.End;
                    Word.Range r = newdoc.Range(ref start, ref end);

                    foreach (Word.ContentControl cc in r.ContentControls)
                    {
                        string tag = cc.Tag;
                        if (tag != null && tag != "" && cc.Tag.Contains('|'))
                        {
                            string[] taga = cc.Tag.Split('|');

                            if (taga.Length > 1 && ((taga[0] == "Concept" && taga[1] != "") || (taga[0] == "Element" && taga[1] != "")))
                            {
                                Word.Range ccr = cc.Range;
                                cc.LockContentControl = false;
                                cc.LockContents = false;
                                cc.Delete(false);
                            }
                        }
                    }


                    // Switch of the element highliting
                    // Need to select somewhere editable!
                    newdoc.Characters.Last.Select();

                    try
                    {
                        Word.Style s = newdoc.Styles["ContentControl"];
                        if (s.Shading.BackgroundPatternColor != Word.WdColor.wdColorAutomatic)
                        {
                            s.Shading.BackgroundPatternColor = Word.WdColor.wdColorAutomatic;
                        }
                    }
                    catch (Exception)
                    {
                    }
                }

                newdoc.TrackRevisions = false;
                newdoc.ShowRevisions = false;

                //Save a copy! - give it the name of the version
                string versionname = tbVersionName.Text;

                Globals.ThisAddIn.ProcessingUpdate("Save PDF");
                string filenamecopy = Utility.SaveTempFile(versionname, "pdf");
                newdoc.SaveAs2(FileName: filenamecopy, FileFormat: Word.WdSaveFormat.wdFormatPDF);

                var docclose = (Microsoft.Office.Interop.Word._Document)newdoc;
                docclose.Close(SaveChanges: false);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(docclose);

                // ** Add back in the doc to the original doc ** so we don't break the original!
                if (_attachedmode)
                {
                    Globals.ThisAddIn.AddDocId(_doc, "Contract", _versionid);
                }
                else
                {
                    Globals.ThisAddIn.AddDocId(_doc, "UAContract", _versionid);
                }

                // open the pdf file in a viewer
                ExternalEditProcess p = new ExternalEditProcess();
                p._id = _versionid;
                p._path = filenamecopy;
                p.StartInfo = new ProcessStartInfo(filenamecopy);
                p.Start();

                Globals.ThisAddIn.ProcessingStop("End");
                
            }
        }
        private void btnSavePDF_Click(object sender, RoutedEventArgs e)
        {
            //save this to a scratch file
            Globals.ThisAddIn.ProcessingStart("Save as Pdf");
            Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;

            //Check we have a name - TODO check name doesn't exist already
            if (tbVersionName.Text == "")
            {
                MessageBox.Show("Please enter a name for the document");
                tbVersionName.Focus();
                return;
            }

            // always save
            try
            {
                //Always fails cause the handler returns an error to stop the normal save
                Globals.ThisAddIn.Application.ActiveDocument.Save();
            }
            catch (Exception)
            {
            }

            //Need to take out the docid!
            Globals.ThisAddIn.DeleteDocId(doc);
            Globals.ThisAddIn.AddDocId(doc, "ExportContract", _versionid);

            // Switch of the element highliting
            // Need to select somewhere editable!
            Globals.ThisAddIn.Application.ActiveDocument.Characters.Last.Select();

            try
            {
                Word.Style s = Globals.ThisAddIn.Application.ActiveDocument.Styles["ContentControl"];
                if (s.Shading.BackgroundPatternColor != Word.WdColor.wdColorAutomatic)
                {
                    s.Shading.BackgroundPatternColor = Word.WdColor.wdColorAutomatic;
                }
            }
            catch (Exception)
            {
            }

            // switch on Revisions
            doc.TrackRevisions = true;
            doc.ShowRevisions = true;

            Globals.ThisAddIn.ProcessingUpdate("Save Scratch");
            string filename = Utility.SaveTempFile(_versionid);
            doc.SaveAs2(FileName: filename, FileFormat: Word.WdSaveFormat.wdFormatXMLDocument, CompatibilityMode: Word.WdCompatibilityMode.wdCurrent);

            //Save a copy! - give it the name of the version
            string versionname = tbVersionName.Text;

            Globals.ThisAddIn.ProcessingUpdate("Save PDF");
            string filenamecopy = Utility.SaveTempFile(versionname, "pdf");
            doc.SaveAs2(FileName: filenamecopy, FileFormat: Word.WdSaveFormat.wdFormatPDF);

            var docclose = (Microsoft.Office.Interop.Word._Document)doc;
            docclose.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(docclose);

            //Now save the file
            Globals.ThisAddIn.ProcessingUpdate("Save To SalesForce");
            DataReturn dr;
            dr = Utility.HandleData(_d.AttachFile(_versionid, versionname + ".pdf", filenamecopy));
            if (!dr.success) return;

            // open the pdf file in a viewer
            ExternalEditProcess p = new ExternalEditProcess();
            p._id = dr.id;
            p._path = filenamecopy;
            // p._lastwrite = System.IO.File.GetLastWriteTimeUtc(dr.strRtn).ToString();
            // p.EnableRaisingEvents = true;
            // p.Exited += new EventHandler(ExternalEditProcess_HasExited);
            p.StartInfo = new ProcessStartInfo(filenamecopy);
            p.Start();

            Globals.ThisAddIn.ProcessingStop("End");
        }