Jaa


Hyperlink Fixing in Microsoft Word with .NET

Here's the deal: you have a load of Word docs with hyperlinks. Some of the docs are old. You need one of them, so you open it up, make a couple of updates and get ride to fire it out to the entire company. Before you send it around to 10,000 people, you want to make sure that the 35 hyperlinks in it are all still working. You could click on each link (tedious) and check them all out. Wouldn't it be great to just get a quick report on which ones didn't work right? Yeah. It would.

Fortunately, it's a cakewalk. I whipped up a VSTO, but the code can be easily ported to VBA. Assume I have form that displays two listboxes, one for good links, one for bad links. This code loops through the hyperlinks, tests them, and adds them to the appropriate list box on the form. There's also a textbox that shows the link count:

Dim f As New Form1

Dim links As Word.Hyperlinks

Dim link As Word.Hyperlink

links = ThisApplication.ActiveDocument.Hyperlinks

f.LinkCount = links.Count

Dim webRequest As System.Net.HttpWebRequest

Dim webResponse As System.Net.HttpWebResponse

For Each link In links

webRequest = _

CType(System.Net.HttpWebRequest.Create(link.Address), _

System.Net.HttpWebRequest)

webRequest.Timeout = 1000

Try

webResponse = _

CType(webRequest.GetResponse(), _

System.Net.HttpWebResponse)

f.AddGoodLink(link.Address)

Catch ex As Net.WebException

f.AddBadLink(link.Address)

End Try

Next

f.Show()

I reduced the timeout so that docs with loads of links will not take too long.

Rock Thought for the Day: I bought tickets for Billy Corgan's show at Seattle's Moore Theater- all part of his first solo tour, TheFutureEmbrace. I have not hidden my enthusiasm for Billy Corgan's music on this blog, and with good reason. His new album is unique, yet so familiar. I'll do justice to a more thorough review this week.

Rock On

Comments

  • Anonymous
    June 22, 2005
    hey John, don't know if you've heard, but apparently Billy has written on his website that he intends to put the Pumpkins back together. Maybe its hype to sell out his shows - who knows. But at least its not just a rumor being spread by unknown sources.
  • Anonymous
    June 29, 2005
    Not really related to this post, but I'd be interested in your views.
    I've recently started using the Office 2003 PIAs in my apps. One thing which I've found a PI(T)A is that I have to be very careful about calling Marshal.ReleaseComObject on all references created explicitly and implicitly (http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q317109).

    I looked at the Office 2003 PIA Sample Launcher and the code it generates doesn't seem to do this. Can you comment on why this is?