【问题标题】:Opening browser on a variable page using vbscript使用 vbscript 在变量页面上打开浏览器
【发布时间】:2023-03-09 18:22:01
【问题描述】:

我正在尝试编写一些 VBScript 来在特定网页上打开浏览器。最终,这个网页将是每个脚本唯一的。目前我有以下有效的代码:

Dim objShell

objShell = CreateObject("Shell.Application")
objShell.ShellExecute("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "www.google.ie", "", "", 1)

但我想让以下工作:

Dim iURL As String
Dim objShell

iURL = "www.google.ie"

objShell = CreateObject("Shell.Application")
objShell.ShellExecute("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", iURL, "", "", 1)

任何想法我做错了什么?任何帮助将不胜感激。

【问题讨论】:

  • 发现问题是由外部程序启动 VBScript 引起的。感谢您的帮助 Alex K。

标签: browser vbscript


【解决方案1】:

没有 As String,因为 VBScript 不是强类型,当你创建 COM 对象的实例时需要 set 并且方法调用周围没有括号;

Dim iURL 
Dim objShell

iURL = "www.google.ie"

set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "chrome.exe", iURL, "", "", 1

或者如果chrome是默认的

set objShell = CreateObject("WScript.Shell")
objShell.run(iURL)

【讨论】:

  • 我正在使用 Visual Studio Express 2012 for Web,它不断删除集合并添加回括号中。我以您推荐的相同方式尝试了其他所有方法,但得到了相同的结果。该变量未被使用,但直接字符串将起作用。
  • 作为改进,您可以使用:oShell.ShellExecute "chrome.exe", """" & iURL & """", "", "", 1(将允许传递按空格分隔的参数而不对其进行编码)
【解决方案2】:

我发现最简单的方法是这样做:

set WshShell=WScript.CreateObject("WScript.Shell")
WshShell.run "chrome.exe"
WScript.sleep 400
WshShell.sendkeys "URL HERE"
WshShell.sendkeys "{ENTER}"

您也可以这样做来关闭 chrome:

WshShell.sendkeys "%{F4}"

【讨论】:

  • 希望我没有打错字
【解决方案3】:

你能在“objShell.ShellExecute”中插入“Call”前缀吗

Dim objShell
Set objShell = CreateObject("Shell.Application")

iURL = "www.google.com"

Call objShell.ShellExecute("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", iURL, "", "", 1)

对于 IE:

'Call objShell.ShellExecute("iexplore.exe", iURL, "", "", 1)


For more info below code also works,

Dim objShell
Set objShell = CreateObject("Shell.Application")

铬:

iURL = "www.google.com"
'objShell.ShellExecute "chrome.exe", iURL, "", "", 1

即:

'objShell.ShellExecute "iexplore.exe", iURL, "", "", 1

【讨论】:

    【解决方案4】:
      Sub RickRoller()
       Dim counter, myNum, objShell, iURL
       counter = 0
       myNum = 100
       Do Until myNum = 1
         myNum = myNum - 1
         counter = counter + 1
         Set WshShell = CreateObject("WScript.Shell")
         WshShell.SendKeys(chr(&hAF))
       Loop
       set objShell = CreateObject("WScript.Shell")
       iURL = "https://youtu.be/oHg5SJYRHA0?autoplay=1&t=44"
       objShell.run(iURL)
     End Sub
    
    
     RickRoller()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 2022-01-14
      相关资源
      最近更新 更多