Class: Bitly in Category Internet/Web : Internet/Web from Total Visual SourceBook

Use the web service to convert a long Internet URL string to a short one and vice versa with VBA and VB6. is used by Twitter to shorten URLs in tweets. For your own use, a single access token can be used for OAuth authentication. To use for free, head to

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the CBitly class.
AccessToken Property Access token which is for personal use of API calls.
UserName Property user name for multiple account use of API calls.
ApiKey Property API Key multiple account use of API calls.
Class_Initialize Initialize Initialize values to defaults which may be overridden with property settings and methods.
GetCredentials Private Create the syntax to provide the credentials to the API callCredentials can be provided as an Access Token or User Name and API Key.
ShortenURL Method Create a shortened URL using the URL shortening service.
OriginalURL Method Convert a shortened URL to its original long URL.
InfoXML Method Get information for a page from a shortened URL. The result is in XML which can be parsed using the CXMLRead class.
URLEncode Private Encodes a string so it can be passed to a URL.
TrimResponse Private Process response to terminate at the first return character. If there isn't one, the entire string is returned.
' Examples of using the CBitly class to use the web service to convert a long Internet URL string to a short one and vice versa with VBA and VB6.
' To use API calls, you need to create an account (free) and get the OAuth authentication access token or user name and API key.
' See the declarations section of the CBitly class for additional details.
' To use this example, create a new module and paste this code into it.
' Then run the procedure by putting the cursor in the procedure and pressing:
'    F5 to run it, or
'    F8 to step through it line-by-line (see the Debug menu for more options)

' Set these login values once you get them from
Private Const mcstrAccessToken As String = ""
Private Const mcstrUserName As String = ""
Private Const mcstrAPIKey As String = ""

' Set the default URL to convert
Private Const mcstrLongURL As String = ""

Private Sub Example_CBitly()
  ' Commments: Examples of using the CBitly class to use the web service to convert a long Internet URL string to a short one and vice versa.

  Dim fOK As Boolean
  Dim clsBitly As CBitly
  Dim strLongURL As String
  Dim strBitlyURL As String
  Dim strOriginal As String
  Dim strInfoXML As String

  Set clsBitly = New CBitly

  ' Set login properties. Either the AccessToken alone or the user name/API Key combination is used.
  fOK = True
  If mcstrAccessToken <> "" Then
    clsBitly.AccessToken = mcstrAccessToken
  ElseIf (mcstrUserName <> "") And (mcstrAPIKey <> "") Then
    clsBitly.UserName = mcstrUserName
    clsBitly.ApiKey = mcstrAPIKey
    MsgBox "Set the login information in the Declarations section before running this code."
    fOK = False
  End If

  If fOK Then
    ' Prompt the user for a URL to shorten
    strLongURL = InputBox("Specify the URL to convert to", "CBitly Example", mcstrLongURL)

    If strLongURL <> "" Then
      ' Get the shortened URL from
      strBitlyURL = clsBitly.ShortenURL(strLongURL)
      If strBitlyURL <> "" Then
        Call InputBox(strLongURL & " converted to ", "BitlyURL Example", strBitlyURL)

        ' Show how to convert the shortened URL back to its original URL
        strOriginal = clsBitly.OriginalURL(strBitlyURL)
        If strOriginal = "" Then
          MsgBox "Shortened URL " & strBitlyURL & "cannot be converted back to its original URL. This is most likely because it wasn't saved to your account whent the was created."
          MsgBox "Converted the shortened URL from " & strBitlyURL & " back to " & strOriginal
        End If

        strInfoXML = clsBitly.InfoXML(strBitlyURL)
        Debug.Print "Information on the URL in XML format:"
        Debug.Print strInfoXML
      End If
    End If
  End If

  ' Clean up
  Set clsBitly = Nothing
End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!

View all FMS products for Microsoft Access All Our Microsoft Access Products


Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info




Free Product Catalog from FMS