Link to home
Start Free TrialLog in
Avatar of lp3535
lp3535

asked on

Inserting Hyperlink to a file with an apostrophe in the filename (VBA)

I'm using VBA to insert a link to a file that has an apostrophe in it's filename, but when the link is inserted, it doesn't work because it chops the filename off after the apostrophe.

Any suggestions for how to handle this?

Thanks.
Avatar of Frosty555
Frosty555
Flag of Canada image

Ensure that you escape special characters in your URL using "percent" escape codes.

http://www.w3schools.com/tags/ref_urlencode.asp

The escape code for an apostrophe is %27

So if your url was:   http://somewebsite.com/joe's_dessert_recipe.doc

Instead put:    http://somewebsite.com/joe%27s_dessert_recipe.doc

Remember to escape spaces, doublequotes etc. properly. If you do not escape special characters in your URLs properly they are malformed URLs. Word and other web browsers *might* handle it properly, but it is not guaranteed.
Avatar of lp3535
lp3535

ASKER

Thanks Frosty,

That worked to insert the hyperlink, and it all looks correct, except when I follow the hyperlink, it cannot open the file, and the file path has the %27 instead of the apostrophe.  

I've used the %20 for the spaces, which all works fine, but it doesn't work for the apostrophe.

Any thoughts?
Avatar of lp3535

ASKER

Sorry, I should add that the link to a word doc is being inserted into an email, and it's always worked, except now that someone has a file with an apostrophe in it's name.
Hm... maybe Word does not handle the more obscure URL escape codes. Is this Word 2003? Can you post the couple lines of code that you use to generate and insert the hyperlink?
Avatar of lp3535

ASKER

It works for the spaces, but not apostrophes.

Funnily enough, if I hover over the link when it's created, the link looks to be correct, but when I click on it, it shows the %27 where the apostrophe should be, and therefore can't find the file.
Dim strLink As String
Dim currname As String
currname = Replace(thisDoc.FullName, " ", "%20")
strLink = Replace(currname, "'", "%27")

strBody = "<A HREF=" & "file://" & strLink & ">Threats Notification Form</A>"

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Frosty555
Frosty555
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually if you make the above change, you may be able to *not* do the conversion from apostrophe to %27 and have it work. The missing doublequotes explains why your url was getting truncated at the apostrophe.
Avatar of lp3535

ASKER

Thanks - that's perfect.  You're right, I don't need to worry about the apostrophe either.