none
activedocument.table + C# RRS feed

  • Question

  • Hi ,

    I am working on merging doc Word 2007 using Visual Studio 2010 and C#. I get some information from another source, create a new Word document, and paste this information in the newly created document. Now I want to format the pasted conted and namely to remove/delete rows who contains an empty cells  from tables, that were pasted.

    Any help would be appreciated.

    Wednesday, May 8, 2013 7:54 AM

Answers

  • Hi Frank

    You'll probably need to loop each row (foreach (Word.Row r in Table.Rows)), then each cell in the row (foreach(Word.Cell c in r.Cells)) and test the c.Range.Text.Length.

    Depending on the version of Word, an empty cell will return either 2 or 1. An empty cell contains the characters ANSI 13 (\r in C#-speak) - a Word carriage return that also carries paragraph-level formatting, and ANSI 7 - the Word end-of-cell marker that also carries cell layout properties.

    Some versions of Word return both, others return only a single character, so you'll need to test what the version of Word you're programming against returns. It's probably also a good idea to test the ANSI values if two characters are being returned.

    Once you've determined what you can use to test whether a cell is empty, if one is, delete the row (r.Delete) and "break;" to the next loop for the rows.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, May 8, 2013 10:20 AM
    Moderator

All replies

  • Hi Frank

    You'll probably need to loop each row (foreach (Word.Row r in Table.Rows)), then each cell in the row (foreach(Word.Cell c in r.Cells)) and test the c.Range.Text.Length.

    Depending on the version of Word, an empty cell will return either 2 or 1. An empty cell contains the characters ANSI 13 (\r in C#-speak) - a Word carriage return that also carries paragraph-level formatting, and ANSI 7 - the Word end-of-cell marker that also carries cell layout properties.

    Some versions of Word return both, others return only a single character, so you'll need to test what the version of Word you're programming against returns. It's probably also a good idea to test the ANSI values if two characters are being returned.

    Once you've determined what you can use to test whether a cell is empty, if one is, delete the row (r.Delete) and "break;" to the next loop for the rows.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, May 8, 2013 10:20 AM
    Moderator
  • thx Cindy for ur answer i will try ur solution
    Wednesday, May 8, 2013 4:32 PM
  • Hello,

    Since you haven't responded for a few days, I temporarily mark Cindy's reply as answer. If you disagree with it, please unmark it and post your doubt. Thanks for your understanding.

    Regards,


    Damon Zheng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 14, 2013 11:59 AM
    Moderator