none
Bookmark to text in a Word table cell and a question about HTML

    Question

  • Hi,

    With my first small VSTO project written in C# I got this little problem on creating bookmarks to text within cells.
    Instead of linking to the plain text, the bookmarks links to the whole cell if I use the following code:

    Word.Cell cellRequirementId= requirementsTable.Cell(currentRow, 1); 
    cellRequirementId.Range.Text = requirementId; 
     
    object bookmarkRangeObject = requirementId.Range.Duplicate; 
    Bookmarks.Add("Requirement_ID_" + requirementId, ref bookmarkRangeObject); 

    For coding purposes linking to the whole cell isn't a big problem, but I want my end users to be able to use cross-references on the bookmarked requirement-id's. Now if you make a cross reference to the a bookmarked requirement-id the whole cell shows up, that's not what I want, I want only the plain text.
    I figured out that be enable to convert a specific text to a range would solve my problem, so I searched through the MSDN libary but I couldn't find a solution.
    So I have anyone over here can help me, that would be great.
    I do have an other question; when I'm importing HTML from a database, is there any function that will automatically convert the HTML to formatted Word tekst (eg. "<b>Text</b>" to bold text)?

    Thanks.
    Thursday, October 30, 2008 2:05 PM

Answers

  • Hi Michael

    What you need to do is to shorten the range by one or two characters. Up until Word 2007 it was definitely 2 characters; someone changed something in 2007 so that it's sometimes one character. Best is to check after shortening by one whether the last character is ANSI 13 (hidden end-of-paragraph). If it is, shorten once more.

    Use the Range.MoveEnd method with Word.WdUnits.wdCharacter and -1 as the parameters.

    Please note that the effect of doing this will make the bookmarks volatile. If the user TABS into a cell then starts typing, thus replacing the current cell content, the bookmark will be deleted.

    HTML: no, the Word object model has nothing built-in that will "transform" HTML coming in as text to formatted Word content. Word can only do this when a converter kicks in. The converter is used when inserting from a file or pasting from the Clipboard. Some people do stream the HTML to the Clipboard, then use the Paste method. If you're interested you should find some discussions with sample code here in the forum.
    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Michaeldv Monday, November 03, 2008 10:30 AM
    • Marked as answer by Michaeldv Monday, November 03, 2008 10:30 AM
    Friday, October 31, 2008 8:37 AM
    Moderator

All replies

  • Hi Michael

    What you need to do is to shorten the range by one or two characters. Up until Word 2007 it was definitely 2 characters; someone changed something in 2007 so that it's sometimes one character. Best is to check after shortening by one whether the last character is ANSI 13 (hidden end-of-paragraph). If it is, shorten once more.

    Use the Range.MoveEnd method with Word.WdUnits.wdCharacter and -1 as the parameters.

    Please note that the effect of doing this will make the bookmarks volatile. If the user TABS into a cell then starts typing, thus replacing the current cell content, the bookmark will be deleted.

    HTML: no, the Word object model has nothing built-in that will "transform" HTML coming in as text to formatted Word content. Word can only do this when a converter kicks in. The converter is used when inserting from a file or pasting from the Clipboard. Some people do stream the HTML to the Clipboard, then use the Paste method. If you're interested you should find some discussions with sample code here in the forum.
    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Michaeldv Monday, November 03, 2008 10:30 AM
    • Marked as answer by Michaeldv Monday, November 03, 2008 10:30 AM
    Friday, October 31, 2008 8:37 AM
    Moderator
  • Thanks for the reply Cindy,
    Shorting the range by one character worked.
    For the HTML issue, I tried coping the HTML in the clipboard with 'Clipboard.SetText' and pasted it with 'Range.PasteSpecial' with parameter type as 'wdPasteHTML', like some topics on this forum described, but that didn't work for me. I get an exception which says the clipboard type ain't right.
    But I'll look into that a little further, thanks again!
    • Edited by Michaeldv Monday, November 03, 2008 10:38 AM
    Monday, November 03, 2008 10:37 AM