none
C#NET2008 Microsoft WORD 2003, Row Gridline Bottom Border, Repeat Column Header text

    Question

  • Hullo Good Guys,

    I need your help. Please help me.

    I am using C#NET2008 and Microsoft WORD 2003.

    I am transferring 178 Customer Order transactions onto Non Template WORD Documents to print it as Transaction statements after inserting Table and created column headers Text

     

    Logically at run time, my coding created the WORD Document and inserted a TABLE with a single row and 3 columns. The idea is to create Column Headers.

     

    Within the WHILE (DATAREADER.READ()) loop I have a routine to insert a new row

     

    These are my problems:

     

    Problem #1.

     How to set the Row GridLine Bottom Border each time a row is insert onto the Table within the WHILE (DATAREADER.READ()) LOOP.

      

    Because of 178 transactions, at the end of the document Microsoft WORD Automatically created a new page when the new row is inserted onto the extended table.

    Problem #1.

    How do I determine a new page created and within the New Page the extended table  first row I want to repeat the column header text on it.

     

     

    Here are the over all coding

     

      private void FCreateColumnHeader()

            {

     

              //declare variable

             int intRow;

             int intCol = 0;

             intRow = 1;

     

             // ----- insert Table -----

             // insert a 9 column and 1 row table to create column header than add row after that for details

           objTableRange =objDoc.Bookmarks.get_Item(ref objEndOfDoc).Range;

            objTable = objDoc.Tables.Add(objTableRange,1, 9,  ref oMissing, ref oMissing);

           objTableRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;                 

            objTableRange.ParagraphFormat.SpaceAfter = 3;

     

           // ------ create column header ------               

           //OrderID column

           intCol += 1;                                            

           objTable.Cell(intRow, intCol).Range.Text = "Order ID";

           objTable.Columns[intCol].Width = 50;

           objTableRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;

     

           //OrderDate

           intCol += 1;

           objTable.Cell(intRow, intCol).Range.Text = "Order Date";

           objTable.Columns[intCol].Width = 55;

           objTableRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;

     

           //Cost Price

           intCol +=1;

           objTable.Cell(intRow, intCol).Range.Text ="Cost Price;

           objTable.Columns[intCol].Width = 60;

           objTableRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;

     

          }

     

      private void FFillWordTableWithData()

      {

    while (DR.Read())

      {

         Word.Row row = objTable.Rows.Add(ref oMissing);    //add row

         intRow += 1;

         intCol += 1;

                                    

       //set row Font Format

        objTable.Rows[intRow].Range.Font.Name = "Arial";

        objTable.Rows[intRow].Range.Font.Size = 8;

        objTable.Rows[intRow].Range.Font.Bold = 0;  // 0 = false

       objTable.Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;

                                                                               

     

      //fill data on row and column

      objTable.Cell(intRow, 1).Range.Text  = DR["OrderID"] == System.DBNull.Value ? string.Empty: Convert.ToString(DR["OrderID"]);

      objTable.Cell(intRow, 2).Range.Text =  DR["OrderDate"] == System.DBNull.Value ? string.Empty: Convert.ToString(DR["OrderDate"]);

     objTable.Cell(intRow, 3).Range.Text =  DR["Cost Price”]= System.DBNull.Value ? string.Empty: Convert.ToString(DR["ShipDate"]);

    }

    }

     

    Please help me. Thank You


    Thank you very Much, Have a Good Day. Cheers, Lennie
    • Moved by Aspen VJ Monday, December 27, 2010 2:47 AM (From:Windows Forms Data Controls and Databinding)
    • Moved by Bessie Zhao Monday, December 27, 2010 7:54 AM for more support (From:Visual Studio Tools for Office)
    Saturday, December 25, 2010 9:08 AM

Answers

  • Hello Tee

    As long as you haven't explicitly split the table, Word will consider it a single table no matter how many pages it covers.

    Or, to put it another way: If the table breaks across the pages automatically, as far as Word is concerned it's one table. If at any point you insert a section break, a page break, or use a split method, then it will not be a single table.

    So: doc.Tables[1] will work across multiple page tables, as long as the table hasn't been split on purpose by the user or by code.

    you can test this by doing something like tbl.Select() then look at the result (whether the entire table is highlighted).


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Tee Leong Saturday, January 01, 2011 9:38 AM
    Friday, December 31, 2010 8:32 AM

All replies

  •  Hullo Good Friends,

    I encountered 2 problems using C#NET3.5 and Microsoft WORD 2003.

    I need your help. Please help me

     

    Problem #1

    During the application runtime, logically a table with one row and 9 columns is inserted onto the WORD Document. Prior to Invoice transaction is fill into the Row.CELL, a new row is added into the table. After adding new row HOW TO SET THE HORIZONTAL BORDER beneath it?

     

    Problem #2.

    On one occasion, a particular Clients has 176 invoice transactions, and the WORD Document automatically created a new Page. How to determine that a new page is created so that the Transaction Banner text is insert prior to the continuous of adding rows to the existing table.

    As an appreciation of you helping me, I would like to take his opportunity to send you greetings and wishes.

     A BLESED MERRY CHRISTMAS AND GRACEFUL HAPPY NEW YEAR 2011 TO YOU AND YOUR FAMILY. As a Christian, I will be praying to my LORD JESUS to bless and protect you and family.

    Thank you for helping me.

     


    Thank you very Much, Have a Good Day. Cheers, Lennie
    Thursday, December 23, 2010 10:52 AM
  • Hi Tee,

     Base on your description,  I think it is little about winform.

    This forum is dedicated to Windows Forms technology. I'm moving this thread from Base "Windows Forms" to "VSTO" forum, since this issue is related to VSTO. There are more VSTO experts in that forum.

    Thank you for understanding and supporting.

     

    Best Regards,

    Vin Jin

     


    Vin Jin [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, December 27, 2010 2:47 AM
  • Hello Tee,

    Thanks for posting. Basically, we could record a macro. Then take look at this VBA code and see how it works. To record a macro, please refer to this thread: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/fdc6cfb5-e7cc-466f-95f0-d8cd29f49d2d/.

    Do you mean repeat as header row at the top of each page? If yes, please use HeadingFormat property. For more information, please refer to Cindy's post in this thread: http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/1e931e84-d7fb-41fc-b344-88b181599a95.

    For Row Gridline Bottom Border, I think you could get it by looking VBA code above. If it does not help you, just feel free to follow up. Have a nice day.


    Bessie Zhao [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, December 27, 2010 9:48 AM
  • Hi Vin Jin,

    What is the meaning of VSTO that you suggested ?

    Can you please explain the meaning of it please


    Thank you very Much, Have a Good Day. Cheers, Lennie
    Tuesday, December 28, 2010 4:29 AM
  • Hi Bessie Zhao,

    Do you mean repeat as header row at the top of each page? If yes, please use HeadingFormat property. For more information, please refer to Cindy's post in this thread: http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/1e931e84-d7fb-41fc-b344-88b181599a95

    Regarding your suggestion to view the URL link relating to Repeating Header rows, this sample coding how does it relate to my original coding that I posted as my problem.

     oTable.Rows(1).HeadingFormat = true


    Thank you very Much, Have a Good Day. Cheers, Lennie
    Tuesday, December 28, 2010 4:41 AM
  • Hi Tee,

    Welcome to MSDN forum and we are glad to help with you.

    After reading your post, I knew that problem:

    Problem #1

    During the application runtime, logically a table with one row and 9 columns is inserted onto the WORD Document. Prior to Invoice transaction is fill into the Row.CELL, a new row is added into the table. After adding new row HOW TO SET THE HORIZONTAL BORDER beneath it?  

    If you set the border of the table, you needn't set the horizontal border beneth it. When inserting a new row, it will automatically set the border.

     

    Problem #2.

    On one occasion, a particular Clients has 176 invoice transactions, and the WORD Document automatically created a new Page. How to determine that a new page is created so that the Transaction Banner text is insert prior to the continuous of adding rows to the existing table.

    You can get the current page number at first and store it in a variable.When inserting rows, try to get the current page number and compare to the variable. You can judge a new page is created via this method.

     

    Below is my code for you to reference:

    public static void AutomateWord()
            {
                
    object missing = Type.Missing;
                
    object notTrue = false;
     
                Word.
    Application oWord = null;
                Word.
    Documents oDocs = null;
                Word.
    Document oDoc = null;
                Word.
    Paragraphs oParas = null;
                Word.
    Paragraph oPara = null;
                Word.
    Range oParaRng = null;
                Word.
    Font oFont = null;
     
                
    try
                {
                    
    // Create an instance of Microsoft Word and make it invisible.
     
                    oWord = 
    new Word.Application();
                    oWord.Visible = 
    false;
                    
    Console.WriteLine("Word.Application is started");
     
                    
    // Create a new Document.
     
                    oDocs = oWord.Documents;
                    oDoc = oDocs.Add(
    ref missing, ref missing, ref missing, ref missing);
                    
    Console.WriteLine("A new document is created");
     
                    
    //Insert a 1 x 9 table, fill it with data
                    
    object oEndOfDoc = "\\endofdoc"/* \endofdoc is a predefined bookmark */
                    
    object oMissing = System.Reflection.Missing.Value;
                    Word.
    Table oTable;
                    Word.
    Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                    wrdRng = oDoc.Bookmarks.get_Item(
    ref oEndOfDoc).Range;
                    oTable = oDoc.Tables.Add(wrdRng, 1, 9, 
    ref oMissing, ref oMissing);
                    oTable.Range.ParagraphFormat.SpaceAfter = 6;
     
                    
    // set the border of table
                    oTable.Borders.InsideLineStyle = Word.
    WdLineStyle.wdLineStyleSingle;
                    oTable.Borders.OutsideLineStyle = Word.
    WdLineStyle.wdLineStyleSingle;
                    
    int r, c;
                    
    string strText;
                    
    for (r = 1; r <= 1; r++)
                    {
                        
    for (c = 1; c <= 9; c++)
                        {
                            strText = 
    "r" + r + "c" + c;
                            oTable.Cell(r, c).Range.Text = strText;
                        }
                    }
                    Word.
    WdStatistic stat = Word.WdStatistic.wdStatisticPages;
                    
    int numOfCurrentDoc = oDoc.ComputeStatistics(stat, oMissing);
     
                    
    // insert row
                    
    for (int i = 0; i < 30; i++)
                    {
                        
    // if create a new page, then insert banner text:
                        
    // here you can modify the code to merge the cells of the first row
                        
    // and only set the bottom line, then insert banner text 
                        
    // or you can insert a new table in the new page
                        
    if (oDoc.ComputeStatistics(stat, oMissing) > numOfCurrentDoc)
                        {
                            
    // innsert banner text
     
                            
    Console.WriteLine("Insert banner text");
     
                            oParas = oDoc.Paragraphs;
                            oPara = oParas.Add(
    ref missing);
                            oParaRng = oPara.Range;
                            oParaRng.Text = 
    " Invoice Transactions";
                            oFont = oParaRng.Font;
                            oFont.Bold = 1;
                            oParaRng.InsertParagraphAfter();
                            numOfCurrentDoc = oDoc.ComputeStatistics(stat, oMissing);
                        }
                        oTable.Rows.Add(oMissing);
                    }
     
     
                    
    Console.WriteLine("Save and close the document");
                    
    object fileName = @"E:\sample1.docx";
                    
    object fileFormat = Word.WdSaveFormat.wdFormatXMLDocument;
                    oDoc.SaveAs(
    ref fileName, ref fileFormat, ref missing,
                        
    ref missing, ref missing, ref missing, ref missing,
                        
    ref missing, ref missing, ref missing, ref missing,
                        
    ref missing, ref missing, ref missing, ref missing,
                        
    ref missing);
                    ((Word.
    _Document)oDoc).Close(ref missing, ref missing,
                        
    ref missing);
     
                    
    // Quit the Word application.
     
                    
    Console.WriteLine("Quit the Word application");
                    ((Word.
    _Application)oWord).Quit(ref notTrue, ref missing,
                        
    ref missing);
                }
                
    catch (Exception ex)
                {
                    
    Console.WriteLine("Solution1.AutomateWord throws the error: {0}",
                           ex.Message);
                }
                
    finally
                {
                    
    // Clean up the unmanaged Word COM resources by explicitly 
                    
    // calling Marshal.FinalReleaseComObject on all accessor objects. 
                    
    // See http://support.microsoft.com/kb/317109.
     
                    
    if (oFont != null)
                    {
                        
    Marshal.FinalReleaseComObject(oFont);
                        oFont = 
    null;
                    }
                    
    if (oParaRng != null)
                    {
                        
    Marshal.FinalReleaseComObject(oParaRng);
                        oParaRng = 
    null;
                    }
                    
    if (oPara != null)
                    {
                        
    Marshal.FinalReleaseComObject(oPara);
                        oPara = 
    null;
                    }
                    
    if (oParas != null)
                    {
                        
    Marshal.FinalReleaseComObject(oParas);
                        oParas = 
    null;
                    }
                    
    if (oDoc != null)
                    {
                        
    Marshal.FinalReleaseComObject(oDoc);
                        oDoc = 
    null;
                    }
                    
    if (oDocs != null)
                    {
                        
    Marshal.FinalReleaseComObject(oDocs);
                        oDocs = 
    null;
                    }
                    
    if (oWord != null)
                    {
                        
    Marshal.FinalReleaseComObject(oWord);
                        oWord = 
    null;
                    }
                }
            }

    I hope it can help you and feel free to follow up after you have tried. Wish you a nice holiday.

    Best Regards,


    Bruce Song [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.

    • Proposed as answer by Bruce Song Tuesday, December 28, 2010 7:36 AM
    Tuesday, December 28, 2010 7:31 AM
  • Hello Tee,

    Thanks for posting. Basically, we could record a macro. Then take look at this VBA code and see how it works. To record a macro, please refer to this thread: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/fdc6cfb5-e7cc-466f-95f0-d8cd29f49d2d/.

    Do you mean repeat as header row at the top of each page? If yes, please use HeadingFormat property. For more information, please refer to Cindy's post in this thread: http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/1e931e84-d7fb-41fc-b344-88b181599a95.

    For Row Gridline Bottom Border, I think you could get it by looking VBA code above. If it does not help you, just feel free to follow up. Have a nice day.


    Bessie Zhao [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.


    Hi Bessie Zhao [MSFT].

    Regarding your sugggestion to view this URL Link and your suggestion,I do appreciate you effor trying to help me but the URL Link does not relate to my initial posting of : C#NET2008 Microsoft WORD 2003, Row Gridline Bottom Border, Repeat Column Header text.

    The URL Link that you suggested relating to EXCEL Spreadsheet and not WORD Document Table.


    Thank you very Much, Have a Good Day. Cheers, Lennie
    Tuesday, December 28, 2010 1:12 PM
  • Hi Bruce Song,

    Thank you very much for sharing with the the sample coding relating to the problem that I posted here. I will try t out and get back to you.

    You are awesome and very accurate in sharing information with me. I really appreciate that very much. 

     

     


    Thank you very Much, Have a Good Day. Cheers, Lennie
    Tuesday, December 28, 2010 1:25 PM
  • Hi Bruce Song,

    This is Tee Leong Singer.....Yee...Haa........

    My IT Senior Team Leader asking me to use WORD 2003 TABLE Row Bottom Border.  I am completed ignorance of it.

    Can you please share with me the sample coding

    Thank you very much for helping.


    Thank you very Much, Have a Good Day. Cheers, Lennie
    Tuesday, December 28, 2010 10:50 PM
  • Hi Tee,

    Do you mean that you want to set the table's border style like this(each row has one bottom line):

    Order ID

    Order Date

    Cost Price

    Row1, Col1

    Row1, Col2

    Row1, Col3

    Row2, Col1

    Row2, Col2

    Row2, Col3

    ? If so, I will do further research and give you solution about this.

    Best Regards,


    Bruce Song [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.

    Wednesday, December 29, 2010 12:13 PM
  • Hullo Wonderful Bruce Song,

    Yes, the IT Senior Team Leader wants each row with a bottom line to reflect the original WORD Table that the Call Center did manually last year.


    Thank you very Much, Have a Good Day. Cheers, Lennie
    Wednesday, December 29, 2010 9:22 PM
  • Hi Tee

    In order to put a bottom border on each row a table:

        Dim doc As word.Document = Application.ActiveDocument
        Dim tbl As word.Table = doc.Tables(1)
        Dim rw As word.Row
        For Each rw In tbl.Rows
            rw.Borders(Word.WdBorderType.wdBorderBottom).Visible = True
        Next


    Cindy Meister, VSTO/Word MVP
    Thursday, December 30, 2010 7:31 AM
  • Hi Cindy Meister,

    Thank you for sharing your sample coding with me. The problem that I initialy posted is based on Softwar C#NET2008. So how does you VB sample coding looks like in C# format ?


    Thank you very Much, Have a Good Day. Cheers, Lennie
    Thursday, December 30, 2010 7:38 AM
  • Hi Tee

    Well, of the top of my head...

    Word.Document doc = wdApp.ActiveDocument;
    Word.Table tbl = doc.Tables[1];
    foreach (Word.Row rw in tbl.Rows)
    {
        rw.Borders[Word.WdBorderType.wdBorderBottom].Visible = True;
    }

    But C# might require you first create a Word.Border object before you can assign properties to it. And if you need to change more than one property (the LineStyle, for example) it makes more sense to create such an object, anyway.


    Cindy Meister, VSTO/Word MVP
    Thursday, December 30, 2010 7:59 AM
  • Hi Tee,

    I modified the code which I posted before, and you can download the project from my site(it can insert banner text to the top of the page after creating a new page):

    http://cid-c7d080162e80a07a.office.live.com/self.aspx/ProjectFiles/VS2010%5E_Word2010%5E_CS%5E_C%5E_AutomateWord.rar

    I think it may meet your needs, but you need to modify to adapt to your program.

    And thank Cindy very much for sharing the codes about setting the bottom border.

    I hope it can help you and feel free to follow up.

    Best Regards,


    Bruce Song [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.

    Thursday, December 30, 2010 9:49 AM
  • Hullo Awesome Cindy

    Thank you very much for sharing with me the sample coding. It's wonderful that you are very generous in sharing. I appreciate your help very much.

    Regarding thus coding: Word.Table tbl = doc.Tables[1];
    If the table extended to more than a single page does this still apply to it ?

    The reason I am asking is because alot of the  Customer transactions exceeded 178 rows because of Christmas festival sales.

     

     

     

     


    Thank you very Much, Have a Good Day. Cheers, Lennie
    Thursday, December 30, 2010 9:49 PM
  • Hello Tee

    As long as you haven't explicitly split the table, Word will consider it a single table no matter how many pages it covers.

    Or, to put it another way: If the table breaks across the pages automatically, as far as Word is concerned it's one table. If at any point you insert a section break, a page break, or use a split method, then it will not be a single table.

    So: doc.Tables[1] will work across multiple page tables, as long as the table hasn't been split on purpose by the user or by code.

    you can test this by doing something like tbl.Select() then look at the result (whether the entire table is highlighted).


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Tee Leong Saturday, January 01, 2011 9:38 AM
    Friday, December 31, 2010 8:32 AM
  • Hullo Wonderful Cindy Meister,

    Thank you very much for the new information. It does help me to understand logically. That is awesome.

    I will try out your suggestion in order to make amendment to my coding.

    You are wonderful and Awesome and generous in sharing information with me and helping me

    Yaa....Hooo.......


    Thank you very Much, Have a Good Day. Cheers, Lennie
    Saturday, January 01, 2011 9:40 AM
  • Hi Bruce,

     

    This Solution is really good.

    I am also creating a table but in a different way, i can't use this method because of time issue.

    I have pasted table in word document and while pasting new page is automatic created if rows are more then limit of page

    Can you suggest ,How to get Row number in particular page. So that i can insert table header in before that row.

    Thanks in advance.

     

    Regards

     

     

     

    Friday, February 18, 2011 1:02 PM