Excel 2007 VBA 网络查询错误 1004 "address of this site is not valid"

Excel 2007 VBA error 1004 in web query "address of this site is not valid"

我正在使用以下代码从 html 页面获取数据,该页面存储在我电脑上的一个文件夹中。 我在另一个模块中使用了基本相同的代码,它工作得很好,所以我不明白为什么它不能在这个单独的例程中工作。我需要单独例程的原因是因为原始代码嵌入在一个非常复杂的大型例程中,我不能 运行 只是为了检查特定文件的数据,BTW 已经分析过代码没有任何问题在更复杂的套路中。 (原代码如下对比)

Dim Act As Worksheet, ActSt As Worksheet

On Error GoTo errorhandler

    Set Actbl = Workbooks("table.xlsm")
    Set ActSt = Actbl.Worksheets("act.st.")                 'query will be stored here
'some code to define path &filename
............
' Create URL
URL = path & filename

' Create Web Query & refresh it

If Len(Dir(URL)) > 0 Then               'found the file
    ActSt.Activate
    ActSt.Cells.Clear                   'clear sheet "act.st."

        'set up a table import (the URL; tells Excel that this query comes from an html file)
        Set qt = ActSt.QueryTables.Add( _
            Connection:="URL;" & filename, Destination:=ActSt.Range("A4"))     'save data in "act.st."
    With qt
        .WebConsecutiveDelimitersAsOne = False
        .WebDisableDateRecognition = False
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = False
        .WebSelectionType = xlEntirePage
        .WebSingleBlockTextImport = False
        .RefreshStyle = xlOverwriteCells
        .Refresh                                    'get the data
    End With
    ActSt.QueryTables.Item(1).Delete                'delete the created query, otherwise they accumulate
Else
    MsgBox "File not found"
    Exit Sub
End If
errorhandler:
    answer = MsgBox("Error " & Err.Number & ": " & Err.Description & ". Exit?", vbYesNo)
    If answer = vbYes Then Exit Sub
    Resume

虽然行 "If Len(Dir(URL)) > 0 Then" 确保文件存在,但是当代码到达 "refresh" 时我得到一个错误。消息是:

Error 1004: The address of this site is not valid. Check the address and try again. Exit?

(文本可能与英语略有不同 OS 因为实际上它是西班牙语,这只是我的 t运行slation)

我不明白当文件明显存在并且用"Dir(URL)"检测到时地址怎么会是"not valid"以及如何解决这个问题。

与此相关的第二个问题涉及我之前测试此代码时遇到的另一个错误 1004。当我 运行 代码时,我在浏览器中打开了文件,但我收到了一个 1004 错误,类似于 "error defined by application"。 我想这意味着 "file in use by another user" 左右。有没有办法区分这种 1004 错误,以便错误消息更具体?像 "error sub-number" 之类的东西? 1004 非常通用。

谢谢大家帮我找到解决办法,尤其是第一个问题。

这是我为较短的例程稍微复制和改编的原始代码片段:

If Len(Dir(filename)) > 0 Then      'found the file
    GetFile = True
    ws1.Cells.Clear                 'clear sheet "act.st."

        'set up a table import (the URL; tells Excel that this query comes from an html file)
        Set qt = ws1.QueryTables.Add( _
            Connection:="URL;" & filename, Destination:=ws1.Range("A4"))     'save data in "act.st."
    With qt
        .WebConsecutiveDelimitersAsOne = False
        .WebDisableDateRecognition = False
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = False
        .WebSelectionType = xlEntirePage
        .WebSingleBlockTextImport = False
        .RefreshStyle = xlOverwriteCells
        .Refresh                                'get the data
    End With
    ws1.QueryTables.Item(1).Delete              'delete the created query, otherwise they accumulate
Else
    GetFile = False
End If

看起来您还没有设置两个变量的值,即路径和文件名,它们正在构建 URL,导致 URL 的空值。请仔细检查路径和文件名变量,看看它们是否设置正确。谢谢