public void placeActiveView() { UIDocument uidoc = this.ActiveUIDocument; Document doc = uidoc.Document; View activeView = doc.ActiveView; using(var form = new Form1()) { //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; //create a new variable to store the user input text string sheetNumber = form.textString.ToString(); ViewSheet viewSh = null; FilteredElementCollector sheets = new FilteredElementCollector(doc).OfClass(typeof(ViewSheet)); foreach (ViewSheet sht in sheets) { if (sht.SheetNumber.ToString() == sheetNumber) //find the sheet that matches the user input text viewSh = sht; } //TaskDialog.Show("out","Selected sheet Id: " + viewSh.Id.ToString()); using (Transaction t = new Transaction(doc)) { t.Start("Add view to sheet"); try { Viewport vp = Viewport.Create(doc, viewSh.Id, activeView.Id, new XYZ(0,0,0)); t.Commit(); } /* catch (System.NullReferenceException ex) { TaskDialog.Show("Exception Caught", "The Sheet Number does not exists"); t.RollBack(); placeActiveView(); } */ catch { if (sheetNumber == ""){ TaskDialog.Show("Warning","Please enter a sheet number"); //t.RollBack(); placeActiveView(); } else if (viewSh == null){ TaskDialog.Show("Warning","The sheet number does not exist"); t.RollBack(); placeActiveView(); } else { TaskDialog.Show("Warning","The view is already placed on another sheet"); t.RollBack(); placeActiveView(); } } //close using } //close Form1 } //close placeActiveView }