none
Problem adding tables to a word document RRS feed

  • Question

  • I want to create a document with very small pages 3.5cm x 5cm there will be 1 header page then 1 record per page each page includes a string title and image and a list

    So I create a table for each page with 1 column and 3 rows (Title/Image/List)

    It all works fine except that it appears to insert the last table inside the next resulting in a lot of nested tables.

    The app is WPF, the code is complete and should just paste into  xmal.cs the xaml needs a button

    I am sure someone looking at it will know Immediately what is wrong

    using System;
    using System.Reflection;
    using System.Windows;


    namespace WordDocPlay
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                object oMissing = System.Reflection.Missing.Value;
                object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */

                //Start Word and create a new document.
                Microsoft.Office.Interop.Word.Application wApp;
                Microsoft.Office.Interop.Word.Document doc;
                wApp = new Microsoft.Office.Interop.Word.Application();
                wApp.Visible = true;
                doc = wApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                #region set the page parameters copied from mine
                doc.PageSetup.RightMargin = 0;
                doc.PageSetup.LeftMargin = 0;
                doc.PageSetup.TopMargin = 0;
                doc.PageSetup.BottomMargin = 0;
                doc.PageSetup.PageHeight = wApp.CentimetersToPoints(5f);
                doc.PageSetup.PageWidth = wApp.CentimetersToPoints(3.5f);
                wApp.ActiveDocument.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait;//Moved below
                #endregion

                //Insert a table, fill it with data, and make the first row
                Microsoft.Office.Interop.Word.Table wTbl;
                object start = 0;
                object end = 0;
                Microsoft.Office.Interop.Word.Range wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                //Microsoft.Office.Interop.Word.Range wrdRng = doc.Range(ref start, ref end);
                Object defaultTableBehavior = Type.Missing;
                Object autoFitBehavior = Type.Missing;
                #region Body
                string strText;
                int ssRow = 1;
                int rc = 10;//4 records
                int colNo = 1;

                #region Iterate the Dataset the dataset must be sorted alpha in reverse
                for (int dsr = 0; dsr <= rc - 1; dsr++)
                {
                    #region insert and  format the table


                    wTbl = doc.Tables.Add(wrdRng, 3, 1, ref defaultTableBehavior, ref autoFitBehavior);

                    wTbl.Columns[1].Width = wApp.CentimetersToPoints(3.5f);
                    wTbl.Rows[1].Height = wApp.CentimetersToPoints(1f);
                    wTbl.Rows[1].Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
                    wTbl.Rows[1].Range.Font.Bold = 1;
                    wTbl.Rows[2].Height = wApp.CentimetersToPoints(2);
                    wTbl.Rows[2].Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
                    wTbl.Rows[2].Range.Font.Bold = 0;
                    wTbl.Rows[3].Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
                    wTbl.Rows[3].Range.Font.Bold = 0;
                    wTbl.Rows[3].Height = wApp.CentimetersToPoints(2f);

                    #endregion

                    #region grab/use the data and interate the 3 rows of the table in reverse

                    int TblNo = (rc - dsr) - 1;
                    strText = "dish name " + TblNo;
                    wTbl.Cell(ssRow, colNo).Range.Text = strText;
                    ssRow++;
                    strText = "dish Image " + TblNo;
                    wTbl.Cell(ssRow, colNo).Range.Text = strText;
                    ssRow++;
                    strText = "dish Ingredients " + TblNo;
                    wTbl.Cell(ssRow, colNo).Range.Text = strText;


                    #endregion

                    ssRow = 1;
                    object oCollapseEnd = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;
                    wTbl.Range.Collapse(ref oCollapseEnd);
               
                }
                #endregion
                #endregion
            
                #region The Header page at the end of the doc it will appear first

                wTbl = doc.Tables.Add(wrdRng, 1, 1, ref defaultTableBehavior, ref autoFitBehavior);
                wTbl.PreferredWidth = wApp.CentimetersToPoints(3.5f);
                wTbl.Rows[1].Height = wApp.CentimetersToPoints(5f);
                wTbl.Rows[1].Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
                wTbl.Rows[1].Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                wTbl.Rows[1].Range.Font.Bold = 1;
                wTbl.Cell(1, 1).Range.Text = "Starters"; ;
                #endregion


                #region document, insert a hard page break.

                object oPos;
                double dPos = wApp.CentimetersToPoints(5f);
                oPos = wrdRng.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdVerticalPositionRelativeToPage);
                while (dPos >= Convert.ToDouble(oPos)) ;
                object oCollapseEnd2 = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;
                object oPageBreak2 = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
                wrdRng.Collapse(ref oCollapseEnd2);
                wrdRng.InsertBreak(ref oPageBreak2);
                wrdRng.Collapse(ref oCollapseEnd2);

                #endregion

            }
        }
    }


    jnc

    Tuesday, October 23, 2012 12:26 PM

