none
How to draw the diagonal lines in table MS word using C#?

    Question

  • Hi All,

    Can any one please tell howto draw the Diagonal lines in MS Word Table using C#.

    Thanks,

    Kesavan R.

    • Moved by Kee Poppy Friday, March 16, 2012 2:30 AM (From:Windows Presentation Foundation (WPF))
    Wednesday, March 14, 2012 3:50 AM

Answers

  • Kesavan,

    Thank you for posting.

    Please try the following code snippet which I made:

    using System;
    using Word = Microsoft.Office.Interop.Word;
    
    namespace WordCreateDiagnalLine
    {
        class Program
        {
            static void Main(string[] args)
            {
                Word.Application wordApp = null;
                Word.Document oDoc = null;
                Word.Range rng = null;
                
                try
                {
                    wordApp = new Word.Application();
                    wordApp.Visible = true;
                    string other = AppDomain.CurrentDomain.BaseDirectory + "Other.docx";
                    oDoc = wordApp.Documents.Add();
                    // C#
                    rng = oDoc.Range();
                    CreateTable(oDoc,rng);
    
                    CreateDiagonal(oDoc.Tables[1].Cell(1,1).Range);  
                }
    
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    
            private static void CreateDiagonal(Word.Range rng)
            {
                rng.Select();
                rng.Borders[Word.WdBorderType.wdBorderDiagonalDown].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
            }
    
            private static void CreateTable(Word.Document oDoc, Word.Range rng)
            {
                // C#
                // Move to the end.
                rng.SetRange(rng.End, rng.End);
    
                // Add the table.
                Object defaultTableBehavior = Type.Missing;
                Object autoFitBehavior = Type.Missing;
                Word.Table tbl = oDoc.Tables.Add(rng, 2, 3,
                    ref defaultTableBehavior, ref autoFitBehavior);
                tbl.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                tbl.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
            }
        }
    }

    Hope this can help you and just feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Bruce Song Monday, April 02, 2012 6:45 AM
    Monday, March 19, 2012 2:42 AM

All replies

  • You should ask this in the VSTO forum .... they would know this better...

    Kenneth

    Thursday, March 15, 2012 1:16 AM
  • Hi All,

    Can any one please tell howto draw the Diagonal lines in MS Word Table using C#.

    Thanks,

    Kesavan R.

    Thursday, March 15, 2012 4:57 AM
  • Hi Kenneth,

    Thanks for your suggestion.

    Thanks,

    Kesavan R.

    Thursday, March 15, 2012 4:59 AM
  • Hi Kesavan,

    We have word for developers forum would have more resources for your question. I will move this thread there for your convenience.

    Have a nice day,


    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us

    Friday, March 16, 2012 2:30 AM
  • Kesavan,

    Thank you for posting.

    Please try the following code snippet which I made:

    using System;
    using Word = Microsoft.Office.Interop.Word;
    
    namespace WordCreateDiagnalLine
    {
        class Program
        {
            static void Main(string[] args)
            {
                Word.Application wordApp = null;
                Word.Document oDoc = null;
                Word.Range rng = null;
                
                try
                {
                    wordApp = new Word.Application();
                    wordApp.Visible = true;
                    string other = AppDomain.CurrentDomain.BaseDirectory + "Other.docx";
                    oDoc = wordApp.Documents.Add();
                    // C#
                    rng = oDoc.Range();
                    CreateTable(oDoc,rng);
    
                    CreateDiagonal(oDoc.Tables[1].Cell(1,1).Range);  
                }
    
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    
            private static void CreateDiagonal(Word.Range rng)
            {
                rng.Select();
                rng.Borders[Word.WdBorderType.wdBorderDiagonalDown].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
            }
    
            private static void CreateTable(Word.Document oDoc, Word.Range rng)
            {
                // C#
                // Move to the end.
                rng.SetRange(rng.End, rng.End);
    
                // Add the table.
                Object defaultTableBehavior = Type.Missing;
                Object autoFitBehavior = Type.Missing;
                Word.Table tbl = oDoc.Tables.Add(rng, 2, 3,
                    ref defaultTableBehavior, ref autoFitBehavior);
                tbl.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                tbl.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
            }
        }
    }

    Hope this can help you and just feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Bruce Song Monday, April 02, 2012 6:45 AM
    Monday, March 19, 2012 2:42 AM