none
Table cell lost formatting RRS feed

  • Question

  • I'm working on Word VSTO application using C# language.

    When I have changed table cell text, formatting of cell is lost like text indentation, color, etc.

    Please note that I need to change the cell text more than time.

    Any Help?



    Thursday, July 17, 2014 7:00 AM

Answers

  • HI Saurabh

    RAnge.Text does not carry formatting. The code you're currently using is therefore dropping all formatting since you're essentially replacing the current content with "plain text".

    You need to APPEND new content to the cell, which is best done using the Range object. Something along these lines. Here's some VBA code that shows how it's done

        Dim newText As String
        Dim rng As word.Range
        
        newText = "some text"
        Set rng = ActiveDocument.Tables(1).Cell(1, 1).Range
        rng.Collapse wdCollapseEnd
        rng.MoveEnd wdCharacter, -1
        rng.Text = newText
    
    You should be able to "translate" this to C# without any great problems. The Collapse and MoveEnd methods need to use the ref keyword for parameters and you'll need to define the parameters as objects prior to using them in the methods. Word.WdCollapseDirection.wdCollapseEnd, Word.WdUnits.wdCharacter are the Enum values you need.

    Cindy Meister, VSTO/Word MVP, my blog

    Friday, July 18, 2014 4:30 PM
    Moderator

All replies

  • Hi Saurabh,

    How did you change the text of cell and set the text indentation for the cell? I test it with VBA quick and the code works well for me:

    Sub SetLeftPadding()
    ActiveDocument.Tables(1).Cell(1, 1).Range.Paragraphs(1).LeftIndent = 20
    End Sub
    Sub test()
    ActiveDocument.Tables(1).Cell(1, 1).Range.Text = "test"
    End Sub
    Screenshot:

    Can you show some code sampe with us to reproduce this issue.

    Best 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.

    Friday, July 18, 2014 3:10 AM
    Moderator
  • I simply get the cell text and append some text to it. I tried as follow:

    currentTable.Cell(1,1).Range.Text = currentTable.Cell(1,1).Range.Text + " some text";

    snapshot Before modification:

    snapshot after appending some text:

    How to preserve cell formatting after any changes in the cell text? and please give the example in C#.


    Friday, July 18, 2014 5:50 AM
  • HI Saurabh

    RAnge.Text does not carry formatting. The code you're currently using is therefore dropping all formatting since you're essentially replacing the current content with "plain text".

    You need to APPEND new content to the cell, which is best done using the Range object. Something along these lines. Here's some VBA code that shows how it's done

        Dim newText As String
        Dim rng As word.Range
        
        newText = "some text"
        Set rng = ActiveDocument.Tables(1).Cell(1, 1).Range
        rng.Collapse wdCollapseEnd
        rng.MoveEnd wdCharacter, -1
        rng.Text = newText
    
    You should be able to "translate" this to C# without any great problems. The Collapse and MoveEnd methods need to use the ref keyword for parameters and you'll need to define the parameters as objects prior to using them in the methods. Word.WdCollapseDirection.wdCollapseEnd, Word.WdUnits.wdCharacter are the Enum values you need.

    Cindy Meister, VSTO/Word MVP, my blog

    Friday, July 18, 2014 4:30 PM
    Moderator