public void createBuildingBlock(Word.Range range)
        {
            // Identify logic
            // If repeated content is being reused, it must be reused
            // from the shallowest on down.
            FabDocxState fabDocxState = (FabDocxState)Globals.ThisAddIn.Application.ActiveDocument.GetVstoObject(Globals.Factory).Tag;
            Helpers.LibraryHelper libHelp = new Helpers.LibraryHelper(fabDocxState.model);
            try
            {
                libHelp.identifyLogic(range);
            }
            catch (Helpers.BuildingBlockLogicException bble)
            {
                MessageBox.Show("Since your selection includes repeating content, to make a building block you need to include the outermost repeat.");
                return;
            }

            // For each bit of logic in the selection,
            // copy it to FabDocx.dotx
            // (if the logic is there already, from this docx,
            //  overwrite it, since its been updated)
            Word.Document fabdocx = null;
            try
            {
                fabdocx = FabDotxAsTemplate.OpenAsDocument();
                // TODO hide it
                Model targetModel = Model.ModelFactory(fabdocx);
                libHelp.injectLogic(targetModel, true, false, true);
            }
            catch (Helpers.BuildingBlockLogicException bble)
            {
                // ID collisions
                // (these are OK only if same source document)
                // TODO - alter IDs...
                MessageBox.Show(bble.Message);
                return;
            }
            finally
            {
                saveTemplate(fabdocx);
                closeDocument(fabdocx);
            }

            libHelp.TagsSourceAdd(range);

            // Save selection as building block in FabDocx.dotx
            FormBuildingBlockNew bbn = new FormBuildingBlockNew();
            bbn.ShowDialog();

            object description = bbn.textBoxDescription.Text;
            //object insertOptions = Word.WdDocPartInsertOptions.wdInsertParagraph;
            FabDotxAsTemplate.BuildingBlockEntries.Add(bbn.textBoxName.Text,
                Word.WdBuildingBlockTypes.wdTypeCustom1,
                bbn.textBoxCategory.Text, range, ref description); //, Word.WdDocPartInsertOptions.wdInsertParagraph);
                // NB, how to adjust any bindings so they point to FabDocx's custom xml parts?
                // as we can't easily change anything in the glossary document.
                // I guess it doesn't really matter; we'll change them when we load.
                // If it did, could always do it via OpenXML.

            // OK, now the building block is done, remove od:source from the cc's
            // in our document
            libHelp.TagsSourceRemove(range);

            bbn.Dispose();

            // Word prompts to save changes to the dotx when you exit Word,
            // not when you close the docx.
            FabDotxAsTemplate.Save();

            // Hack to force our gallery to refresh
            addinObj.Installed = false;
            addinObj.Installed = true;

            findTemplate(); //avoid object has been deleted
        }
        public void copyBuildingBlockLogic(Word.Document target, Word.Range range)
        {
            // FabDotxAsTemplate.OpenAsDocument() throws:
            //     This object model command is not available while in the current event.
            // Documents.Open(the dotx) is not allowed either
            // so make a temporary copy.

            //string tmp = System.IO.Path.GetTempFileName();
            //System.IO.File.Copy(templatePath, tmp, true);
            // .. but that fails the second time
            Word.Document fabdocx;
            try
            {
                //fabdocx = openDocument(tmp);
                fabdocx = FabDotxAsTemplate.OpenAsDocument();
            }
            catch (Exception e)
            {
                log.Error(e);
                throw e;
            }

            Model srcModel = Model.ModelFactory(fabdocx);

            Helpers.LibraryHelper libHelp = new Helpers.LibraryHelper(srcModel);
            try
            {
                libHelp.identifyLogic(range);
            }
            catch (Helpers.BuildingBlockLogicException bble)
            {
                log.Error(bble);
                MessageBox.Show("You can't reuse repeating content in a repeat"); // TODO, relax this restriction
                range.Delete();
                return;
            }

            Model targetModel = Model.ModelFactory(target);

            try
            {
                libHelp.injectLogic(targetModel, false, true, false);
                libHelp.updateBindings(range, targetModel.answersPart);
                // OK, remove od:source from the cc's
                // in our document
                libHelp.TagsSourceRemove(range);
            }
            catch (Helpers.BuildingBlockLogicException bble)
            {
                log.Error(bble);
                MessageBox.Show("ID collision"); // TODO, relax this restriction
                range.Delete();
                return;
            }
            finally
            {
                // OK to close the template now
                closeDocument(fabdocx);
                //System.IO.File.Delete(tmp);
                //log.Debug("deleted " + tmp);
            }
        }