Delete Layout using c#

Delete Layout using c#

lvthunder
Advocate Advocate
554 Views
3 Replies
Message 1 of 4

Delete Layout using c#

lvthunder
Advocate
Advocate

I have around a thousand files I need to change.  I update the titleblock and that is working fine.  I am trying to delete the Layout called Layout2, but it doesn't work.  This is the part of the code I'm using.  I don't have the drawing open in the editor because it saves so much time.  Does anyone know why it's not doing anything?

 

  public void PTDrawing(Database currentDb, MainForm control, string sheetName, string sheetType, Microsoft.Office.Interop.Excel.Worksheet tbExcel)
  {
      using (Transaction acTrans = currentDb.TransactionManager.StartTransaction())
      {
 LayoutManager.Current.DeleteLayout("Layout2");
    acTrans.Commit();
}

 

0 Likes
Accepted solutions (1)
555 Views
3 Replies
Replies (3)
Message 2 of 4

ActivistInvestor
Mentor
Mentor
Accepted solution

See if this works:

 

 

public void PTDrawing(Database currentDb, MainForm control, string sheetName, string sheetType, Microsoft.Office.Interop.Excel.Worksheet tbExcel)
{
   Database prevWorkingDb = HostApplicationServices.WorkingDatabase;
   HostApplicationServices.WorkingDatabase = currentDb;
   try
   {
      LayoutManager.Current.DeleteLayout("Layout2");
   }
   finally
   {
      HostApplicationServices.WorkingDatabase = prevWorkingDb;
   }

}

 

 

0 Likes
Message 3 of 4

ActivistInvestor
Mentor
Mentor

You should also get rid of the Transaction because I don't think it's needed when calling DeleteLayout(), and could be the cause of the problem.

0 Likes
Message 4 of 4

lvthunder
Advocate
Advocate

Thanks that worked.

0 Likes