Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 2007 web查询中的VBA错误1004“;此网站的地址无效;_Vba_Excel 2007_Excel Web Query - Fatal编程技术网

Excel 2007 web查询中的VBA错误1004“;此网站的地址无效;

Excel 2007 web查询中的VBA错误1004“;此网站的地址无效;,vba,excel-2007,excel-web-query,Vba,Excel 2007,Excel Web Query,我使用以下代码从html页面获取数据,该页面存储在我电脑的文件夹中。 我在另一个模块中使用了基本相同的代码,在这个模块中它工作得很好,所以我不明白为什么它在这个单独的例程中不工作。我之所以需要单独的例程,是因为原始代码嵌入在一个相当复杂的大型例程中,我无法运行该例程来检查特定文件的数据,顺便说一句,在更复杂的例程集中,对该文件进行了分析,代码没有任何问题。(下面是原始代码以供比较) 尽管行“If Len(Dir(URL))>0 Then”确保文件存在,但当代码到达“刷新”时,我会收到一个错误。信

我使用以下代码从html页面获取数据,该页面存储在我电脑的文件夹中。 我在另一个模块中使用了基本相同的代码,在这个模块中它工作得很好,所以我不明白为什么它在这个单独的例程中不工作。我之所以需要单独的例程,是因为原始代码嵌入在一个相当复杂的大型例程中,我无法运行该例程来检查特定文件的数据,顺便说一句,在更复杂的例程集中,对该文件进行了分析,代码没有任何问题。(下面是原始代码以供比较)

尽管行“If Len(Dir(URL))>0 Then”确保文件存在,但当代码到达“刷新”时,我会收到一个错误。信息是:

错误1004:此站点的地址无效。请检查地址,然后重试。退出

(文本可能与英语操作系统略有不同,因为实际上它是西班牙语的,这只是我的翻译)

我不明白当文件明显存在并且被“Dir(URL)”检测到时,地址如何“无效”,以及如何解决这个问题

与此相关的第二个问题涉及我之前测试此代码时遇到的另一个错误1004。当我运行代码时,我在浏览器中打开了文件,我得到了一个1004错误,类似于“应用程序定义的错误”。 我想这意味着“其他用户正在使用文件”之类的东西。有没有办法区分这种1004错误,使错误信息更具体?类似“错误子编号”的东西?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为空值。请仔细检查路径和文件名变量以及它们是否设置正确。谢谢

当您请求文件时,是否检查服务器是否返回200 OK HTTP响应代码?感谢您抽出时间回复。问题解决了(见下文)。非常感谢,虽然略有不同,但确实存在错误。路径和文件名都已定义,然后组合到变量URL中。这是我用Dir函数检查的结果,结果是ok,但在查询中我只使用了filename而不是URL。你的提示帮助我找到了错误。荣誉
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