none
how to insert table in word documents inline the text in c#?? RRS feed

  • Question

  • hi every one,

    I'm using a ribbon button which added some text and a table next to the text. it works well for the first click but when I click again the table appear in the first table. I want when I click the button the table appear next to the text. my code is

     internal void ToggleTableTextControlOnDocument()
            {
                object start = 0;
                object end = 0;
        
                Word.Range wdFunctionR = this.Application.ActiveDocument.Range(ref start, ref end);
               Document vstoDocument = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
                
               Word.Selection selection = Globals.ThisAddIn.Application.Selection;
                if (selection != null && selection.Range != null)
                {
                                
                     Word.Table newTable = Globals.ThisAddIn.Application.ActiveDocument.Tables.Add(
                                          wdFunctionR, 1, 1, ref missing, ref missing);
                            
                    Word.Border[] borders = new Word.Border[6];
                    borders[0] = newTable.Borders[Word.WdBorderType.wdBorderLeft];
                    borders[1] = newTable.Borders[Word.WdBorderType.wdBorderRight];
                    borders[2] = newTable.Borders[Word.WdBorderType.wdBorderTop];
                    borders[3] = newTable.Borders[Word.WdBorderType.wdBorderBottom];
                    borders[4] = newTable.Borders[Word.WdBorderType.wdBorderHorizontal];
                    borders[5] = newTable.Borders[Word.WdBorderType.wdBorderVertical];
                    this.Application.ActiveDocument.Tables[1].AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
                            
                                 
                Word.Cell cell = this.Application.ActiveDocument.Tables[1].Cell(1, 1);              
                    newTable.Range.ParagraphFormat.Alignment =
                    Word.WdParagraphAlignment.wdAlignParagraphRight;
                  
                    // Format each of the borders.
                    foreach (Word.Border border in borders)
                    {
                        border.LineStyle = Word.WdLineStyle.wdLineStyleDashDot;
                        border.Color = Word.WdColor.wdColorBlue;
                        newTable.Range.Font.Size = 8;
                        newTable.Range.Font.Name = "Verdana";
                    }
                    wdFunctionR.Application.Selection.TypeText("جا");
                    wdFunctionR.Application.Selection.TypeText(" ");
                }
            }

    Monday, September 17, 2012 2:33 PM

Answers

  • Hi Student

    The anser is "maybe".

    This isn't something Word really "wants" to do. I can think of a couple of ways I might try to do it as an end-user, in the UI, but they'd be very tricky to maintain.

    I think it would be best if you'd work on this first at the end-user level. See if you can find a combination of things Word "knows" how to do that will be stable enough for your purpose (the use to which the end-result will be put). You might want to discuss this with the folks on "Answers" as you'll find a lot of Word specialists there who know how to make Word jump through fiery hoops: http://answers.microsoft.com/en-us/office/forum/word

    Once you find a technique that suits your purpose come back here and list the steps you use to create that in the UI. You might also try recording those steps in a macro to get the basic starting syntax. With that information we should be able to help you find the code to generate the same solution that can be created as an end-user. (That's what the Word APIs were designed for.)

    If I were to try to cobble something together here, without knowing how it's intended to be used, I'd most likely end up spending hours for something that you will tell me you can't use because it doesn't do this or that or whatever. Much better for you to research this for yourself as you'll see immediately how something won't be suitable. Also, it's the best way to learn how to work with Word at the developer level - know it at the end-user level :-)


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, September 19, 2012 9:11 AM
    Moderator
  • Hi Fermin

    Oh, it can be done, but it's not straightforward and each possibility has certain problems. That's why I recommended a forum where there are a lot more Word layout specialists who regularly drop by. They'll be better at figuring out what would work optimally for "Student's" end requirements.

    Examples of possibilities, without going into a lot of detail:

    1. Tables can be formatted with "text wrap", like a graphic

    2. Tables can be nested within table cells

    3. Tables can be put in Frames or Text boxes and these positioned as graphics. (Advantage over 1 is that positioning is simpler; disadvantage is adressing the tables)


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, September 26, 2012 8:39 AM
    Moderator

All replies

  • It's because of how you're defining the target Range object. You tell Word the Range is this:

      Word.Range wdFunctionR = this.Application.ActiveDocument.Range(ref start, ref end);

    Where start and end are both 0.

    So, when you insert the table with this:

      Word.Table newTable = Globals.ThisAddIn.Application.ActiveDocument.Tables.Add(
                                          wdFunctionR
    , 1, 1, ref missing, ref missing);                   

    Each new table is inserted at the beginning of the document.

    If you want the table to be inserted adjacent to the current selection, then define wdFunctionR like this:
      Word.Range wdFuncitonR = this.Application.Selection.Range.Duplicate

    However, I advise you to use Selection sparingly. Unless you want to work with the user's selection, don't use Selection. Use Range instead. For example, if you want to insert text following the table:

      WordRange afterTable = newTable.Range;
      object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
      afterTable.Collapse(ref oCollapseEnd);
      afterTable.Text = "text after the table";
      //Select the range in the UI:
      afterTable.Select();


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, September 18, 2012 9:28 AM
    Moderator
  • thanx alot for your reply.

    but I want to insert text then a table next to the text when I click the ribbon button.

    your code also make the text under the table.

    can I do that??

    thanx...

    Wednesday, September 19, 2012 6:49 AM
  • Hi Student

    The anser is "maybe".

    This isn't something Word really "wants" to do. I can think of a couple of ways I might try to do it as an end-user, in the UI, but they'd be very tricky to maintain.

    I think it would be best if you'd work on this first at the end-user level. See if you can find a combination of things Word "knows" how to do that will be stable enough for your purpose (the use to which the end-result will be put). You might want to discuss this with the folks on "Answers" as you'll find a lot of Word specialists there who know how to make Word jump through fiery hoops: http://answers.microsoft.com/en-us/office/forum/word

    Once you find a technique that suits your purpose come back here and list the steps you use to create that in the UI. You might also try recording those steps in a macro to get the basic starting syntax. With that information we should be able to help you find the code to generate the same solution that can be created as an end-user. (That's what the Word APIs were designed for.)

    If I were to try to cobble something together here, without knowing how it's intended to be used, I'd most likely end up spending hours for something that you will tell me you can't use because it doesn't do this or that or whatever. Much better for you to research this for yourself as you'll see immediately how something won't be suitable. Also, it's the best way to learn how to work with Word at the developer level - know it at the end-user level :-)


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, September 19, 2012 9:11 AM
    Moderator
  • Hi ISE Student,

    If I have understood you clearly, I think your request is not supported.

    It seems that you cannot have 2 tables in one row. Since this cannot be achieved manually, I think there's also no programmatic way to achieve this.

    HTH

    Best regards,
    Fermin

    Wednesday, September 26, 2012 2:29 AM
  • Hi Fermin

    Oh, it can be done, but it's not straightforward and each possibility has certain problems. That's why I recommended a forum where there are a lot more Word layout specialists who regularly drop by. They'll be better at figuring out what would work optimally for "Student's" end requirements.

    Examples of possibilities, without going into a lot of detail:

    1. Tables can be formatted with "text wrap", like a graphic

    2. Tables can be nested within table cells

    3. Tables can be put in Frames or Text boxes and these positioned as graphics. (Advantage over 1 is that positioning is simpler; disadvantage is adressing the tables)


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, September 26, 2012 8:39 AM
    Moderator
  • Hi Cindy,

    Thank you very much. I've learned a great deal from you.

    Regards, Fermin

    Monday, October 8, 2012 1:54 AM