Answers

  • Hi jnc,

    Thanks for posting in the MSDN Forum.

    I think the key of your issue is this:

    insert the last table inside the next resulting in a lot of nested tables.

    Is it right?

    Please try following snippet to see whether it can help you (I develop it under Office 2010, Visual Studio 201. Consider you used early version of it, please feel free to reply if  you have any trouble to approach it).

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Word = Microsoft.Office.Interop.Word;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                Word.Application wdApp = null;
                try
                {
                    wdApp = new Word.Application();
                    Word.Document wdDoc = wdApp.Documents.Add();
                    wdDoc.PageSetup.RightMargin = 0;
                    wdDoc.PageSetup.LeftMargin = 0;
                    wdDoc.PageSetup.TopMargin = 0;
                    wdDoc.PageSetup.BottomMargin = 0;
                    wdDoc.PageSetup.PageHeight = ((Word._Application)wdApp)
                        .CentimetersToPoints(5f);
                    wdDoc.PageSetup.PageWidth = ((Word._Application)wdApp)
                        .CentimetersToPoints(3.5f);
                    wdApp.ActiveDocument.PageSetup.Orientation = 
                        Word.WdOrientation.wdOrientPortrait;
                    
                    // Following part will insert two table into document.
                    Word.Table wdTable1 = wdDoc.Tables.Add(wdDoc.Range(), 1, 3);
                    wdTable1.Borders[Word.WdBorderType.wdBorderBottom]
                        .LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    wdTable1.Borders[Word.WdBorderType.wdBorderTop].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;
                    wdTable1.Borders[Word.WdBorderType.wdBorderLeft].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;
                    wdTable1.Borders[Word.WdBorderType.wdBorderRight].LineStyle = 
                        Word.WdLineStyle.wdLineStyleSingle;
                    wdTable1.Borders[Word.WdBorderType.wdBorderHorizontal]
                        .LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    wdTable1.Borders[Word.WdBorderType.wdBorderVertical].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;
                    // Key code before insert seconde tables.
                    wdApp.Selection.MoveDown(Unit: Word.WdUnits.wdLine,
                        Count: 1);
                    wdApp.Selection.TypeParagraph();
    
                    Word.Table wdTable2 = wdDoc.Tables.Add(wdApp.Selection.Range
                        ,1,3);
                    wdTable2.Borders[Word.WdBorderType.wdBorderBottom]
                        .LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    wdTable2.Borders[Word.WdBorderType.wdBorderTop].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;
                    wdTable2.Borders[Word.WdBorderType.wdBorderLeft].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;
                    wdTable2.Borders[Word.WdBorderType.wdBorderRight].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;
                    wdTable2.Borders[Word.WdBorderType.wdBorderHorizontal]
                        .LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    wdTable2.Borders[Word.WdBorderType.wdBorderVertical].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;
    
                    wdApp.Visible = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (wdApp != null)
                    {
                        ((Word._Application)wdApp).Quit();
                    }
                }
            }
        }
    }
    

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by jnc Wednesday, October 24, 2012 5:22 PM
    Wednesday, October 24, 2012 5:24 AM
    Moderator

