Trapping a Web Query error

LEXCERM

Active Member
Joined
Jun 26, 2004
Messages
320
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I'm trying to trap the following error which is produced at the following line of a web query import (because the website is down):
.Refresh BackgroundQuery:=False

The following error is produced but without an error number:-
"Unable to open http://........ Cannot download the information you requested."

Then directly followed by:-
Error 1004: "Application-defined or object-defined error."

I always use error trapping in all of my codes but nothing seems to trap this one.

Any ideas?

Thanks in advance,
Paul.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
How are you creating the web query? If you do it like this - creating a QueryTable object, and refreshing it separately - you should be able to handle any error, as shown:
Code:
Option Explicit

Sub Macro1()

    Dim QT As QueryTable
    
    Set QT = ActiveSheet.QueryTables.Add(Connection:="URL;http://www.mrexcel.com/forum/index.php", _
        Destination:=Range("A1"))
        
    With QT
        .Name = "MrExcel_Forums"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = False                'Was True with Macro Recorder
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = "8"
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        '.Refresh BackgroundQuery:=False        'Don't refresh here
    End With
    
    On Error Resume Next
    QT.Refresh BackgroundQuery:=False
    
    If Err.Number = 0 Then

        'No error occurred
        
        On Error GoTo 0
        Debug.Print Now; QT.Name & " - Retrieved OK"

    Else

        'An unexpected error occurred - tell the user
        
        On Error GoTo 0
        Debug.Print Now; QT.Name & " - Error " & Err.Number & " " & Err.Description

        MsgBox "Web query name: " & QT.Name & vbNewLine & _
            "Error number " & Err.Number & vbNewLine & _
            Err.Description, , "Web query error"

    End If
    
End Sub
The above web query was generated by the Macro Recorder and I just edited the code to separate the .Add, property values and .Refresh parts.
 
Upvote 0
Many thanks John W! That's exactly what I was after.

Cheers,
Paul.
 
Upvote 0
Not sure if it's my excel 2013 but when I paste the code and change the url into a non excisting one the error isnt handled. I still get a message box saying error 1004 unable to open "http...." etc etc.


In fact, it doesnt load my webquery at all even when I don't change any parameters from your code. Does anyone know how to fix this ?
 
Upvote 0

Forum statistics

Threads
1,217,383
Messages
6,136,267
Members
450,001
Latest member
KWeekley08

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top