Null
Null

Reputation: 152

Setting word document table cell margin programmatically using c#

As we can change default table cells margin in MS WORD using this steps

Click the table.
On the Table menu, click Table Properties, and then click the Table tab.
Click Options.
Under Default cell margins, enter the new values you want.

and exactly I want to ask how to do this programmatically I have tried top and bottom padding properties but it did not work and also I tried spacing but I did not work too so is there any way to set default cell margin using Microsoft.Interop.Word libraries thank you so much

PS: I am adding tables in header and footer using Microsoft.Interop.Wordeverything gone perfect expect this :/

Upvotes: 1

Views: 5693

Answers (1)

Loathing
Loathing

Reputation: 5266

There is Table padding, and there is also Cell padding. I'm guessing that Table padding contains the default values to apply to new cells added to the table. Probably use Cell padding to change the existing cells. E.g.

    Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
    Documents docs = app.Documents;
    Document doc = docs.Open("C:\\temp\\Test2.docx", ReadOnly:true);
    Table t = doc.Tables[1];
    double b1 = t.BottomPadding;
    double t1 = t.TopPadding;
    double r1 = t.RightPadding;
    double l1 = t.LeftPadding;

    Range r = t.Range;
    Cells cells = r.Cells;
    for (int i = 1; i <= cells.Count; i++) {
        Cell cell = cells[i];
        double b2 = cell.BottomPadding;
        double t2 = cell.TopPadding;
        double r2 = cell.RightPadding;
        double l2 = cell.LeftPadding;

        // e.g. Here is the edit:
        cell.TopPadding = 21.6f;
        cell.BottomPadding = 28.8f;

        Range r2b = cell.Range;
        String txt = r2b.Text;
        Marshal.ReleaseComObject(cell);
        Marshal.ReleaseComObject(r2b);
    }

    doc.Close(false);
    app.Quit(false);
    Marshal.ReleaseComObject(cells);
    Marshal.ReleaseComObject(r);
    Marshal.ReleaseComObject(t);
    Marshal.ReleaseComObject(doc);
    Marshal.ReleaseComObject(docs);
    Marshal.ReleaseComObject(app);

Upvotes: 4

Related Questions