none
Can i create a table in word like below shown in C# RRS feed

  • Question

  • FICHE D’INSTRUCTIONS

    Centre de charge

    RXX 

    Page
    1/1

    Produit

    Produit

    N° Classement

    Classement

    Ensemble

    Ensemble

    Action

    Description de la tâche

    Pièce

    Pièce

    Indice

    Indice


    I want to Create a table like this in C# and put it at a Headers place. ? is it possible by c# ?

    Friday, July 29, 2011 4:44 PM

Answers

  • Thanks Andrei for your help.

    Hi Sakpalhemant,

    Thanks for your post.

    Just like Andrei said, you can record a macro in Word application to see how these actions performs in VBA, and then translate them to C# code.

    As the table style you posted is not clear enough to identify, I post a code snippet about some general methods when we create and edit tables in Word:

     Word.Document doc = this.Application.ActiveDocument;   
       Word.Range rng = doc.Range(ref missing, ref missing);
       //insert table
       Word.Table wTabel = doc.Tables.Add(rng, 4, 5, 
        Word.WdDefaultTableBehavior.wdWord9TableBehavior, 
        Word.WdAutoFitBehavior.wdAutoFitFixed);
     
       //some tabel properties
       wTabel.ApplyStyleHeadingRows = true;
       wTabel.ApplyStyleLastRow = false;
       wTabel.ApplyStyleFirstColumn = true;
       wTabel.ApplyStyleLastColumn = false;
       wTabel.ApplyStyleRowBands = true;
       wTabel.ApplyStyleColumnBands = false;
    
       //merge fisrst row of the table
       Word.Cells cells = wTabel.Rows[1].Cells;
       cells.Merge();
       
       //change table style
       wTabel.set_Style("Light Shading - Accent 3");
    
       //setting tabel border
       Word.Border borderTop = wTabel.Borders[Word.WdBorderType.wdBorderTop];
       Word.Border borderLeft = wTabel.Borders[Word.WdBorderType.wdBorderLeft];
       Word.Border borderBottom = wTabel.Borders[Word.WdBorderType.wdBorderBottom];
       Word.Border borderRight = wTabel.Borders[Word.WdBorderType.wdBorderRight];
       Word.Border borderHorizontal = wTabel.Borders[Word.WdBorderType.wdBorderHorizontal];
       Word.Border borderVertical = wTabel.Borders[Word.WdBorderType.wdBorderVertical];
    
       borderTop.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
       borderTop.LineWidth = Word.WdLineWidth.wdLineWidth050pt;
       borderTop.Color = Word.WdColor.wdColorBlack;
    
       borderLeft.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
       borderLeft.LineWidth = Word.WdLineWidth.wdLineWidth050pt;
       borderLeft.Color = Word.WdColor.wdColorBlack;
    
       borderBottom.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
       borderBottom.LineWidth = Word.WdLineWidth.wdLineWidth050pt;
       borderBottom.Color = Word.WdColor.wdColorBlack;
    
       borderRight.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
       borderRight.LineWidth = Word.WdLineWidth.wdLineWidth050pt;
       borderRight.Color = Word.WdColor.wdColorBlack;
    
       borderHorizontal.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
       borderHorizontal.LineWidth = Word.WdLineWidth.wdLineWidth050pt;
       borderHorizontal.Color = Word.WdColor.wdColorBlack;
    
       borderVertical.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
       borderVertical.LineWidth = Word.WdLineWidth.wdLineWidth050pt;
       borderVertical.Color = Word.WdColor.wdColorBlack;
    
       //setting backcolor of a cell
       Word.Cell c1 = wTabel.Cell(1, 1);
       c1.Shading.Texture = Word.WdTextureIndex.wdTextureNone;
       c1.Shading.ForegroundPatternColor = Word.WdColor.wdColorAutomatic;
       c1.Shading.BackgroundPatternColor = Word.WdColor.wdColorBrightGreen;
    
       //format table font
       c1.Range.Font.Bold = 1;
       //pass a value to a cell of the tabel
       c1.Range.Text = "Hello World";  
    


    You can do a little bit of modifications to fit your requirement.

    Have a nice day,


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Monday, August 1, 2011 7:43 AM
    Moderator

