0

I'm tyring to read out a table of content from a word document. I made a working system for that but I don't like it at all and I was thinking if there is any way to do this much more simple. My Goal is to show every content into a Datagridview. Sry for bad English :)

static void Main(string[] args)
    {
        Application app = new Application();
        Document doc = app.Documents.Open("C:\\Users\\----\\Desktop\\Kopie.docx");

        int count = doc.Words.Count;

            var table = doc.TablesOfContents[1].Range.Text;
            var strings = table.Split(new string[] {"\r"},StringSplitOptions.None);
            List<string> inhalt = new List<string>();
            foreach (var item in strings)
            {
                var tmpA= item.Split(new string[] { "\t" }, StringSplitOptions.None);
                if (tmpA.Length==1)
                {
                    break;
                }
                string erg = tmpA[0] +" "+ tmpA[1];
                inhalt.Add(erg);

            }
        inhalt.RemoveAt(0);

        app.Quit();
        Console.Read();

    }
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
WalterDerHalter
  • 29
  • 2
  • 12
  • The question is: what do you want to *do* with that data. Do you want to transfer it to a database, or print it out, or... ? We need to know about the *goal* in order to recommend a method... – LocEngineer Jul 04 '19 at 08:31
  • thx for the tip – WalterDerHalter Jul 04 '19 at 08:35
  • Oh, so a DatagridView. Doesn't look like WinForms, which is what you'd do, so you can *display* a datagrid. Have a look at the upvoted answers here: https://stackoverflow.com/questions/26232928/how-to-fill-datagridview-a-delimited-text-from-a-richtextbox and here: https://stackoverflow.com/questions/52866098/delimited-text-to-datatable and here: https://stackoverflow.com/questions/11720787/how-can-i-correctly-parse-a-text-file-delimited-by-white-space – LocEngineer Jul 04 '19 at 10:02

0 Answers0