Finding and converting text to hyperlink with VBA

J

Jon

Greetings from sunny Sweden,

I have a macro that converts a SGML-style (custom formatted) plain text file
to a word document with proper formatting (headers, styles etc.) but I cannot
figure out how to convert the links. How can I use the macro to locate
"tagged" (hyper-)links and convert them to working hyperlinks?

This is how it looks like:

---
Some text here. Some more text.
Click here to view the PDF: <LINK>FileName.PDF</LINK>
Even more text.
---

I want the "tags" to be removed and the text FileName.PDF to remain, but as
a functional (clickable) link to the PDF file. I know that there is a
search-and-replace method that can extract substrings from strings (have used
it for style control) but can't make it happen with the link creation.

Any coding hints would be appreciated.

Thanks in advance!
Jon
 
D

Doug Robbins - Word MVP

The following will convert such tagged items to hyperlinks:

Dim lrange As Range, lbeg As Range, lend As Range

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="\<LINK\>[A-z.]{1,}\<\/LINK\>",
MatchWildcards:=True, Forward:=True, Wrap:=wdFindStop) = True
Set lrange = Selection.Range
Set lbeg = Selection.Range.Duplicate
Set lend = Selection.Range.Duplicate
lrange.Start = lrange.Start + 6
lrange.End = lrange.End - 7
lbeg.End = lbeg.Start + 6
lbeg.Delete
lend.Start = lend.End - 7
lend.Delete
ActiveDocument.Hyperlinks.Add Anchor:=lrange, Address:=lrange.Text,
TextToDisplay:=lrange.Text
Loop
End With

Whether following them will work however will depend upon whether
FileName.PDF, contains the drive and path as well as the filename

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

Jon

Doug,

Thanks a bunch! It works like a charm now.

As a relative newbie to VBA, it's easy to get tangled in "unnecessary code"
- which I did trying on my own. :)

Cheers!
Jon

Doug Robbins - Word MVP said:
The following will convert such tagged items to hyperlinks:

Dim lrange As Range, lbeg As Range, lend As Range

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="\<LINK\>[A-z.]{1,}\<\/LINK\>",
MatchWildcards:=True, Forward:=True, Wrap:=wdFindStop) = True
Set lrange = Selection.Range
Set lbeg = Selection.Range.Duplicate
Set lend = Selection.Range.Duplicate
lrange.Start = lrange.Start + 6
lrange.End = lrange.End - 7
lbeg.End = lbeg.Start + 6
lbeg.Delete
lend.Start = lend.End - 7
lend.Delete
ActiveDocument.Hyperlinks.Add Anchor:=lrange, Address:=lrange.Text,
TextToDisplay:=lrange.Text
Loop
End With

Whether following them will work however will depend upon whether
FileName.PDF, contains the drive and path as well as the filename

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Jon said:
Greetings from sunny Sweden,

I have a macro that converts a SGML-style (custom formatted) plain text
file
to a word document with proper formatting (headers, styles etc.) but I
cannot
figure out how to convert the links. How can I use the macro to locate
"tagged" (hyper-)links and convert them to working hyperlinks?

This is how it looks like:

---
Some text here. Some more text.
Click here to view the PDF: <LINK>FileName.PDF</LINK>
Even more text.
---

I want the "tags" to be removed and the text FileName.PDF to remain, but
as
a functional (clickable) link to the PDF file. I know that there is a
search-and-replace method that can extract substrings from strings (have
used
it for style control) but can't make it happen with the link creation.

Any coding hints would be appreciated.

Thanks in advance!
Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top