none
WdInformation.wdWithInTable not set for last cell RRS feed

  • Question

  • hi All,

    So using Word 2010, C#, Visual Studio 2010.

    I've written a Word AddIn to parse all of the sentences in a Word Document, but I want to ignore the sentences that are within a Table.

    But the sentence in the last cell of the Table (no matter the table size) always seems to be processed, I.E. it's WdInformation.wdWithInTable bit is not set.

    Mytest code is:

            private void PushMe_Click(object sender, RibbonControlEventArgs e)
            {
                foreach (Microsoft.Office.Interop.Word.Range _sentence in Globals.ThisAddIn.Application.ActiveDocument.Sentences)
                {
                    if (_sentence.Information[WdInformation.wdWithInTable] == false)
                        _sentence.Font.Color = WdColor.wdColorRed;
                }
            }

    And the result is that the sentence in the last cell of the Table has its color changed also. 

    I've posted a zip file containing my test AddIn VS project and test file on my SkyDrive and made it publically available for reading.

    Can you access it directly, or do I need to send you an email with a link??

    Thanks, billwa992

    Document Content before pushing button====================================================

    This is the beginning of the document for testing table scanning (non-scanning actually).

    This is the beginning of the document for testing table scanning (non-scanning actually).

     

    This is the beginning of the document for testing table scanning (non-scanning actually).

     

    This is the beginning of the document for testing table scanning (non-scanning actually).

     

     

    This is the beginning of the document for testing table scanning (non-scanning actually).

    Document Content after pushing button====================================================

    This is the beginning of the document for testing table scanning (non-scanning actually).

    This is the beginning of the document for testing table scanning (non-scanning actually).

     

    This is the beginning of the document for testing table scanning (non-scanning actually).

     

    This is the beginning of the document for testing table scanning (non-scanning actually).

     

     

    This is the beginning of the document for testing table scanning (non-scanning actually).

    Friday, May 13, 2011 7:01 AM

Answers

  • Sometimes I despair!
     
    How one enumerates the sentences makes no difference to what happens, although it will make a difference to how quickly it happens - in a long document, your version will be significantly slower.
     
    What happens (and also happened prior to Word 2003) depends on whether or not the table is immediately followed by a paragraph mark.
     

    Enjoy,
    Tony
    www.WordArticles.com
    • Marked as answer by billwa992 Monday, May 23, 2011 8:10 PM
    Monday, May 23, 2011 8:47 AM

All replies

  • Hi,

     

    I have tested your code. The same issue also occurred in my side. I used the follow code find that every table identified as one sentence. But it still can’t explain why the issue happens. I’m doing some further research to check if it is a bug in Word.

     

    Additionally, if you change Sentences to Words, everything will be ok:

      foreach (Word.Range range in doc.Words)

     

    I hope this helps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 16, 2011 9:31 AM
    Moderator
  • Hi,

     

    Have you got the problem solved? I have tested how the sentences property works in C# using the following code:

                foreach (Word.Range rangeSen in doc.Sentences)

                {

                    rangeSen.Select();

                   

                    if (app.Selection.Information[Word.WdInformation.wdWithInTable] == false)

                    {

                       

                        rangeSen.Font.ColorIndex = Word.WdColorIndex.wdRed;

                    }

                }

     

    The last range the application select is not the last cell but the last row in the table and a row under the table. Please check the illustration:

     

    I don’t know why this happens. However, if you iterate over the words in the document, it works fine. You can also using screenUpdating property to speed up the calculation.

     

    Check the code below:

                bool OldUpdatingValue = app.ScreenUpdating;

                app.ScreenUpdating = false;

                foreach (Word.Range range in doc.Words)

                {  

                    if (range.Information[Word.WdInformation.wdWithInTable] == false)

                    {

                        range.Font.ColorIndex = Word.WdColorIndex.wdRed;

                    }

                }

                app.ScreenUpdating = OldUpdatingValue;

     

     I hope this helps


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Thursday, May 19, 2011 8:33 AM
    Moderator
  • Thanks for the update Calvin,

    But no, that does not solve my problem without redesigning my AddIn.  I'm parsing thru "sentences" from the "Sentences" collection, not "words", within my AddIn.

    So I'd have to "hack" around the bug by switching to a word based scan until I'm out of a table, this just isn't a very pretty solution.

    billwa992

    Friday, May 20, 2011 10:17 PM
  • I can duplicate this, and it does seem to be a trifle odd. Instead of checking the Information value, you could try checking the Tables.Count instead - if the sentence is in a table, it will be non-zero.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Saturday, May 21, 2011 11:29 AM
  • Hi Bill,

     

    Sorry for the late response.

     

    It’s known issue from Office 2003. Here is a workaround to access the sentences via an index rather than an enumerator:

              Word.Range rangeSen;

                int sentencesCount = doc.Sentences.Count;

                for (int i = 1; i <= sentencesCount; i++)

                {

                    rangeSen = doc.Sentences[i];

                    if (rangeSen.Information[Word.WdInformation.wdWithInTable] == false)               

                        rangeSen.Font.ColorIndex = Word.WdColorIndex.wdRed;                             

                }

     

    The solution works fine on my side.

     

    I hope this helps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 23, 2011 8:25 AM
    Moderator
  • Sometimes I despair!
     
    How one enumerates the sentences makes no difference to what happens, although it will make a difference to how quickly it happens - in a long document, your version will be significantly slower.
     
    What happens (and also happened prior to Word 2003) depends on whether or not the table is immediately followed by a paragraph mark.
     

    Enjoy,
    Tony
    www.WordArticles.com
    • Marked as answer by billwa992 Monday, May 23, 2011 8:10 PM
    Monday, May 23, 2011 8:47 AM
  • Tony,

    Thanks for the hint.  That "Tables.Count" check works just great - as a work around for the bug.

    billwa992

    Monday, May 23, 2011 8:10 PM