none
Link to bookmark in document converted from HTML not working RRS feed

  • Question

  • I have created an HTML document with a summary page. Hyperlinks on the summary page lead to different parts of the document.

    I want to import it to Word. I have tried two methods:
    1. Copy the document to the clipboard and paste it into a Range of a newly created Word document
    2. Save the HTML doument to file then use InsertFile method of a Ducument.Range in the new Word  document.

    In both cases the import and conversion works except for the case of the bookmarks. The hyperlinks are there in the summary page and they behave as you would expect - the colour and the hand cursor. However, when you click them nothing happens. This makes me think the problem is in the bookmark.

    The HTML for the link is:
    <td><a href = "#Link1">1</a></td>

    The HTML for the bookmark is:
    <P style="page-break-before: always; text-align: center" id="Link1">****** Message 1 ******</P>

    Can anyone spot anything wrong or suggest a work-around.

    Thanks


    Mike VE

    Monday, February 10, 2020 3:26 PM

Answers

  • As far as I know, the basic problem is that Word does not understand everything in HTML 5 and does not recognise the id=in the <p> element as being a link target. (I believe the idea that you could give any element an id= and use that id in an <a> href came in HTML 5, but not completely sure.

    What Word does recognise is the old name= attribute in an <a> element as a target.

    So if you use

    <a href="#Link1"1</a>

    then

    <P><a name="Link1"></a>***** Message 1 *****</p>

    I think Word will create the hyperlink you need.

    Word will actually only link *internally* to a Heading style paragraph or a Word bookmark, and you should see that the above construct will create a bookmark called Link1.

    [Incidentally, Word more naturally uses a REF field with a bookmark as the target for *internal* links, but in order to get Word to construct one of those from HTML you would have to mess around with the idiosyncratic dialect of HTML that Word uses to "round-trip" it own features.]

    Peter Jamieson


    • Edited by Peter Jamieson Monday, February 10, 2020 5:17 PM
    • Marked as answer by Mike VE Thursday, February 13, 2020 9:38 AM
    Monday, February 10, 2020 5:13 PM

All replies

  • I have just noticed that when I hover above the first link in the completed Word document the text "about:blank#Link1".

    It seems that "about:blank" is the URL property of the WebBrowser control where the HTML was assembled but it is not contained in the WebBrowers.DocumentText that I copy to the clipboard and paste into Word.

    It that the source of the problem and how do I get rid of it?


    Mike VE

    Monday, February 10, 2020 4:58 PM
  • As far as I know, the basic problem is that Word does not understand everything in HTML 5 and does not recognise the id=in the <p> element as being a link target. (I believe the idea that you could give any element an id= and use that id in an <a> href came in HTML 5, but not completely sure.

    What Word does recognise is the old name= attribute in an <a> element as a target.

    So if you use

    <a href="#Link1"1</a>

    then

    <P><a name="Link1"></a>***** Message 1 *****</p>

    I think Word will create the hyperlink you need.

    Word will actually only link *internally* to a Heading style paragraph or a Word bookmark, and you should see that the above construct will create a bookmark called Link1.

    [Incidentally, Word more naturally uses a REF field with a bookmark as the target for *internal* links, but in order to get Word to construct one of those from HTML you would have to mess around with the idiosyncratic dialect of HTML that Word uses to "round-trip" it own features.]

    Peter Jamieson


    • Edited by Peter Jamieson Monday, February 10, 2020 5:17 PM
    • Marked as answer by Mike VE Thursday, February 13, 2020 9:38 AM
    Monday, February 10, 2020 5:13 PM
  • Thanks for the suggestion, Peter.

    My link now looks like this:
    <a href = "#Link1">1</a>

    And the bookmark looks like this:
    <P style="page-break-before: always; text-align: center"><a id="Link1">****** Message 1 ******</a></P>

    However, the behaviour is the same. The links look like links but the tooltip reads "about:blank#Link1".
    The bookmark does not appear in the list of existing bookmarks when I open the Bookmark dialog box. I have tried removing the stars and space from the Link1 text - "Message_1" but it made no difference. 


    Mike VE

    Tuesday, February 11, 2020 10:00 AM
  • It needs to be

    <a name=“Link1”>

    rather than

    <a Id=“Link1”>

    and that may be a problem elsewhere because the “name” attribute is either deprecated or not valid in HTML5.


    Peter Jamieson

    Tuesday, February 11, 2020 10:45 AM
  • Thanks Peter.

    That's got the bookmarks working, i.e. Word's bookmark dialog recognised them, lists them, and can go to them.

    So now the remaining problem is that the links don't do anything although they look right. I suspect it is something to do with "about:blank#Link1" appearing in the tooltip/link. It's not there in the HTML.


    Mike VE

    Tuesday, February 11, 2020 11:57 AM
  • I don't know where the about:blank is coming from, but

     a. the <a href=> element translates into a { HYPERLINK } field in Word - in this case you should be able to use Alt-F9 to toggle field code/results display and see something like

    { HYPERLINK "" \l "Link1" }

    With just a one-character link ("1") in your example, it can be a bit hard to follow the link. Here, I need to do ctrl-click but that is settings-dependent.

    To specify a tooltip, normally you would add a \o parameter, e.g.

    { HYPERLINK "" \l "Link1" \o "about:blank" }

    Otherwise, Word generates the tooltip "on the fly" - for example, here it looks like

    file:///<some path>test.htm#Link1

    or 

    file:///<some path>test.docx#Link1

    depending on whether I just opened the HTML file or saved it as a .docx.

    This is in the Windows Desktop version of Word 365 (2019).

    Although the about:blank seems familiar it doesn't actually ring the necessary bell for me. My best guesses are:

     a. you're using a different version of Word (if so, which? - incidentally, Mac desktop Word 365/2019 sensibly just displays the bookmark name in the tooltip)

     b. some other non-obvious thing in the HTML is causing Word to add an \o parameter

    Can you post a copy of a sample that shows the problem somewhere accessible (OneDrive/Dropbox etc.), along with the HTML used to generate it?


    Peter Jamieson

    Tuesday, February 11, 2020 2:01 PM
  • It's now working properly. The key things were the use of name= rather than id= and avoiding pasting straight from the webcontrol where I had previewed the document. That was the origin of "about:blank". I changed tack and saved the newly constructed HTML doc to file then used Doc.Range.InsertFile into a new word document. 

    Thanks very much for your help Peter.


    Mike VE

    Thursday, February 13, 2020 9:37 AM