Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #4  
Old 06-26-2011, 07:36 PM
macropod's Avatar
macropod macropod is offline Convert html links into embedded hyperlinks Windows 7 64bit Convert html links into embedded hyperlinks Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi njcloud,

Try the following:
Code:
Sub ConvertHyperlinks()
Dim RngFld As Range, RngTmp As Range, oFld As Field, StrTmp As String, HLink As Hyperlink
' Turn Off Screen Updating
Application.ScreenUpdating = False
ActiveDocument.ActiveWindow.View.ShowFieldCodes = True
Set RngFld = ActiveDocument.Range
With RngFld
  'Convert HREF codes to {HYPERLINK \1}«\2» format
  With .Find
    .Text = "\<[Aa] href=([!\>]{1,})\>([!\<]{1,})\</a\>"
    .ClearFormatting
    .Replacement.ClearFormatting
    .Replacement.Text = "{HYPERLINK \1}«\2»"
    .Forward = True
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
  End With
  'Validate content
  If Len(Replace(.Text, "{", vbNullString)) <> Len(Replace(.Text, "}", vbNullString)) Then
    MsgBox "Unmatched field brace pairs in the document.", vbCritical + vbOKOnly, "Error!"
    Exit Sub
  End If
  If Len(Replace(.Text, "«", vbNullString)) <> Len(Replace(.Text, "»", vbNullString)) Then
    MsgBox "Unmatched 'Text to display' tags in the document.", vbCritical + vbOKOnly, "Error!"
    Exit Sub
  End If
  'Convert Hyperlink fields
  Do While InStr(1, .Text, "{") > 0
    Set RngTmp = ActiveDocument.Range(Start:=.Start + _
        InStr(.Text, "{") - 1, End:=.Start + InStr(.Text, "}"))
    With RngTmp
      Do While Len(Replace(.Text, "{", vbNullString)) <> _
          Len(Replace(.Text, "}", vbNullString))
        .End = .End + 1
        If .Characters.Last.Text <> "}" Then .MoveEndUntil cset:="}", _
          Count:=Len(ActiveDocument.Range(.End, RngFld.End))
      Loop
      .Characters.First = vbNullString
      .Characters.Last = vbNullString
      StrTmp = .Text
      Set oFld = ActiveDocument.Fields.Add(Range:=RngTmp, _
          Type:=wdFieldEmpty, Text:="", PreserveFormatting:=False)
      oFld.Code.Text = StrTmp
    End With
  Loop
  ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
  'update the field display
  .Fields.Update
  'Update the hyperlink 'Text To Display' values
  For Each HLink In .Hyperlinks
    Set RngTmp = HLink.Range
    With RngTmp
      If .Characters.Last.Next = "«" Then
        .MoveEndUntil cset:="»"
        .MoveStartUntil cset:="«"
        .Characters.First.Delete
        .Characters.Last.Next.Delete
        HLink.TextToDisplay = Trim(.Text)
        .Delete
      End If
    End With
  Next
End With
Set RngTmp = Nothing: Set RngFld = Nothing: Set oFld = Nothing
' Restore Screen Updating
Application.ScreenUpdating = True
End Sub
Note: The code does all the Find/Replace work and conversion in one go. As part of the Find/Replace work, the '{ }' pairs are created for the field code, along with some temporary '« »' pairs for the 'Text To Display'. If you use any of these characters elsewhere in the document, some different (unused) characters will be needed.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
 

Tags
a href, hyperlink, macro



Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert html links into embedded hyperlinks Embedded links spheon Word 1 06-07-2011 04:11 PM
convert html to text at opening etfjr Word 0 12-13-2010 11:14 AM
Can you actually write HTML and CSS in a word document and send it as an html page jackaroo Word 0 07-12-2010 07:49 AM
Word with frames, table of contents, and hyperlinks to html NHMC Word 0 12-09-2009 12:54 PM
Convert a file from HTML to WORD format weblayout view gtselvam Word 0 12-02-2008 03:53 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:37 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft