1

I've been trying to create a quick subroutine in VBA through Excel 2010 to automate putting a list of URLs through bit.ly and copying the abbreviated links back to replace their original link. BUT I'm getting an Error 70: Permission Denied runtime error along the way. I've had a few courses and this MOSTLY works, but I'm not highly familiar with VBA and could use some help in debugging this if possible (it'd be a huge help). Here's the code:

Option Explicit

Dim IE As Object

Sub AutoAbbrev()

Set IE = CreateObject("InternetExplorer.Application")
Dim holdURL As String
Dim row_number As Integer
IE.Visible = True

For row_number = 101 To 112

holdURL = ""

If Range("b" & row_number).Value = "" Then GoTo Skip

IE.navigate "http://www.bitly.com" 'load bit.ly

Do While IE.readyState <> 4
    DoEvents
Loop

IE.document.all("shorten_url").Value = Range("b" & row_number).Value
IE.document.all("shorten_btn").Click

Do While IE.document.all("shorten_url").Value = Range("b" & row_number).Value Or IE.document.all("shorten_url").Value = ""
    DoEvents
Loop

holdURL = IE.document.all("shorten_url").Value
IE.document.all("shorten_url").Value = ""
Range("b" & row_number).Value = holdURL

Skip:
Next row_number

End Sub

Private Sub Command1_Click()

AutoAbbrev
End Sub

Private Sub Form_Unload(Cancel As Integer)
Set IE = Nothing
If TypeName(IE) <> "Nothing" Then Unload IE
Set IE2 = Nothing
If TypeName(IE2) <> "Nothing" Then Unload IE2

End Sub

The error is mostly thrown on this line after the program has run through one or more iterations:

Do While IE.document.all("shorten_url").Value = Range("b" & row_number).Value Or IE.document.all("shorten_url").Value = ""
        DoEvents
    Loop

If any specific advice could be provided to help me over this hump, I'd greatly appreciate it. Thanks!

3
  • Can you give an example of any one of the Col B cell values so that we can test it? Sep 25, 2013 at 4:20
  • May be use could try like this .. While IE.document.getelementsbyid("shorten_url") nothing 'do more checks here DoEvents end while Sep 25, 2013 at 4:44
  • Any value in column b could be substituted with any web link - stackoverflow.com, google.com, etc. Sep 26, 2013 at 4:25

2 Answers 2

1

Automating Internet Explorer should always be a last resort, it's slow and relies on the structure of the page remaining unchanged. It's always better to opt for an API if one is available, in this case bitly provide an API for shotening links, you just need to get your Authentication Token and enter it in the below:

Public Function Shorten(url As String) As String

    Const token As String = "YOUR AUTHENTICATION TOKEN"
    Static oRequest As Object

    If oRequest Is Nothing Then Set oRequest = CreateObject("winhttp.winhttprequest.5.1")

    With oRequest
        .Open "GET", "https://api-ssl.bitly.com/v3/shorten?access_token=" & token & "&longUrl=" & url & "&format=xml", False
        .send
        If Left(Split(.responsetext, "txt>")(1), 2) = "OK" Then Shorten = Split(Split(.responsetext, "url>")(1), "<")(0)
    End With

End Function

You can then use the above as a function in your worksheet

2
  • This looks relatively elegant and simple. Only I'm not sure how I'd go about finding/incorporating my authentication token. Also, this is a macro that will be distributed for many users. Is there a way to have the program determine the authentication token (which I assume will be different for each user on a different machine?) Sep 26, 2013 at 4:19
  • I suspect that your error is due to the fact that bitly is limiting the number of requests in a given time period to prevent this kind of web scraping. For the API, once you've singed up, you can get your API key here: bitly.com/a/your_api_key. You'll need to look at the usage restrictions and see if you can work with them. I suppose you could use your API key for all your users, but you might be better off finding another solution
    – SWa
    Sep 26, 2013 at 8:15
0

I found this occurs because of the page not being fully loaded. IE is SLOW but sometimes you need to use it because you have dynamic content in Divs which need to be opened with an object.click event. Do Until Not appIE.Busy And appIE.ReadyState = 4: DoEvents: Loop can help but it can also hang up your browser so adding a wait period using a timer can help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.