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

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Hyperlink without Style? (2000, 2002, 2003, 2007?)

    Author
    Topic
    #439196

    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?

    Viewing 1 reply thread
    Author
    Replies
    • #1048947

      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.

    • #1048962

      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:

      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
      

      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.

      • #1049000

        (Edited by jscher2000 on 29-Jan-07 12:48. )

        I’m probably missing something, but why not store the character style and re-apply it?

        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

        (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!

        • #1049021

          > 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.

    Viewing 1 reply thread
    Reply To: Hyperlink without Style? (2000, 2002, 2003, 2007?)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: