none
Replace the Textual values of Word table by Colored ovals in C# at the Run time RRS feed

  • Question

  • Hello,

    In my working project, I'm trying to replace the cells of Word table that have textual data by colored ovals into the C# code.

    The Word table will be generated dynamically at the run time through my code.For example, 

    • replace the value for the cell that has the text A by Red Oval
    • the value for the cell that has the text B by Green Oval
    • and the the value cell that has the text C by Yellow Oval. 

    I really cannot figure out how I can do this task

    Please if you could help me in that. 

    • Moved by Kristin Xie Monday, February 2, 2015 2:53 AM move to better forum
    Sunday, February 1, 2015 12:25 AM

Answers

  • Hi Bilal7777,

    According to the description, you want to repleace the specific text with some shapes.

    As far as I know, there is no such kind of API in Word object model we can use directly to ahcieve the goal. As a workaround, we can loop the cells of tables via Row.Cells and then get the text via the Cell.Range.Text.

    At last if the text meet the requirment, we can insert a colored Oval and put it into the specific cell. Here is a sample that insert a rec Oval into the first cell of table for your reference:

    Dim aShape As Shape
    Set aShape = ActiveDocument.Shapes.AddShape(MsoAutoShapeType.msoShapeOval, 10, 10, 20, 20, ActiveDocument.Tables(1).Rows(1).Cells(1).Range.Characters(1))
    aShape.Fill.ForeColor.RGB = RGB(255, 0, 0)
    aShape.ConvertToInlineShape
    aShape.Select
    Selection.Cut
    ActiveDocument.Tables(1).Rows(1).Cells(1).Range.Paste

    If you have any issues about convert it to C#, please let me know.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 2, 2015 7:40 AM
    Moderator

All replies

  • If you know how to use Word to replace the values, then there is a nifty trick that will tell you how to do the same thing from C#: start a Macro recording in Word; then perform the replacement manually; then stop the recording and examine the macro. Although the macro is generated in VB, it is usually reasonably simple to translate into C#. You can then use COM/Interop to execute the commands against Word from C#.

    And sorry, I don't know how to replace text with colored ovals in Word. This type of question would be better asked in one of the Microsoft Answers forums. Once you learn how to do that, you can come back, record the macro, and then convert it into C#.

    Another alternative is to use the OpenXML SDK to modify the .docx file directly. Once again, there is a trick that can help you if you know how to do the replacement in Word: Create two copies of the same .docx file. Then open one of the files in Word and do a manual replacement of a text into an oval. Save the file, and compare both files using the OpenXML Productivity Tool provided with the SDK. The tool will produce the C# code that converts one file into the other. You can then use that code as an example to learn how to perform the replacements.

    Sunday, February 1, 2015 6:38 PM
  • Hi Bilal7777,

    According to the description, you want to repleace the specific text with some shapes.

    As far as I know, there is no such kind of API in Word object model we can use directly to ahcieve the goal. As a workaround, we can loop the cells of tables via Row.Cells and then get the text via the Cell.Range.Text.

    At last if the text meet the requirment, we can insert a colored Oval and put it into the specific cell. Here is a sample that insert a rec Oval into the first cell of table for your reference:

    Dim aShape As Shape
    Set aShape = ActiveDocument.Shapes.AddShape(MsoAutoShapeType.msoShapeOval, 10, 10, 20, 20, ActiveDocument.Tables(1).Rows(1).Cells(1).Range.Characters(1))
    aShape.Fill.ForeColor.RGB = RGB(255, 0, 0)
    aShape.ConvertToInlineShape
    aShape.Select
    Selection.Cut
    ActiveDocument.Tables(1).Rows(1).Cells(1).Range.Paste

    If you have any issues about convert it to C#, please let me know.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 2, 2015 7:40 AM
    Moderator