multiple format into one cell of table in word 2007 using c#

Answered multiple format into one cell of table in word 2007 using c#

  • Tuesday, May 01, 2012 8:20 AM
     
     

    I try create a table in word 2007, example: 

    color        green,blue

    hight        1.5(inch)

    but i can't format normal text and bold text into one cell. Held me!

    Thanks and sorry for my english!


All Replies

  • Wednesday, May 02, 2012 7:07 AM
     
     

    Hi Hungnguyen10,

    Welcome to the MSDN forum.

    According to your description, I'd like to move this thread to word Forum for better support, where more experts live.

    Thanks for your understanding.

     

    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

  • Wednesday, May 02, 2012 10:45 AM
    Moderator
     
     Answered Has Code

    Hi Hungnguyen10,
     
    Welcome to MSDN Forum.

    According to your description, I've built a Document Customization. The code is as follows:

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml.Linq;
    using Microsoft.Office.Tools.Word;
    using Microsoft.VisualStudio.Tools.Applications.Runtime;
    using Office = Microsoft.Office.Core;
    using Word = Microsoft.Office.Interop.Word;
    
    namespace Table_In_Word
    {
        public partial class ThisDocument
        {
            private void ThisDocument_Startup(object sender, System.EventArgs e)
            {
                Object start = System.Type.Missing;
                Object end = System.Type.Missing;
                Word.Range range = this.Range(ref start, ref end);
    
                Word.Table table = this.Tables.Add(range, 2, 2);
                table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleDouble;
                table.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                table.Columns.Width = 60;
                table.Rows.Height = 20;
                table.Cell(1, 1).Range.Text = "Color";
                table.Cell(2, 1).Range.Text = "Height";
                table.Cell(1, 2).Range.Text = "green,blue";
                table.Cell(2, 2).Range.Text = "1.5(inch)";
                
                //boldrange_1 for "green", boldrange_2 for "inch".
                Word.Range boldrange_1 = table.Cell(1, 2).Range;//Assign the whole cell range to boldrange, then adjust it with SetRange method.
                boldrange_1.SetRange(table.Cell(1, 2).Range.Start, table.Cell(1, 2).Range.Words[1].End);
                boldrange_1.Bold= 1;
    
                Word.Range boldrange_2 = table.Cell(2, 2).Range;
                boldrange_2.SetRange(table.Cell(2, 2).Range.Words[4].End, table.Cell(2, 2).Range.Words[5].End);
                boldrange_2.Font.Bold = 1;
                //I've found that boldrange_2.Font.Bold = 1; and boldrange_2.Bold = 1; have the same effect.
    
            }
    
            private void ThisDocument_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #region VSTO Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisDocument_Startup);
                this.Shutdown += new System.EventHandler(ThisDocument_Shutdown);
            }
    
            #endregion
        }
    }

    This snippet of code should be included in your ThisDocument.cs file, and the effect is showing below.

    Hope it helps.
    If you still have some other confusion, please feel free to let know. I’m looking forward to your reply.

    Best Regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

  • Wednesday, May 02, 2012 4:40 PM
     
     

    This very helfull for me.

    Thanks you very much, Quist Zhang!!!