public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Application app = uiapp.Application; Document doc = uidoc.Document; View activeView = doc.ActiveView; using (var form = new FormAddActiveView("Enter Beam Identity Type Mark")) { using (Transaction t = new Transaction(doc)) { Reference selectedBeam = uidoc.Selection.PickObject(ObjectType.Element, "Select a beam"); FamilyInstance beam = doc.GetElement(selectedBeam) as FamilyInstance; string interrupt = "False"; while (interrupt == "False") { //use ShowDialog to show the form as a modal dialog box. form.ShowDialog(); //if the user hits cancel just drop out of macro if (form.DialogResult == forms.DialogResult.Cancel) { return(Result.Cancelled); } string identityTypeMark = form.TextString.ToUpper().ToString(); FilteredElementCollector beamTypesCollector = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_StructuralFraming).WhereElementIsElementType(); var wte = beamTypesCollector.FirstOrDefault((e) => { if (string.Equals(e.LookupParameter("Identity_Type Mark").AsString(), identityTypeMark)) { return(true); } else { return(false); } }); ElementType et = wte as ElementType; t.Start("Add view to sheet"); try { beam.ChangeTypeId(et.Id); interrupt = "True"; t.Commit(); } catch (Exception ex) { if (identityTypeMark == "") { TaskDialog.Show("Warning", "Please enter an identity type mark"); t.RollBack(); // form.ShowDialog(); } else if (identityTypeMark == null) { TaskDialog.Show("Warning", "This identity type mark not exist"); t.RollBack(); // form.ShowDialog(); } else { TaskDialog.Show("Warning", ex.Message); t.RollBack(); // form.ShowDialog(); } } //close catch } //close while } } return(Result.Succeeded); }