<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Change active document in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7795363#M52352</link>
    <description>&lt;P&gt;Hmm, close but no sigar &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I start with document A. Later I open Document B which becomes the active document. I use the showelements to open a view from Document B, so it becomes the active document. Then I use DOcB.Close(false) and revit keeps telling me it can't do that because of some subtransaction being active??? No idea what transaction Revit means &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 21 Feb 2018 15:14:06 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2018-02-21T15:14:06Z</dc:date>
    <item>
      <title>Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7787792#M52347</link>
      <description>&lt;P&gt;When you have 2 revit projects open and want to switch between them it can be done with:&lt;/P&gt;
&lt;P&gt;application.OpenAndActivateDocument(file).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In order to get the filename I used the get it with Document.PathName.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I come accross problems with files on Revit Server and BIm 360 docs. Their Document.Pathname stays empty.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, my questions is, how do I switch between those documents (when Document.Pathname is empty) ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a way to switch between documents without having to specify the file name ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2018 07:23:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7787792#M52347</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-02-19T07:23:50Z</dc:date>
    </item>
    <item>
      <title>Re: Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7789974#M52348</link>
      <description>&lt;P&gt;The only way I have found is indirectly via UIDocument.ShowElements. You pick an element from the DB document you want to change to, create a UIDocument object from a DB document and use UIDocument.ShowElements. You have to handle the occasional “No good view found” dialogue but it seems to always switch the active document regardless of what is found. Helps if it is a View specific element.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Not sure if there is a better way, also wondering when future implementation of .ActiveUIDocument is coming (as noted in RevitAPI.chm file). Odd that there seems to be no way to do this directly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://forums.autodesk.com/t5/revit-api-forum/how-to-open-and-active-a-new-document-that-is-not-saved/m-p/7710749/highlight/true#M28051" target="_blank"&gt;https://forums.autodesk.com/t5/revit-api-forum/how-to-open-and-active-a-new-document-that-is-not-saved/m-p/7710749/highlight/true#M28051&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#808080"&gt;&lt;EM&gt;&amp;nbsp;"External API commands can access this property in read-only mode only! The ability to modify the property is reserved for future implementations."&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2018 21:18:47 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7789974#M52348</guid>
      <dc:creator>RPTHOMAS108</dc:creator>
      <dc:date>2018-02-19T21:18:47Z</dc:date>
    </item>
    <item>
      <title>Re: Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7790759#M52349</link>
      <description>&lt;P&gt;Haha, never thought of this approach &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; thanks. "&lt;SPAN&gt;Odd that there seems to be no way to do this directly" Yeah, and even stranger they make an "OpenAndActivate", while the file is already openend. A simple "SetActiveDocument" would be enough.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2018 07:31:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7790759#M52349</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-02-20T07:31:13Z</dc:date>
    </item>
    <item>
      <title>Re: Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7790904#M52350</link>
      <description>&lt;P&gt;Many thanks to&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/1035859"&gt;@RPTHOMAS108&lt;/a&gt; for the solution!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also documented here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://thebuildingcoder.typepad.com/blog/2010/11/mirroring-in-a-new-family-and-changing-active-view.html" target="_blank"&gt;http://thebuildingcoder.typepad.com/blog/2010/11/mirroring-in-a-new-family-and-changing-active-view.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"The first issue that arises is that the mirror command requires a current active view, which is not automatically present in the family document. Joe discovers a workaround for that issue using the ShowElements method. It generates an unwanted warning message, so a second step is required to deal with eliminating that as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As you can see on reading the final solution carefully, you can use the ShowElements method to change the active view and even switch it between the family and project documents. The official Revit 2011 API does not provide any method to switch the active view, but using ShowElements can be used to create a workaround for that."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jeremy&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2018 08:26:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7790904#M52350</guid>
      <dc:creator>jeremytammik</dc:creator>
      <dc:date>2018-02-20T08:26:20Z</dc:date>
    </item>
    <item>
      <title>Re: Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7790951#M52351</link>
      <description>&lt;P&gt;Thanks Jeremy,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's the same conclusion as&amp;nbsp;&lt;SPAN class=""&gt;&lt;A id="link_969071189555e3" class="lia-link-navigation lia-page-link lia-user-name-link" href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/1035859" target="_self"&gt;RPTHOMAS108&lt;/A&gt;&amp;nbsp;posted &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Well, I gave it a shot on the Revit Ideas site:&amp;nbsp;&lt;A title="Change active document" href="https://forums.autodesk.com/t5/revit-ideas/change-active-document/idi-p/7790936" target="_blank"&gt;Revit Idea&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, everyone who reads this post, please vote, so AutoDesk will notice and maybe put this functionality in the next Revit &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2018 08:45:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7790951#M52351</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-02-20T08:45:58Z</dc:date>
    </item>
    <item>
      <title>Re: Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7795363#M52352</link>
      <description>&lt;P&gt;Hmm, close but no sigar &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I start with document A. Later I open Document B which becomes the active document. I use the showelements to open a view from Document B, so it becomes the active document. Then I use DOcB.Close(false) and revit keeps telling me it can't do that because of some subtransaction being active??? No idea what transaction Revit means &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Feb 2018 15:14:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7795363#M52352</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-02-21T15:14:06Z</dc:date>
    </item>
    <item>
      <title>Re: Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7798417#M52353</link>
      <description>&lt;P&gt;Dear Remy,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I never heard of that problem before.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here are two exploration of closing documents... don't know whether they will be of any use in your case:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://thebuildingcoder.typepad.com/blog/2010/10/closing-the-active-document-and-why-not-to.html" target="_blank"&gt;http://thebuildingcoder.typepad.com/blog/2010/10/closing-the-active-document-and-why-not-to.html&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://thebuildingcoder.typepad.com/blog/2012/12/closing-the-active-document.html" target="_blank"&gt;http://thebuildingcoder.typepad.com/blog/2012/12/closing-the-active-document.html&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jeremy&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2018 09:03:56 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7798417#M52353</guid>
      <dc:creator>jeremytammik</dc:creator>
      <dc:date>2018-02-22T09:03:56Z</dc:date>
    </item>
    <item>
      <title>Re: Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7799042#M52354</link>
      <description>&lt;P&gt;The exact message reads: Revit encountered a Autdesk.Revit.Exceptions.InvalidOperationException: Close is not allowed when there is any open sub-transaction, transaction or transaction group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I do is:&lt;/P&gt;
