Results 1 to 11 of 11
  1. #1
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    UK
    Posts
    239
    Thanks
    4
    Thanked 1 Time in 1 Post

    Replace with bookmark (Word XP)

    <P ID="edit" class=small>(Edited by jscher2000 on 28-May-06 11:06. Importable module attached for Loungers who want to play with or read the code in a more indented format.)</P>I need to work on documents produced by an application known as RoboHelp. It generates a document with the same content as the online help it generates. For some reason known only to the developers, hyperlinks in the source html file are generated to look like links in the Word document but they do not function as links.

    The macro below has been posted on the RH forum and it is designed to find the "links" and make them work provided the string of the link is the same as the string of the heading.

    ****************************************

    Sub theOne()
    Dim myHeadings() As String

    'Dim myPageNumbers() As String

    Dim i As Integer

    'store all heading information
    myHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHea ding)

    'move the cursor to section 3
    Selection.HomeKey Unit:=wdStory
    Selection.Move wdSection, 2

    'search for text with hyperlink style
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Hyperlink")

    With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False

    .Execute FindText:="", Format:=True
    While .Found = True
    'loop around refs to find the right one and update link
    For i = 1 To UBound(myHeadings)
    If Selection = Trim(myHeadings(i)) Then
    'update the style with a real link and page info
    Selection.Delete
    Selection.InsertCrossReference ReferenceType:="Heading", ReferenceKind:= _
    wdContentText, ReferenceItem:=CStr(i), InsertAsHyperlink:=True, _
    IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
    Selection.TypeText Text:=" on page "
    Selection.InsertCrossReference ReferenceType:="Heading", ReferenceKind:= _
    wdPageNumber, ReferenceItem:=CStr(i), InsertAsHyperlink:=True, _
    IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "

    End If
    Next i
    .Execute
    Wend
    End With

    End Sub

    ****************************************
    I found that with the line
    IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
    it did not work but if I amended that to
    IncludePosition:=False
    it did work but with some spacing problems.

    Let's say the line to be worked on is
    This is a link to New Topic 2 and this is a link to New Topic 3.
    where New Topic n are formatted as hyperlinks.

    What I end up with is
    This is a link to New Topic 2 on page 3and this is a link toNew Topic 3 on page 5.
    where New Topic n is a working link. Note the absence of a space after the first page number and before the second link.

    I have tried various tweaks but my VBA knowledge is just not up to it. I wonder if someone can see the problem please?
    Attached Files Attached Files

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Replace with bookmark (Word XP)

    It works as originally written for me in Word 2003. I don't really like the way it changes the text from hyperlink style to a field code. I suppose there might be a switch that makes the field code appear to be a hyperlink... or perhaps it's just a matter of re-applying the hyperlink style. But maybe there is something unique about your documents that is breaking those additional parameters?

    If you decided that you did want to remove the SeparatorString argument, you could manually "type" a space after inserting your cross-reference field. Something along these lines should do:

    Selection.Collapse direction:=wdCollapseEnd
    Selection.TypeText Text:=" "

    But you lose the intelligence of the built-in function (which suppresses the extra separator before commas and periods, for example).
    Attached Images Attached Images

  3. #3
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    UK
    Posts
    239
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Replace with bookmark (Word XP)

    First many thanks for setting up a test and for tidying up the code.

    I wondered if the problem might be related to my document being generated by RoboHelp so I have created a new document in Word and I still hit the same problem. When I run the macro as written I get a compile error, Named Argument Not Found with Separate Numbers highlighted.

    I wonder if you could let me have the document you created to see if I get the problem with that, i.e. a document not created on this PC. If I did, then would that suggest the problem is a setting in Word?

    I agree it would be better if the link continued to look like a link but at the moment I am happy with anything that provides the functionality.

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Replace with bookmark (Word XP)

    I just discarded that test document. <img src=/S/sad.gif border=0 alt=sad width=15 height=15> I wonder, though... I tested in Word 2003. If a named argument is not recognized in XP, perhaps it was new in 2003? Check the help file (not the RoboHelp file!) and see whether it is supposed to exist in Word 2002.

  5. #5
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    UK
    Posts
    239
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Replace with bookmark (Word XP)

    That was it. I have another machine with Word 2003. Ran it on that and did not get the error but still got the spacing issues. It seems the macro works in same way on Word XP without those lines as it does with them in Word 2003.

    So I still have the spacing issues and as you pointed out, the links are fields codes so they do not look like links. My guess though is that is a completely different macro.

    Any observations welcome but many thanks for spotting the problem I asked about.

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Replace with bookmark (Word XP)

    I thought the spacing issues must be caused by the way the hyperlink style was applied (i.e., including the leading and/or trailing spaces). But because the hyperlinked text exactly matches the heading you are linking to -- or it would be skipped -- I think now that perhaps I was wrong and there is some other reason that Selection.Delete is deleting too much. If this option is ticked, try clearing the checkbox: Tools>Options..., Edit tab, "When selecting, automatically select entire word". If that doesn't help, can you post a sample snippet of "bad" links?

  7. #7
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    UK
    Posts
    239
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Replace with bookmark (Word XP)

    I tried the macro with the option turned off but it made no difference. I notice that you do not get this issue but I get it both with the RoboHelp generated document and anything I create outside RoboHelp.

    I have attached a screen shot of what I see before and after. The bracket at the beginning is something RoboHelp inserts, the closing bracket is the last thing in the document. I have not been able to identify its purpose in life or delete it.

    In a separate post, I will attach the document I am working from as that may help. I really appreciate your help on this.
    Attached Images Attached Images

  8. #8
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    UK
    Posts
    239
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Replace with bookmark (Word XP)

    Here is the document that I am working from.
    Attached Files Attached Files

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Replace with bookmark (Word XP)

    Sorry, I had the wrong option. It's "Smart Cut & Paste" that eliminates extra spaces when you delete text. Try this toward the top:

    <code>Dim blnSmartCutAndPaste As Boolean
    blnSmartCutAndPaste = Options.SmartCutPaste
    Options.SmartCutPaste = False</code>

    and this toward the end:

    <code>Options.SmartCutPaste = blnSmartCutAndPaste</code>

    (Not so "smart," is it. This is one of the first things I disable when configuring Word.)

  10. #10
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    UK
    Posts
    239
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Replace with bookmark (Word XP)

    That fixed it.

    This macro will be used by many technical authors around the world. You have no idea how useful it will be in fixing a deficiency in RoboHelp. On their behalf and for myself, very many thanks for helping me with this.

  11. #11
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    UK
    Posts
    239
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Replace with bookmark (Word XP)

    Post edited by <!profile=StuartR>StuartR<!/profile>.
    <!t><!/t> and <!t><!/t> tags added before and after hyperlink to make it active. See <!help=19>Help 19<!/help>.

    Jefferson

    I thought you might like to see the results of your work.

    http://www.grainge.org/pages/authoring/pri...tm#broken_links

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •