访问VBA以关闭通过Shell打开的Chrome窗口

时间:2016-02-01 14:58:44

标签: vba shell google-chrome access-vba

我正在尝试通过VBA功能关闭shell Chrome窗口。我的函数运行一个返回.csv文件的URL查询。问题是我想关闭窗口,以便它不会一直显示(此过程每3分钟运行一次)。我还没有能够找到一个我可以开始工作的解决方案。我尝试在一个网站建议之后添加SendKeys "%{F4}"。这只是最小化窗口,而不是关闭窗口。我之后尝试添加DoCmd.Close Shell, "Untitled",但这也没有用。我花了几个小时试图做,我想象的是一个简单的任务,感觉另一组眼睛可以指向正确的方向。以下是我打开Chrome的代码。非常感谢任何帮助。

Public Function RunYahooAPI()

Dim chromePath As String

chromePath = """C:\Program Files\Google\Chrome\Application\chrome.exe"""

Shell (chromePath & " -url http://download.finance.yahoo.com/d/quotes.csv?s=CVX%2CXOM%2CHP%2CSLB%2CPBA%2CATR%2CECL%2CNVZMY%2CMON&f=nsl1op&e=.csv")

End Function

1 个答案:

答案 0 :(得分:0)

此VBA代码将启动(如您的问题)chrome,将变量pHandle中的Process句柄保存,使用此Handle循环所有进程,然后停止进程(在检查进程所有者的用户和域之后) )。

Sub LaunchandStopProcess()

'
' As in your Question
'
Dim chromePath As String
Dim pHandle As Variant

chromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

'
' Note: Shell pass the Process Handle to the PID variable
'
PHandle = Shell(chromePath & " -url http://download.finance.yahoo.com/d/quotes.csv?s=CVX%2CXOM%2CHP%2CSLB%2CPBA%2CATR%2CECL%2CNVZMY%2CMON&f=nsl1op&e=.csv")

Dim objWMIcimv2 As Object
Dim objProcess As Object
Dim objList As Object
Dim ProcToTerminate As String
Dim intError As Integer

Set objWMIcimv2 = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set objList = objWMIcimv2.ExecQuery("select * from win32_process where Handle='" & CStr(pHandle) & "'")

'
' ObjList contains the list of all process matching the Handle (normally your chrome App, if running)
'

If objList.Count = 0 Then
    ' No matching Process
    ' Set all objects to nothing
    Set objWMIcimv2 = Nothing
    Set objList = Nothing
    Set objProcess = Nothing
    Exit Sub
Else
    '
    ' Parse all matching Processes
    '
    For Each objProcess In objList
        ' additionally check with actual user
        colProperties = objProcess.getowner(strNameofUser, strUserdomain)

        If strUserdomain + "\" + strNameofUser = Environ$("userdomain") + "\" + Environ$("username") Then
            intError = objProcess.Terminate
            If intError <> 0 Then
                '
                ' Trap Error or do nothing if code run unattended
                '
            Else
                ' Confirm that process is killed or nothing if code run unattended
            End If
        End If
    Next

    Set objWMIcimv2 = Nothing
    Set objList = Nothing
    Set objProcess = Nothing

End If 
End Sub