I get nested tables, but I need separated tables (Netoffice,Word)

394 views Asked by At

I wrote a WPF-Application to print a word documents with some tables in it, it works, but I get nested tables instead of separated tables.

I was looking at different post, but they all did not work quite well e.g.:

As well, as I tried to record a macro with word, which gave me

Selection.MoveDown Unit:=wdLine, Count:=1

and I turned this into:

wordApplication.Selection.MoveDown(WdUnits.wdLine, 1);

or even:

wordApplication.Selection.MoveDown(WdUnits.wdLine, 11);

This is the beginning of my originally code:

using System.Drawing;
using System.Reflection;
using NetOffice;
using Word = NetOffice.WordApi;
using NetOffice.WordApi.Enums;
using System.Globalization;
using System.Windows;
...
Word.Application wordApplication ...
Word.Document newDocument = wordApplication.Documents.Add();

This is the first table:

        Word.Table table = newDocument.Tables.Add(wordApplication.Selection.Range, 6, 2);

        // insert some text into the cells
        table.Cell(1, 1).Select();
        wordApplication.Selection.TypeText("...");
...

This is the second table, but the text goes into the last cell of my first table(last cell):

            Word.Table tableSchool = newDocument.Tables.Add(wordApplication.Selection.Range, 4, 2, WdDefaultTableBehavior.wdWord9TableBehavior);
        tableSchool.Cell(1, 1).Select();
        wordApplication.Selection.TypeText("...");
...

updated. 17 th june, 9 p.m

I found something,which puts the cursor outsisde of the table, but it is at the begining of the file, this shouldn't be:

    wordApplication.Selection.GoToNext(WdGoToItem.wdGoToSection);
    wordApplication.Selection.TypeText("GoToNext");

updated. 17 th june, 9:30 p.m

Using "InsertAfter" works, depending on where I put this statement, but this still does not solve my problem.

    wordApplication.Selection.Range.InsertAfter("test2");

    Word.Table table = newDocument.Tables.Add(wordApplication.Selection.Range, 4, 2, WdDefaultTableBehavior.wdWord9TableBehavior);

This should be adjusted "wordApplication.Selection.Range" to some kind of "InsertAfter", combining the two things I found out.

updated 18.06 07:30 a.m

If I use this, I get a "ComException"(macro recorder showed me this):

wordApplication.Selection.MoveRight(WdUnits.wdCharacter, 1);
        wordApplication.Selection.TypeParagraph();

updated 18.06 07:45 a.m

Then I found this, which is pretty close to my aim(,but it replaces the last cell of the previous table):

            wordApplication.Selection.EndKey(WdUnits.wdStory, WdMovementType.wdExtend);
        wordApplication.ActiveWindow.Selection.PasteAndFormat(WdRecoveryType.wdPasteDefault);

Thanks in advance :)

0

There are 0 answers