Search code examples
c#ms-wordrangeoffice-addinsword-addins

Add table in current position with a word add-in


I want to add a table with my Word Add-in with data from my database. I've done that successfully but now I have a problem with the position of the table. I want to place it exactly where my current position in the Word document is. But so it's always added at the beginning. Does anyone know how to adjust the range that my start value is always my current position? This is a part of my code:

private void createTable_Click(object sender, EventArgs e) {
object start = 0, end = 0;
Word.Document document = Globals.ThisAddIn.Application.ActiveDocument; 
Word.Range rng = document.Range(ref start, ref end);

// Insert a title for the table and paragraph marks.
rng.InsertBefore("List"); 
rng.Font.Name = "Verdana"; 
rng.Font.Size = 16; 
rng.InsertParagraphAfter(); 
rng.InsertParagraphAfter(); 
rng.SetRange(rng.End, rng.End);

// Add the table.
rng.Tables.Add(document.Paragraphs[2].Range, 1, 7, ref missing, ref missing);

// Format the table and apply a style.
Word.Table tbl = document.Tables[1]; tbl.Range.Font.Size = 8;   
tbl.Borders[WdBorderType.wdBorderLeft].LineStyle =
  WdLineStyle.wdLineStyleSingle; 
tbl.Borders[WdBorderType.wdBorderRight].LineStyle =
  WdLineStyle.wdLineStyleSingle; 
tbl.Borders[WdBorderType.wdBorderTop].LineStyle =
  WdLineStyle.wdLineStyleSingle; 
tbl.Borders[WdBorderType.wdBorderBottom].LineStyle =
  WdLineStyle.wdLineStyleSingle; 
tbl.Borders[WdBorderType.wdBorderHorizontal].LineStyle =
  WdLineStyle.wdLineStyleSingle; 
tbl.Borders[WdBorderType.wdBorderVertical].LineStyle =
  WdLineStyle.wdLineStyleSingle;
tbl.Borders[WdBorderType.wdBorderBottom].Color = WdColor.wdColorBlack; tbl.Rows.Alignment = WdRowAlignment.wdAlignRowCenter; tbl.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitWindow);

Solution

  • On re-reading... To insert at the current position - if you mean where the cursor is:

    Word.Range rngSel = wdApp.Selection.Range;
    rngSel.Tables.Add(//params here);
    

    Otherwise, if you mean at the end of the information being inserted byy the code, instead of these two lines

    rng.InsertBefore("List"); 
    rng.Font.Name = "Verdana"; 
    rng.Font.Size = 16; 
    rng.InsertParagraphAfter(); 
    rng.InsertParagraphAfter(); 
    rng.SetRange(rng.End, rng.End);
    

    Use

    rng.Text = "List\n\n"
    rng.Font.Name = "Verdana"; 
    rng.Font.Size = 16; 
    rng.Collapse(WdCollapseDirection.wdCollapseEnd);
    

    \n inserts a new paragraph (carriage return) and can be included as part of a string.

    Assigning text directly to the Range and working with the Collapse method is (in my opinion) a bit more predictable than the various Insert methods. Some of the Insert methods include what's inserted in the range, others do not.

    FWIW when it's not clear what the problem might be it can help to put a rng.Select(); at a key point in the code and comment out the rest of the lines so that the code ends with the range in question visible. That can often be informative as to the origin of a problem with a range.