All replies

  • Hello,

    Absolutely. I'd recommend that you record a macro while creating such a table in Word. The macro will tell you what objects and their members are involved in the process. Then you can translate the macro to your language. What do you think?


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Friday, July 29, 2011 4:47 PM
  • Thanks Andrei for your help.

    Hi Sakpalhemant,

    Thanks for your post.

    Just like Andrei said, you can record a macro in Word application to see how these actions performs in VBA, and then translate them to C# code.

    As the table style you posted is not clear enough to identify, I post a code snippet about some general methods when we create and edit tables in Word:

     Word.Document doc = this.Application.ActiveDocument;   
       Word.Range rng = doc.Range(ref missing, ref missing);
       //insert table
       Word.Table wTabel = doc.Tables.Add(rng, 4, 5, 
        Word.WdDefaultTableBehavior.wdWord9TableBehavior, 
        Word.WdAutoFitBehavior.wdAutoFitFixed);
     
       //some tabel properties
       wTabel.ApplyStyleHeadingRows = true;
       wTabel.ApplyStyleLastRow = false;
       wTabel.ApplyStyleFirstColumn = true;
       wTabel.ApplyStyleLastColumn = false;
       wTabel.ApplyStyleRowBands = true;
       wTabel.ApplyStyleColumnBands = false;
    
       //merge fisrst row of the table
       Word.Cells cells = wTabel.Rows[1].Cells;
       cells.Merge();
       
       //change table style
       wTabel.set_Style("Light Shading - Accent 3");
    
       //setting tabel border
       Word.Border borderTop = wTabel.Borders[Word.WdBorderType.wdBorderTop];
       Word.Border borderLeft = wTabel.Borders[Word.WdBorderType.wdBorderLeft];
       Word.Border borderBottom = wTabel.Borders[Word.WdBorderType.wdBorderBottom];
       Word.Border borderRight = wTabel.Borders[Word.WdBorderType.wdBorderRight];
       Word.Border borderHorizontal = wTabel.Borders[Word.WdBorderType.wdBorderHorizontal];
       Word.Border borderVertical = wTabel.Borders[Word.WdBorderType.wdBorderVertical];
    
       borderTop.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
       borderTop.LineWidth = Word.WdLineWidth.wdLineWidth050pt;
       borderTop.Color = Word.WdColor.wdColorBlack;
    
       borderLeft.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
       borderLeft.LineWidth = Word.WdLineWidth.wdLineWidth050pt;
       borderLeft.Color = Word.WdColor.wdColorBlack;
    
       borderBottom.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
       borderBottom.LineWidth = Word.WdLineWidth.wdLineWidth050pt;
       borderBottom.Color = Word.WdColor.wdColorBlack;
    
       borderRight.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
       borderRight.LineWidth = Word.WdLineWidth.wdLineWidth050pt;
       borderRight.Color = Word.WdColor.wdColorBlack;
    
       borderHorizontal.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
       borderHorizontal.LineWidth = Word.WdLineWidth.wdLineWidth050pt;
       borderHorizontal.Color = Word.WdColor.wdColorBlack;
    
       borderVertical.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
       borderVertical.LineWidth = Word.WdLineWidth.wdLineWidth050pt;
       borderVertical.Color = Word.WdColor.wdColorBlack;
    
       //setting backcolor of a cell
       Word.Cell c1 = wTabel.Cell(1, 1);
       c1.Shading.Texture = Word.WdTextureIndex.wdTextureNone;
       c1.Shading.ForegroundPatternColor = Word.WdColor.wdColorAutomatic;
       c1.Shading.BackgroundPatternColor = Word.WdColor.wdColorBrightGreen;
    
       //format table font
       c1.Range.Font.Bold = 1;
       //pass a value to a cell of the tabel
       c1.Range.Text = "Hello World";  
    


    You can do a little bit of modifications to fit your requirement.

    Have a nice day,


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Monday, August 1, 2011 7:43 AM
    Moderator