![]() |
#4
|
||||
|
||||
![]()
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
a href, hyperlink, macro |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
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 |