Shorten urls with bit.ly web API and VBA
November 19, 2009 • JP • No Comments • Rate This Article
• Links to this article
Turns out you can shorten URLs easily with bit.ly. In More XMLHTTP Web API examples I mentioned that it was difficult, because bit.ly returns an XML document which must be parsed for the response.
Apparently bit.ly can also return just the shortened URL. So without further ado, here is the extremely short code that returns a shortened URL:
' based on http://code.google.com/p/bitly-api/wiki/ApiDocumentation
Dim xml As Object
Set xml = CreateObject("MSXML2.XMLHTTP")
xml.Open "POST", _
"http://api.bit.ly/shorten?version=2.0.1&format=text&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&longUrl=" & _
txt, False
xml.Send
bitlyURL = xml.responsetext
End Function
By specifying the format as text, we get only the shortened URL and none of that pesky XML. Note that the URL string requires a login and API key. I "borrowed" the demo information from the API documentation, so if the function stops working, you might need to register and get an API key.
Sample usage:
Debug.Print bitlyURL("http://www.google.com")
End Sub
Previous Post: Another free giveaway
Next Post: Create Outlook toolbar buttons using VBA




People use shortened urls way too much. Sure, in twitter you need them,otherwise the url takes up 2/3 of your message. But elsewhere, it's courteous to publish the entire url so a user knows where the click will lead.