none
Using VSTO, how can we convert a selected text in MS Word into a table RRS feed

  • Question

  • I am programming an Application-Level AddIn using VSTO 2008 in C#. I want to convert the following text into a 2x2 table

     

    Text to convert: a,b~c,d

     

    The above text should convert to a 2x2 table as:

    ------

    |a | b|

    ------

    |c | d|

    ------

     

    I select a range for the above text, say, oRange. Use oRange.Find object to find and replace the “~” with a carriage return “\r”. During debugging, I can see that the above text has been converted to as follows:

    a,b

    c,d

     

    But when I use oRange.ConvertToTable() method as shown below:

    object oSeparator = Word.WdTableFieldSeparator.wdSeparateByCommas;
    
    oRange.ConvertToTable(ref oSeparator, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    

    It converts the above text to a 1x3 table as shown below with a weird symbol (an empty squared box)  in between b and c (I have put a ? in place of that empty box). This empty box seems to be a newline character which means that the VSTO is converting the carriage return (ENTER) “\r” into a newline character (Shift+ENTER):

    ------------

    |a | b ? c | d|

    ------------

     

    A Microsoft Moderator of this forum Cindy Meister has mentioned in a response to a post in this forum that Object Model converts ENTER to Shift+ENTER transparently. If that is the case how can I convert the above text into a table programmatically using VSTO?

     

    Please help. Thanks,

    Nam

     

    Wednesday, December 28, 2011 3:54 AM

Answers

  • Hi namwam

    Thanks for the additional information. The code's font was so small I couldn't see the assignment for the separator clearly <sigh>

    I think this line is the key:

      oFind.Replacement.Text = "\r";

    Change this to:

      oFind.Replacement.Text = "^p";

    That's the special code Word has for finding/inserting its paragraph marks.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by namwam Friday, December 30, 2011 4:42 AM
    Thursday, December 29, 2011 9:42 AM
    Moderator

All replies

  • Hi Nam

    Please show us how you've defined oSeparator for use as a parameter in ConvertToTable. Most likely, it's not being recognized by Word as the equivalent of a Word paragraph mark. (You're dealing with the Word API, here, not VSTO by the way.)

    The square character you describe means Word isn't recognizing it, so it's not Shift+Enter. Usually I'd expect \r to work, but \r\n (or in reverse order) will almost always cause problems such as you describe.

    You might try, instead of \r, defining the character code: (char)13


    Cindy Meister, VSTO/Word MVP
    Wednesday, December 28, 2011 3:32 PM
    Moderator
  • Hi Cindy,

    The separator oSeparator for use as a parameter in ConvertToTable is defined as shown in my previous post. The Find/Replace code is shown below

    Word.Find oFind = oRange.Find;
    oFind.ClearFormatting();
    oFind.MatchWholeWord = true;
    oFind.Text = "~";
    oFind.Replacement.Text = "\r";
    object oReplace = Word.WdReplace.wdReplaceAll;
    
    oFind.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oReplace, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

    Using (char)13 gives the error: Cannot implicitly convert char to sting.

    I am using VSTO 2008 with Office 2003.

    Thanks for trying to help,

    Nam




    • Edited by namwam Thursday, December 29, 2011 3:22 AM
    Thursday, December 29, 2011 1:55 AM
  • Hi namwam

    Thanks for the additional information. The code's font was so small I couldn't see the assignment for the separator clearly <sigh>

    I think this line is the key:

      oFind.Replacement.Text = "\r";

    Change this to:

      oFind.Replacement.Text = "^p";

    That's the special code Word has for finding/inserting its paragraph marks.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by namwam Friday, December 30, 2011 4:42 AM
    Thursday, December 29, 2011 9:42 AM
    Moderator
  • to get around the 'cannot convert to string' you need to do something like;

    string text;

    text[0] = (char)13;

    Or there are various in built C# methods that can convert char to string.

     

    Thursday, December 29, 2011 9:47 AM
  • Cindy, thank you for helping out. Your this suggestion worked: oFind.Replacement.Text = "^p";

    Nam

    Friday, December 30, 2011 4:41 AM