Visual Studio Developer Center > Visual Studio Forums > Visual Studio Tools for Office > Word Bug? Characters being appended to strings
Ask a questionAsk a question
 

AnswerWord Bug? Characters being appended to strings

  • Friday, November 06, 2009 5:36 PMmark010101 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hello,
    i am seeing some very odd string behaviors in Word 2007 when trying to read strings in code.  I have a table that contains the string "Test" in a cell(1,1). 

    in code, the following If statement is never true even though in debug mode i see that str does contain the value "test":

    Dim str as String
    ...
    str = table.cell(1,1).Range.Text
    
    if str = "Test" then
        'never gets here even though str contains "Test"
    end if
    Now, when i print the ascii characters out, i see why.  In one case, the end character was a Chr 7 (even though it did not display as the number '7', but instead as a square dot), in another it was chr 13.  I looked at the cell carefully and see that there is NOT character at the end of the text in that cell?  I deleted the cell and added back just the string "Test".  Same problem however.

    Why is this occurring?   This also is happening when i iterate through the BuildingBlocks collection and and read the category name from an item.

       For i = 1 To CoverPagesBBTemplate.BuildingBlockEntries.Count()
            Set bb = CoverPagesBBTemplate.BuildingBlockEntries.Item(i)
                
            If bb.Category.Name = "Test" Then
                bb.Delete 'never gets here.
            End If
        Next
    In the above case, bb.Category.Name shows that there is char 10 at the end, thus the comparison fails. Looking at the actual entry in the organizer, i see no such character. Removing that entry and re-adding it, taking care to not add any carriage return or line feed, still shows the same problem.

    These seems to be a bug, but i haven't found much about it.
        

Answers

  • Friday, November 06, 2009 8:56 PMCindy MeisterMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Every Word table cell contains two characters, by default: ANSI 13 (carriage return) and ANSI 7 (which Word interprets as "end-of-cell"). Both of these are contained visually in the end-of-cell marker that can be seen if the display of non-printing characters is active. Word uses these characters to recognize and manage the table structures; I can't speak to why the original programmers chose to do it this way, a couple of decades ago.

    Word has always had this, and hopefully always will (otherwise it will break a lot of existing code). It's definitely not a bug.

    About the character at the end of a building block category name I was not aware. I have no idea whether it's supposed to be there, or not. You might enquire about this in a venue that specializes in the Word object model. You'll find some links in the forum's Please Read First message.
    Cindy Meister, VSTO/Word MVP

All Replies

  • Friday, November 06, 2009 8:56 PMCindy MeisterMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Every Word table cell contains two characters, by default: ANSI 13 (carriage return) and ANSI 7 (which Word interprets as "end-of-cell"). Both of these are contained visually in the end-of-cell marker that can be seen if the display of non-printing characters is active. Word uses these characters to recognize and manage the table structures; I can't speak to why the original programmers chose to do it this way, a couple of decades ago.

    Word has always had this, and hopefully always will (otherwise it will break a lot of existing code). It's definitely not a bug.

    About the character at the end of a building block category name I was not aware. I have no idea whether it's supposed to be there, or not. You might enquire about this in a venue that specializes in the Word object model. You'll find some links in the forum's Please Read First message.
    Cindy Meister, VSTO/Word MVP
  • Monday, November 09, 2009 8:36 PMmark010101 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Really? i find that amazing...where is this documented?  it would be nice if there was some sort of 'clean' api provided that would handle all these 'identification' util characters.
  • Tuesday, November 10, 2009 9:40 AMCindy MeisterMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Best I can do for you is this section of the discussion on the Word binary file format.
    Cindy Meister, VSTO/Word MVP