Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.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
VBA:打开网站时出错_Vba_Hyperlink_Explorer_Basic - Fatal编程技术网

VBA:打开网站时出错

VBA:打开网站时出错,vba,hyperlink,explorer,basic,Vba,Hyperlink,Explorer,Basic,我缩短了代码以提高可见性: Sub open_explorer() 'Open Website 1 Set IE = CreateObject("InternetExplorer.Application") IE.navigate ("https://www.google.ch/search?newwindow=0&q=Dodecan+Sigma-Aldrich") IE.Visible = True i = 0 Do

我缩短了代码以提高可见性:

Sub open_explorer()

    'Open Website 1
    Set IE = CreateObject("InternetExplorer.Application")

    IE.navigate ("https://www.google.ch/search?newwindow=0&q=Dodecan+Sigma-Aldrich")
    IE.Visible = True

    i = 0
    Do
        Wait
        i = i + 1
    Loop Until IE.ReadyState = 4 Or i > 10

    Set dom = IE.document
    Debug.Print (dom)
    Debug.Print (dom.anchors.Length)

    'Open Website 2
    Set IE = CreateObject("InternetExplorer.Application")

    IE.navigate ("https://www.sigmaaldrich.com/catalog/product/sial/457116?lang=en&region=US")
    IE.Visible = True

    i = 0
    Do
        Wait
        i = i + 1
    Loop Until IE.ReadyState = 4 Or i > 10

    Set dom = IE.document
    Debug.Print (dom)
    Debug.Print (dom.anchors.Length)

End Sub

Sub Wait()
    Application.Wait (Now + TimeValue("0:00:01"))
End Sub
如您所见,Sub
open_explorer
打开两个网站并尝试读取其DOM。然而,虽然代码对于第一个网站来说非常有效,但是对于第二个网站来说却不是,尽管代码是相同的。你知道为什么第二个网站不起作用吗


我认为,至少要将
选项显式
放在所有模块的顶部。然后在这段代码中声明所有变量及其类型。以下内容适合我

注:

如果浏览器出于安全原因阻止它,则会在同一来源中发出请求。对于跨域请求,您需要执行一些不同的操作。更多信息请点击这里

代码:

Option Explicit

Sub open_explorer()

    Dim Ie As Object
    'Open Website 1
    Set Ie = CreateObject("InternetExplorer.Application")

    Ie.navigate ("https://www.google.ch/search?newwindow=0&q=Dodecan+Sigma-Aldrich")
    Ie.Visible = True

    Dim i As Long
    i = 0
    Do
        Wait
        i = i + 1
    Loop Until Ie.ReadyState = 4 Or i > 10
    Dim dom As Object
    Set dom = Ie.document
    Debug.Print (dom)
    Debug.Print (dom.anchors.Length)

    'Open Website 2
    Set Ie = CreateObject("InternetExplorer.Application")

    Ie.navigate ("https://www.sigmaaldrich.com/catalog/product/sial/457116?lang=en&region=US")
    Ie.Visible = True

    i = 0
    Do
        Wait
        i = i + 1
    Loop Until Ie.ReadyState = 4 Or i > 10

    Set dom = Ie.document
    Debug.Print (dom)
    Debug.Print (dom.anchors.Length)

End Sub

Sub Wait()
    Application.Wait (Now + TimeValue("0:00:01"))
End Sub
结果:

Option Explicit

Sub open_explorer()

    Dim Ie As Object
    'Open Website 1
    Set Ie = CreateObject("InternetExplorer.Application")

    Ie.navigate ("https://www.google.ch/search?newwindow=0&q=Dodecan+Sigma-Aldrich")
    Ie.Visible = True

    Dim i As Long
    i = 0
    Do
        Wait
        i = i + 1
    Loop Until Ie.ReadyState = 4 Or i > 10
    Dim dom As Object
    Set dom = Ie.document
    Debug.Print (dom)
    Debug.Print (dom.anchors.Length)

    'Open Website 2
    Set Ie = CreateObject("InternetExplorer.Application")

    Ie.navigate ("https://www.sigmaaldrich.com/catalog/product/sial/457116?lang=en&region=US")
    Ie.Visible = True

    i = 0
    Do
        Wait
        i = i + 1
    Loop Until Ie.ReadyState = 4 Or i > 10

    Set dom = Ie.document
    Debug.Print (dom)
    Debug.Print (dom.anchors.Length)

End Sub

Sub Wait()
    Application.Wait (Now + TimeValue("0:00:01"))
End Sub

参考文献:

Option Explicit

Sub open_explorer()

    Dim Ie As Object
    'Open Website 1
    Set Ie = CreateObject("InternetExplorer.Application")

    Ie.navigate ("https://www.google.ch/search?newwindow=0&q=Dodecan+Sigma-Aldrich")
    Ie.Visible = True

    Dim i As Long
    i = 0
    Do
        Wait
        i = i + 1
    Loop Until Ie.ReadyState = 4 Or i > 10
    Dim dom As Object
    Set dom = Ie.document
    Debug.Print (dom)
    Debug.Print (dom.anchors.Length)

    'Open Website 2
    Set Ie = CreateObject("InternetExplorer.Application")

    Ie.navigate ("https://www.sigmaaldrich.com/catalog/product/sial/457116?lang=en&region=US")
    Ie.Visible = True

    i = 0
    Do
        Wait
        i = i + 1
    Loop Until Ie.ReadyState = 4 Or i > 10

    Set dom = Ie.document
    Debug.Print (dom)
    Debug.Print (dom.anchors.Length)

End Sub

Sub Wait()
    Application.Wait (Now + TimeValue("0:00:01"))
End Sub

  • 我不知道为什么它对你有用。我更正了“Option Explicit”标题,但这并没有解决CORS错误

    但是,我为我的服务器编写了一个小PHP片段来加载URL:

    <?php
    
    $link = urldecode( $_GET['link'] );
    
    $file = file_get_contents( $link );
    
    echo $file;
    

    请习惯将Option Explicit放在模块顶部。选择工具->选项->并选中“需要变量声明”框。然后检查代码并声明所有变量。从长远来看,这确实会带来好处。