QueryTable: Always getting Error 1004 after first bad refresh despite valid connections

Apple2Plus

New Member
Joined
Sep 18, 2008
Messages
4
Hello,

I am using Excel 2007 to work on a problem which might be shared by others; that is, to retrieve delimited data (tab, comma, etc) from text files but that are at remote locations. The files are obtained by http or ftp protocol though I have not yet tried with ftp.

Instead of using a URL connection type with a URL string (Eg: connection:="URL;http://www.awebsitename.com/filename.txt"), I am unconventionally using a TEXT connection with the URL string (i.e. connection:="TEXT;http://www.awebsitename.com/filename.txt").

Looping over a list of files, this method works well until you attempt to connect to a file that doesn't exist. At this instant, you get Error 1004 and then you get Error 1004 for all subsequent files you attempt to connect to even though they do truely exist and are accessible.

Below you will find the code I created to accomplish this. The error occurs at the line refreshing the query table:
".Refresh BackgroundQuery = False"

Am I missing something? Currently the only way of proceeding is to close Excel, re-open it, remove the bad file from the list, then resume at the next valid file.

Does anyone have some insight into this?

Thanks in advance!

Rich (BB code):
Function downloadDataToSheet(ByRef urlString As String, ByRef destinationSheet As Worksheet) As Boolean
 
    Dim success As Boolean
    success = true ' start optimistically
 
On Error GoTo EH
 
    ' Clear any previous data from the destination sheet
    clearAllDataFromSheet destinationSheet
 
    Dim connectionString As String
    connectionString = "TEXT;" + urlString
    Dim pQueryTable As QueryTable
    Set pQueryTable = destinationSheet.QueryTables.Add(Connection:=connectionString, Destination:=destinationSheet.Range("A1"))
 
    If Not (pQueryTable Is Nothing) Then
        With pQueryTable
            .Name = "downloadTable"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = True
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery = False
        End With
    Else
        ' Couldn't add the QueryTable
        success = false
    End If
EH:
    If Err.Number <> 0 Then
 
        If (True) Then
            Msg = "Error # " & Str(Err.Number) & " was generated by " _
                    & Err.Source & Chr(13) & Err.Description
            MsgBox Msg, , Error, Err.HelpFile, Err.HelpContext
        End If
 
        success = false
 
    End If
 
    downloadDataToSheet = success
 
End Function
 
Sub clearAllDataFromSheet(ByRef aSheet As Worksheet)
 
On Error GoTo EH1
 ' Remove all data connections
 Dim i As Long
 For i = (ActiveWorkbook.Connections.Count) To 1 Step -1
     ActiveWorkbook.Connections.Item(i).Delete
 Next i
 
EH1:
 
On Error GoTo EH2
 ' Remove all query tables
 aSheet.Range("A:IV").QueryTable.Delete
 
EH2:
 
 aSheet.Range("A:IV").ClearContents
 
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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