&lt;P&gt;In a project, open family, do some actions within a transaction which I commit and afterwards dispose. Then save the family. Use the showelements option to get a view of the project active and then try to close the family. That is when this errors comes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2018 10:48:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7799042#M52354</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-02-22T10:48:26Z</dc:date>
    </item>
    <item>
      <title>Re: Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7799378#M52355</link>
      <description>&lt;P&gt;Dear Remy,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sounds weird.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want the development team (or anyone else, for that matter) to be able to take a look, please provide a complete minimal reproducible case including sample model, macro and exact steps to reproduce:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://thebuildingcoder.typepad.com/blog/about-the-author.html#1b" target="_blank"&gt;http://thebuildingcoder.typepad.com/blog/about-the-author.html#1b&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jeremy&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2018 13:08:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7799378#M52355</guid>
      <dc:creator>jeremytammik</dc:creator>
      <dc:date>2018-02-22T13:08:40Z</dc:date>
    </item>
    <item>
      <title>Re: Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7799699#M52356</link>
      <description>&lt;P&gt;It is pretty easy to reproduce:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;[Transaction(TransactionMode.Manual)]&lt;BR /&gt; [Regeneration(RegenerationOption.Manual)]&lt;BR /&gt; [Journaling(JournalingMode.NoCommandData)]&lt;BR /&gt; public class DrieBToolTest : IExternalCommand&lt;BR /&gt; {&lt;/P&gt;
&lt;P&gt;public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)&lt;BR /&gt; {&lt;BR /&gt; UIApplication uiapp = commandData.Application;&lt;BR /&gt; UIDocument uidoc = uiapp.ActiveUIDocument;&lt;BR /&gt; Document doc = uidoc.Document;&lt;/P&gt;
&lt;P&gt;// select some elements in the first document&lt;/P&gt;
&lt;P&gt;FilteredElementCollector notElemTypeCtor = (new FilteredElementCollector(doc, uidoc.ActiveGraphicalView.Id)).WhereElementIsNotElementType();&lt;BR /&gt; List&amp;lt;ElementId&amp;gt; theelems = notElemTypeCtor.ToList().Select(theelem =&amp;gt; theelem.Id).ToList();&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;// open the second file and do some actions&lt;/P&gt;
&lt;P&gt;string fam = @"d:\test\fam\31_3B_vo bu deur met weerszijde 2 zijlichten.rfa";&lt;BR /&gt; UIDocument famDoc = commandData.Application.OpenAndActivateDocument(fam);&lt;/P&gt;
&lt;P&gt;Transaction transac = new Transaction(famDoc.Document);&lt;BR /&gt; transac.Start("change scale");&lt;BR /&gt; famDoc.ActiveView.get_Parameter(BuiltInParameter.VIEW_SCALE_PULLDOWN_METRIC).Set(20);&lt;BR /&gt; transac.Commit();&lt;BR /&gt; SaveAsOptions opt = new SaveAsOptions { OverwriteExistingFile = true };&lt;BR /&gt; famDoc.Document.SaveAs(fam, opt);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;// On a new file the doc,PathName is empty&lt;/P&gt;
&lt;P&gt;if (!string.IsNullOrEmpty(doc.PathName))&lt;BR /&gt; {&lt;BR /&gt; commandData.Application.OpenAndActivateDocument(doc.PathName);&lt;BR /&gt; famDoc.Document.Close(false); // no problem here&lt;BR /&gt; }&lt;BR /&gt; else&lt;BR /&gt; {&lt;/P&gt;
&lt;P&gt;// this is the way to go when to avoid using OpenAndActivateDocument&lt;BR /&gt; uidoc.ShowElements(theelems);&lt;BR /&gt; uidoc.RefreshActiveView();&lt;BR /&gt; famDoc.Document.Close(false); //here revit throws the exception and doesn't close the file&lt;BR /&gt; }&lt;/P&gt;
&lt;P&gt;return Result.Succeeded;&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2018 14:24:37 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7799699#M52356</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-02-22T14:24:37Z</dc:date>
    </item>
    <item>
      <title>Re: Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7799895#M52357</link>
      <description>&lt;P&gt;Please also describe the exact steps to reproduce the problem. Thx!&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2018 15:09:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7799895#M52357</guid>
      <dc:creator>jeremytammik</dc:creator>
      <dc:date>2018-02-22T15:09:22Z</dc:date>
    </item>
    <item>
      <title>Re: Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7799910#M52358</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Start revit&lt;/P&gt;
&lt;P&gt;Start with a new project&lt;/P&gt;
&lt;P&gt;Run the code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2018 15:13:00 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7799910#M52358</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-02-22T15:13:00Z</dc:date>
    </item>
    <item>
      <title>Re: Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7804744#M52359</link>
      <description>&lt;P&gt;Sorry to hear this doesn't work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To be honest I don't think I've actually needed to save after using this trick since I'm usually either opening and activating (in the case of a add-in that processes multiple files) or just working in the current file within an IExternalCommand context.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do get this same exception for something unrelated, where I open detached. Hard to diagnose these things when you have other add-ins running which may be opening transactions during events etc.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2018 22:16:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7804744#M52359</guid>
      <dc:creator>RPTHOMAS108</dc:creator>
      <dc:date>2018-02-23T22:16:31Z</dc:date>
    </item>
    <item>
      <title>Re: Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7834884#M52360</link>
      <description>&lt;P&gt;Dear Remy,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have not yet submitted this to the development team.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I did take a closer look at your code now, at least, and have some minor improvement suggestions as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;    /// &amp;lt;summary&amp;gt;
    /// Toggle back and forth between two different documents
    /// &amp;lt;/summary&amp;gt;
    void ToggleViews( 
      View view1, 
      string filepath2 )
    {
      Document doc = view1.Document;
      UIDocument uidoc = new UIDocument( doc );
      Application app = doc.Application;
      UIApplication uiapp = new UIApplication( app );

      // Select some elements in the first document

      ICollection&amp;lt;ElementId&amp;gt; idsView1
        = new FilteredElementCollector( doc, view1.Id )
          .WhereElementIsNotElementType()
          .ToElementIds();

      // Open the second file

      UIDocument uidoc2 = uiapp
        .OpenAndActivateDocument( filepath2 );

      Document doc2 = uidoc2.Document;

      // Do something in second file

      using( Transaction tx = new Transaction( doc2 ) )
      {
        tx.Start( "Change Scale" );
        doc2.ActiveView.get_Parameter(
          BuiltInParameter.VIEW_SCALE_PULLDOWN_METRIC )
            .Set( 20 );
        tx.Commit();
      }

      // Save modified second file

      SaveAsOptions opt = new SaveAsOptions
      {
        OverwriteExistingFile = true
      };

      doc2.SaveAs( filepath2, opt );

      // Switch back to original file;
      // in a new file, doc.PathName is empty

      if( !string.IsNullOrEmpty( doc.PathName ) )
      {
        uiapp.OpenAndActivateDocument(
          doc.PathName );

        doc2.Close( false ); // no problem here, says Remy
      }
      else
      {
        // Avoid using OpenAndActivateDocument

        uidoc.ShowElements( idsView1 );
        uidoc.RefreshActiveView();

        //doc2.Close( false ); // Remy says: Revit throws the exception and doesn't close the file
      }
    }&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The things I care about here are:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Avoid using unnecessary `ToList` calls all over the place.&lt;/LI&gt;
&lt;LI&gt;Encapsulate all transactions in a `using` statement.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It probably won't change the problem of closing the family, though.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jeremy&lt;/P&gt;</description>
      <pubDate>Wed, 07 Mar 2018 15:22:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7834884#M52360</guid>
      <dc:creator>jeremytammik</dc:creator>
      <dc:date>2018-03-07T15:22:38Z</dc:date>
    </item>
    <item>
      <title>Re: Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7837446#M52361</link>
      <description>&lt;P&gt;Edited and saved for posterity here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://thebuildingcoder.typepad.com/blog/2018/03/switch-view-or-document-by-showing-elements.html" target="_blank"&gt;http://thebuildingcoder.typepad.com/blog/2018/03/switch-view-or-document-by-showing-elements.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jeremy&lt;/P&gt;</description>
      <pubDate>Thu, 08 Mar 2018 10:16:54 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/7837446#M52361</guid>
      <dc:creator>jeremytammik</dc:creator>
      <dc:date>2018-03-08T10:16:54Z</dc:date>
    </item>
    <item>
      <title>Re: Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/8740470#M52362</link>
      <description>&lt;P&gt;I had such problems with transaction when I after changeng ActiveDocument close document.&lt;BR /&gt;I solve this through external event.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;//After all logic i call external event&lt;BR /&gt;//Glob is static clase where i store some constant and use for easy 
Glob.GeneralEventWithParams.RunFunc(CloseDoc,
Glob.GeneralExternalEventWithParams,
famDoc);
ui_doc.ShowElements(new List&amp;lt;ElementId&amp;gt; { ins.Id });
ui_doc.RefreshActiveView();&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt; //Method that i use for closing and call inside event&lt;BR /&gt;public static bool CloseDoc(UIDocument ui_doc, params object [ ] obk) {
            var doc = obk [ 0 ] as Document;
            doc.Close(false);
            return true;
        &lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;  //External event that i use&lt;BR /&gt; public class GeneralExternalEventWithParams :IExternalEventHandler
    {

        private Func&amp;lt;UIDocument, object [ ], bool&amp;gt; Function { get; set; }

        public void Execute(UIApplication app) {
            try {
                var ui_doc = app.ActiveUIDocument;
                var doc = ui_doc.Document;
                if ( Function != null ) {
                    Function?.Invoke(ui_doc, Params);
                }
            } catch ( Exception e ) {
                e.WriteEx();
            }


        }
        public void RunFunc(Func&amp;lt;UIDocument, object [ ], bool&amp;gt; function, ExternalEvent runningEvent, params object [ ] parameters) {
            try {
                Function = function;
                Params = parameters;
                runningEvent.Raise();
            } catch ( Exception e ) {
                 
            }

        }

        public object [ ] Params { get; set; }

        public string GetName() {
            return "GeneralExternalEventWithParams";
        }
    }&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Apr 2019 16:33:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/8740470#M52362</guid>
      <dc:creator>Kisli89</dc:creator>
      <dc:date>2019-04-18T16:33:40Z</dc:date>
    </item>
    <item>
      <title>Re: Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/9799435#M52363</link>
      <description>&lt;P&gt;Hi, coming from the future &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I personally use &lt;A title="UIDocument.RefreshActiveView()" href="https://www.revitapidocs.com/2020/5d9f248c-a1be-ac2e-1f4c-2219ed0fc5ae.htm" target="_blank" rel="noopener"&gt;UIDocument.RefreshActiveView()&lt;/A&gt; and it seem work for me.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Oct 2020 08:15:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/9799435#M52363</guid>
      <dc:creator>Thomas_LECUPPRE_Let_it_BIM</dc:creator>
      <dc:date>2020-10-13T08:15:04Z</dc:date>
    </item>
    <item>
      <title>Re: Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/13020141#M52364</link>
      <description>&lt;P&gt;From the even more distant future: I've found, that after changing the active document with uiDoc.ShowElements() I'm not able to close the previous document with oldUiDoc.SaveAndClose(), it does nothing, no exceptions thrown, but instead of returning the expected boolean it jumps right into my IdlingEventHandler.&lt;/P&gt;&lt;P&gt;Seems similar to &lt;A href="https://forums.autodesk.com/t5/revit-api-forum/closing-active-family-document/m-p/11471099/highlight/false#M66434" target="_blank"&gt;https://forums.autodesk.com/t5/revit-api-forum/closing-active-family-document/m-p/11471099/highlight/false#M66434&lt;/A&gt; but the alternate method is not always available...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2024 21:35:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/13020141#M52364</guid>
      <dc:creator>tamas.deri</dc:creator>
      <dc:date>2024-09-13T21:35:23Z</dc:date>
    </item>
    <item>
      <title>Re: Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/13021254#M52365</link>
      <description>&lt;P&gt;I did some further investigation, and found the connection between the issue mentioned in the linked topic.&lt;/P&gt;&lt;P&gt;While my code did not throw any exceptions in Visual Studio (silly me had no try-catch around the specific code), I've found an exception in the Journal:&lt;/P&gt;&lt;LI-CODE lang="general"&gt;' 0:&amp;lt; External event handler &amp;lt;RevitExternalEventHandler&amp;gt; failed with an exception {Close is not allowed when there is any open sub-transaction, transaction or transaction group.}. The event was registered by an AddIn ...(a guid) &lt;/LI-CODE&gt;&lt;P&gt;So it seems that it is not possible to close a document (neither using doc.Close() nor uiDoc.SaveAndClose()) after activating another document via uidoc.ShowElements() if we are in a valid API context (ExternalCommand or ExternalEvent). However there are no issues at all when whe are "outside of valid API context", and I execute my code on a cached UIApplication. I've tried to do everything by the book, and only interact with the Revit API in valid API context, but this case looks like an exception from the general rule, where it should be avoided.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/413917"&gt;@jeremytammik&lt;/a&gt;could you please put up this issue to the development team for further investigation?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 14 Sep 2024 21:10:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/13021254#M52365</guid>
      <dc:creator>tamas.deri</dc:creator>
      <dc:date>2024-09-14T21:10:39Z</dc:date>
    </item>
    <item>
      <title>Re: Change active document</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/13022745#M52366</link>
      <description>&lt;P&gt;Well, the exception to the general rule probably means that you found a loophole that the development team will be happy to close, and then what you are after will not work in that form any more at all. Maybe you can work around the issue by combining the external event with a one-off Idling event? Afaik, there are no restrictions at all on what you can do in an Idling event handler, whereas, according to the journal entry you share, certain restrictions do apply to external events.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2024 06:12:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/change-active-document/m-p/13022745#M52366</guid>
      <dc:creator>jeremy_tammik</dc:creator>
      <dc:date>2024-09-16T06:12:53Z</dc:date>
    </item>
  </channel>
</rss>

