Results 1 to 5 of 5
  1. #1
    2 Star Lounger
    Join Date
    Jan 2004
    Location
    Mount Prospect, Illinois
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hyperlink without Style? (2000, 2002, 2003, 2007?)

    Is it possible to create hyperlinks (preferably in VBA) without applying the character style? If there are already character styles on the text, you lose that formatting. I'd be much happier applying the darn blue underline myself, if I can prevent other character styles from getting trashed.

    I've built a clever routine which can slip the tablecloth of the character style out from other character formatting without having to Clear Character Formatting (ctrl-spacebar), but I can't restore lost character styles. (copy the text, paste it into a hidden document and delete the style, copy it back and paste it where it was)

    Word's being a bully again. Any suggestions for putting it in its place this time?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Hyperlink without Style? (2000, 2002, 2003, 2007?)

    You could modify the Hyperlink style not to be blue and underlined, but that would remove all color from the hyperlinked text. I think you'll have to apply the color after creating the hyperlink instead of the other way round.

  3. #3
    2 Star Lounger
    Join Date
    Jan 2004
    Location
    Mount Prospect, Illinois
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Hyperlink without Style? (2000, 2002, 2003, 2007?)

    It took a bit of hacking, and I'm not certain this is goof-proof, but here's a solution to creating links without the Hyperlink Style:

    <pre>Public Sub InsertCleanLink(oBkmk As Bookmark)
    Dim r As Range
    Dim rstart As Long
    ' save the selection and where the original text started
    Set r = Selection.Range.Duplicate
    rstart = Selection.Start
    ' Start with no text selected, and add a new hyperlink
    Selection.Collapse wdCollapseStart
    Selection.Hyperlinks.Add Selection.Range, "", oBkmk.Name, , "###"
    ' Annoyingly, the above line not only moves the selection,
    ' but also expanded the r Range set above.
    ' So now, select the recently inserted text, and trim the r range
    ' back to the original text
    r.Start = Selection.End
    Selection.Start = rstart
    ' Copy the original text into the hyperlink -- this is the sneaky part
    Selection.Fields(1).Result.FormattedText = r.FormattedText
    ' Then get rid of the original text
    r.Delete
    Set r = Nothing
    End Sub
    </pre>

    There needs to be some error checking -- if the original selection spans cells, it probably can't create the link, and certainly can't use the FormattedText copy to move the text. For my purposes, though, this will probably do the job.

    I'd appreciate critiques on the code.

  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: Hyperlink without Style? (2000, 2002, 2003, 20

    <P ID="edit" class=small>(Edited by jscher2000 on 29-Jan-07 12:48. )</P>I'm probably missing something, but why not store the character style and re-apply it?

    <code>Sub HyperlinkSelectionReapplyingStyle()
    ' Create hyperlink from selection but preserve character style
    If Selection.Type <> wdSelectionNormal Then Exit Sub
    Dim strStyle As String, blnCharStyle As Boolean, hl As Word.Hyperlink
    With Selection
    strStyle = .Style
    blnCharStyle = (.Style.Type = wdStyleTypeCharacter)
    Set hl = .Hyperlinks.Add(.Range, , ActiveDocument.Bookmarks("Page2"))
    If blnCharStyle Then
    With hl.Range
    ' Re-apply prior character style
    .Style = strStyle
    ' Match hyperlink appearance (could be expanded...)
    .Font.Underline = ActiveDocument.Styles("Hyperlink").Font.Underline
    .Font.Color = ActiveDocument.Styles("Hyperlink").Font.Color
    End With
    Else
    ' Default behavior, or clear it with:
    ' hl.Range.Font.Reset
    End If
    End With
    Set hl = Nothing
    End Sub</code>

    (Change my hardcoded bookmark name Page2 to fit your test scenario...)

    Added: I see your original post refers to character styles (plural), which my code does not handle correctly. Never mind!

  5. #5
    2 Star Lounger
    Join Date
    Jan 2004
    Location
    Mount Prospect, Illinois
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Hyperlink without Style? (2000, 2002, 2003, 20

    > I'm probably missing something, but why not store the character style and re-apply it?
    Because each character could have a different style or no style.
    I'm mainly looking for the generalized case where I can choose whether to slather a Hyperlink style atop the text.

Posting Permissions

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