# DocX – Add nested lists to Microsoft Word document programmatically

Dowiedz sie, jak programowo tworzyc zagniezdzone listy punktowane i numerowane w Microsoft Word przy uzyciu DocX i C#, wraz z prostym przykladem.

blog

26 May 2016 pl

![DocX – Add nested lists to Microsoft Word document programmatically](/wp-content/uploads/2016/03/DocX-MicrosoftWord.png)

[Zobacz obrazy (2)](#pf-content-image-1-0)

If you had one of those days looking for solution on how to easily add a nested list to Microsoft Word document by using C# search no more. Below you can find an example on how to do that using **DocX** package.

### 📌 Prerequisites

Following **prerequisites** are required to create **Microsoft Word** document programmatically:

- [DocX](https://docx.codeplex.com) package
- ** Visual Studio 2013** / **Visual Studio 2015** (Free Community Edition will do)
- Basic understanding on how to code in **C# (C Sharp)**
- Basic tutorial on how to start coding with **DocX** **package** can be found on our blog post [DocX – A short tutorial for beginners HelloWorld()](https://evotec.xyz/docx-a-short-tutorial-for-beginners-helloworld/)

### 💻 Example code

Following code provides basic example on how to add **Nested Lists **to Microsoft Word **programmatically**.

```csharp
private static void AddList()
{
    Console.WriteLine("\tAddList()");

    using (var document = DocX.Create(@"docs\Lists.docx"))
    {
        var numberedList = document.AddList("First List Item.", 0, ListItemType.Numbered);
                //Add a numbered list starting at 2
        document.AddListItem(numberedList, "Second List Item.");
        document.AddListItem(numberedList, "Third list item.");
        document.AddListItem(numberedList, "First sub list item", 1);

        document.AddListItem(numberedList, "Nested item.", 2);
        document.AddListItem(numberedList, "Fourth nested item.");

        var bulletedList = document.AddList("First Bulleted Item.", 0, ListItemType.Bulleted);
        document.AddListItem(bulletedList, "Second bullet item");
        document.AddListItem(bulletedList, "Sub bullet item", 1);
        document.AddListItem(bulletedList, "Second sub bullet item", 2);
        document.AddListItem(bulletedList, "Third bullet item");

        document.InsertList(numberedList);
        document.InsertList(bulletedList);
        document.Save();
        Console.WriteLine("\tCreated: docs\\Lists.docx");
    }
}
```

### 📄 Example Output

Code above should create an output file called **Lists.docx**. If you start it up the view should be showing nice **Nested Lists.**

[!\[Dokument Lists.docx w Microsoft Word pokazujący zagnieżdżone listy numerowane i punktowane utworzone programowo.\](/wp-content/uploads/2016/05/img_574761f0bb61f.png)](/wp-content/uploads/2016/05/img_574761f0bb61f.png)

### 🔹 Example Notes

Above example is part of **DocX Examples** and can be found in DocX [source package](https://github.com/WordDocX/DocX) under **Examples \ Program.cs. **This example uses **RelativeDirectory.cs** class. It's **required** for the code sample to work and are included in the sources of DocX.

[!\[Solution Explorer w Visual Studio z zaznaczonym plikiem Program.cs oraz klasą RelativeDirectory.cs w przykładach DocX.\](/wp-content/uploads/2016/03/img_56dabd92ba257.png)](/wp-content/uploads/2016/03/img_56dabd92ba257.png)

Keep that in mind when working with the code to either download the DocX package or change the code appropriately. Below you can find **Relative Directory** class just in case you have no access to sources.

```csharp
using System;
using System.IO;

namespace Examples
{
    class RelativeDirectory
    {
        // Author D. Bolton see https://cplus.about.com (c) 2010
        private DirectoryInfo _dirInfo;

        public string Dir
        {
            get
            {
                return _dirInfo.Name;
            }
        }

        public string Path
        {
            get { return _dirInfo.FullName; }
            set
            {
                try
                {
                    DirectoryInfo newDir = new DirectoryInfo(value);
                    _dirInfo = newDir;
                }
                catch
                {
                    // silent
                }
            }
        }
        public RelativeDirectory()
        {
            _dirInfo = new DirectoryInfo(Environment.CurrentDirectory);
        }

        public RelativeDirectory(string absoluteDir)
        {
            _dirInfo = new DirectoryInfo(absoluteDir);
        }

        public Boolean Up(int numLevels)
        {
            for (int i = 0; i &lt; numLevels; i++)
            {
                DirectoryInfo tempDir = _dirInfo.Parent;
                if (tempDir != null)
                    _dirInfo = tempDir;
                else
                    return false;
            }
            return true;
        }

        public Boolean Up()
        {
            return Up(1);
        }

        public Boolean Down(string match)
        {
            DirectoryInfo[] dirs = _dirInfo.GetDirectories(match + '*');
            _dirInfo = dirs[0];
            return true;
        }

    }

}
```

## Powiązane projekty

Projekty powiązane z tagami, kategoriami i treścią tego wpisu.

[Wszystkie projekty](/projekty/)

Trwa dopasowywanie projektów...

[Powrót do bloga](/blog/)

## Powiązane wpisy

- [Check an API write after a PowerShell timeout](/blog/verify-uncertain-api-outcomes-powershell/)
- [Inspect and convert mixed Office archives with PSWriteOffice](/blog/inspect-and-convert-mixed-office-archives-pswriteoffice/)
- [Compose PDF reports and automate page workflows with PSWriteOffice](/blog/automate-pdf-reports-and-page-workflows-pswriteoffice/)

<!-- PowerForge markdown artifact sha256:069B6081509C516DFCD54712F97106434CF89DC9979DE747A6636CA6E3C5C71B -->