All replies

  • Hi jnc,

    Thanks for posting in the MSDN Forum.

    I think the key of your issue is this:

    insert the last table inside the next resulting in a lot of nested tables.

    Is it right?

    Please try following snippet to see whether it can help you (I develop it under Office 2010, Visual Studio 201. Consider you used early version of it, please feel free to reply if  you have any trouble to approach it).

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Word = Microsoft.Office.Interop.Word;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                Word.Application wdApp = null;
                try
                {
                    wdApp = new Word.Application();
                    Word.Document wdDoc = wdApp.Documents.Add();
                    wdDoc.PageSetup.RightMargin = 0;
                    wdDoc.PageSetup.LeftMargin = 0;
                    wdDoc.PageSetup.TopMargin = 0;
                    wdDoc.PageSetup.BottomMargin = 0;
                    wdDoc.PageSetup.PageHeight = ((Word._Application)wdApp)
                        .CentimetersToPoints(5f);
                    wdDoc.PageSetup.PageWidth = ((Word._Application)wdApp)
                        .CentimetersToPoints(3.5f);
                    wdApp.ActiveDocument.PageSetup.Orientation = 
                        Word.WdOrientation.wdOrientPortrait;
                    
                    // Following part will insert two table into document.
                    Word.Table wdTable1 = wdDoc.Tables.Add(wdDoc.Range(), 1, 3);
                    wdTable1.Borders[Word.WdBorderType.wdBorderBottom]
                        .LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    wdTable1.Borders[Word.WdBorderType.wdBorderTop].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;
                    wdTable1.Borders[Word.WdBorderType.wdBorderLeft].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;
                    wdTable1.Borders[Word.WdBorderType.wdBorderRight].LineStyle = 
                        Word.WdLineStyle.wdLineStyleSingle;
                    wdTable1.Borders[Word.WdBorderType.wdBorderHorizontal]
                        .LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    wdTable1.Borders[Word.WdBorderType.wdBorderVertical].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;
                    // Key code before insert seconde tables.
                    wdApp.Selection.MoveDown(Unit: Word.WdUnits.wdLine,
                        Count: 1);
                    wdApp.Selection.TypeParagraph();
    
                    Word.Table wdTable2 = wdDoc.Tables.Add(wdApp.Selection.Range
                        ,1,3);
                    wdTable2.Borders[Word.WdBorderType.wdBorderBottom]
                        .LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    wdTable2.Borders[Word.WdBorderType.wdBorderTop].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;
                    wdTable2.Borders[Word.WdBorderType.wdBorderLeft].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;
                    wdTable2.Borders[Word.WdBorderType.wdBorderRight].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;
                    wdTable2.Borders[Word.WdBorderType.wdBorderHorizontal]
                        .LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    wdTable2.Borders[Word.WdBorderType.wdBorderVertical].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;
    
                    wdApp.Visible = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (wdApp != null)
                    {
                        ((Word._Application)wdApp).Quit();
                    }
                }
            }
        }
    }
    

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by jnc Wednesday, October 24, 2012 5:22 PM
    Wednesday, October 24, 2012 5:24 AM
    Moderator
  •  

    Thanks Tom,

    very useful, unfortunately it did not solve my problem so I changed my approach slightly and used bits of yours and mine combined.

    One question if I may, my Header page will have a full page image BEHIND text how do I do that, I have tried Z order and loading the image as a watermark, neither seem to work satisfactorily


    jnc

    Wednesday, October 24, 2012 5:22 PM
  • Hi jnc,

    One question if I may, my Header page will have a full page image BEHIND text how do I do that, I have tried Z order and loading the image as a watermark, neither seem to work satisfactorily

    Would you please post a new thread about it? I will continue track it, and I will involve experts into you issue if I can't handle it.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us


    Thursday, October 25, 2012 4:58 AM
    Moderator