# Open and close cells programatically in a notebook

**URL:** <https://community.wolfram.com/t/open-and-close-cells-programatically-in-a-notebook/1364>\
**Category:** Questions\
**Tags:** authoring-and-publishing, mathematica, interface-construction\
**Created:** [January 26, 2014, 9:06pm UTC](https://community.wolfram.com/t/open-and-close-cells-programatically-in-a-notebook/1364 "2014-01-26T21:06:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wrcstewart](https://avatars.discourse-cdn.com/v4/letter/w/ac91a4/32.png) [@wrcstewart](https://community.wolfram.com/u/wrcstewart)\
**Post date:** [January 26, 2014, 9:06pm UTC](https://community.wolfram.com/t/open-and-close-cells-programatically-in-a-notebook/1364/1 "2014-01-26T21:06:28Z")

</div>

How can I programatically open and close cells in a notebook? There are so many commands I am not sure how to go about this.

Suppose I have 3 cells with text and a fourth cell that contains a button that will close (say) the second cell and another button to reopen it.  
I can figure out the rest given this clue.

---

<div class="post-metadata">

**Author:** ![shenghuiy](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/shenghuiy/32/36073_2.png) [@shenghuiy](https://community.wolfram.com/u/shenghuiy)\
**Post date:** [January 26, 2014, 10:14pm UTC](https://community.wolfram.com/t/open-and-close-cells-programatically-in-a-notebook/1364/2 "2014-01-26T22:14:40Z")

</div>

You may take a look at [this discussion on Stack Exchange](http://mathematica.stackexchange.com/questions/6809/close-programmatically-all-cell-groups-in-mathematica).

---

<div class="post-metadata">

**Author:** ![dreiss](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dreiss/32/29094_2.png) [@dreiss](https://community.wolfram.com/u/dreiss)\
**Post date:** [January 26, 2014, 11:48pm UTC](https://community.wolfram.com/t/open-and-close-cells-programatically-in-a-notebook/1364/3 "2014-01-26T23:48:40Z")

</div>

Take a look at how the following code works to get some insight into how to handle a problem like this using CellTags. I am interpreting you question about opening and closeing a cell as wanting to hide the cell rather than in terms of opening and closing CellGroups. But the latter can be handled along similar lines.

```auto
CreateWindow[
 DocumentNotebook[{
   TextCell["Cell one", "Text", CellTags -> {"cell1"}],
   TextCell["Cell two", "Text", CellTags -> {"cell2"}],
   TextCell["Cell three", "Text", CellTags -> {"cell3"}],
   TextCell[
    Row[{
      Button["Open",
       Module[{nb},
        nb = EvaluationNotebook[];
        NotebookFind[nb, "cell2", All, CellTags, 
         AutoScroll -> False];
        SetOptions[NotebookSelection[nb], CellOpen -> True]
        ]],
      Button["Close",
       Module[{nb},
        nb = EvaluationNotebook[];
        NotebookFind[nb, "cell2", All, CellTags, 
         AutoScroll -> False];
        SetOptions[NotebookSelection[nb], CellOpen -> False]
        ]]
      }
     ],
    "Text"]
   }
  ]
 ]

```

For a lot more detail on working with various sorts of tags take a look at the Mathematica conference talk I gave several years ago either at

[http://scientificarts.com/worklife/conference2008.html](http://scientificarts.com/worklife/conference2008.html)

or at

[http://library.wolfram.com/infocenter/Conferences/7246/](http://library.wolfram.com/infocenter/Conferences/7246/)

Hope this helps…

---

<div class="post-metadata">

**Author:** ![wrcstewart](https://avatars.discourse-cdn.com/v4/letter/w/ac91a4/32.png) [@wrcstewart](https://community.wolfram.com/u/wrcstewart)\
**Post date:** [January 27, 2014, 11:42am UTC](https://community.wolfram.com/t/open-and-close-cells-programatically-in-a-notebook/1364/4 "2014-01-27T11:42:39Z")

</div>

Thanks for implementing exactly what I asked for!
