Word-Dokument mit Tabelle per Code erstellen

Microsoft WordAnbei ein einfach gehaltener Job, der ein Word-Dokument mit einer Tabelle per Code erstellt. Das Beispiel legt auch den Rahmen und dessen Farbe der Tabelle fest.
Voraussetzung ist ein lokal installiertes Microsoft Word.

static void CreateWordFileWithTable(Args _args)
{
    COM wordApplication;
    COM wordTables;
    COM wordTable;
    COM wordSelection;
    COM wordTableRows;
    COM wordRange;
    COM wordTableCell;
    COM wordTableCellRange;
    COM wordDocuments;
    COM wordDocument;
    COM wordTableBorders;
    ;
 
    // Initialize Word object and document
    wordApplication = new COM("Word.Application");
    wordDocuments = wordApplication.documents();
    wordDocuments.add();
    wordDocument = wordDocuments.item(1);
 
    wordSelection = wordApplication.selection();
    wordRange = wordSelection.range();
 
    // Get table collection
    wordTables = wordSelection.tables();
 
    // Create table with 3 rows and 5 columns
    wordTable = wordTables.add(wordRange, 3, 5);
 
    // Fill cell: First line, second column
    wordTableCell = wordTable.Cell(1, 2);
    wordTableCellRange = wordTableCell.range();
    wordTableCellRange.text("Hello");

    // Fill cell: Second line, third column
    wordTableCell = wordTable.Cell(2, 3);
    wordTableCellRange = wordTableCell.range();
    wordTableCellRange.text("World");
 
    // Enable table borders
    wordTableBorders = wordTable.borders();
    wordTableBorders.enable(true);

    // Add colored borders
    wordTableBorders.InsideLineStyle(3);
    wordTableBorders.OutsideLineStyle(5);
    wordTableBorders.OutsideColorIndex(2);
 
    // Get table row collection and add a new row
    wordTableRows = wordTable.rows();
    wordTableRows.add();
 
    // Open word
    wordApplication.visible(true);
    wordApplication.finalize();
}

Das per obigem Code erstellte Word-Dokument sieht wie folgt aus:

Screenshot

Dieser Beitrag bezieht sich auf die Versionen:
Dynamics AX 2009, Dynamics AX 2012

Kommentar hinzufügen
 
 

 

 
 
 
Beiträge des aktuellen Monats
April 2024
MoDiMiDoFrSaSo
1234567
891011121314
15161718192021
22232425262728
2930 
 
© 2006-2024 Heinz Schweda | Impressum | Kontakt | English version | Mobile Version
Diese Webseite verwendet Cookies, um Benutzern einen besseren Service anzubieten. Wenn Sie weiterhin auf der Seite bleiben, stimmen Sie der Verwendung von Cookies zu.  Mehr dazu