0

我有一个包含网页链接的 Excel 工作簿。用户可以单击链接,从而最小化 Excel 窗口并打开他们的浏览器。当他们完成网站后,他们会最小化或关闭浏览器,这会将他们返回到 Excel(因为这是他们之前的活动窗口)。

当用户返回 Excel 时,我希望 VBA 采取行动(更新表格)。

我查看了 Workbook_WindowActivate 事件,但这仅适用于您在 Excel 应用程序中从一个 Excel 工作簿移动到另一个工作簿时。

也许我可以以某种方式使用 Application.name 或 Windows 函数 GetActiveWindow 但我不确定如何最好地做到这一点。

有任何想法吗?谢谢!

4

2 回答 2

2

您想要为 Workbook_SheetFollowHyperlink 添加事件处理程序。然后,您可以使用下面的代码。这只是检查网页是否有焦点。'DO EVENTS' 是您添加代码然后退出子的地方

'********************* References used
'* Microsoft Shell Controls An Automation : shell32.dll*
'* Microsoft HTML Objects Library: MSHTML.dll expand » *
'* Microsoft Internet Controls: IEFRAME.dll *

Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)
Dim ie As InternetExplorer 'IE window variable
Dim sUrl 'url of the webpage
Dim dt As Date 'timer

'set the url to look for
sUrl = Target.Address
'set initial timeout period *used instead of browser ready due to page redirection.
'you should also check the browser ready status
dt = DateAdd("s", 5, DateTime.Now)
    Do While dt > DateTime.Now
        DoEvents
    Loop

'reset the timeout period to allow time to view and select
dt = DateAdd("s", 30, DateTime.Now)
Dim shShell As New Shell ' windows shell variable
    'continue loop until we hit the timeout or the webpage no longer has focus
    Do While dt > DateTime.Now
        'Loop through all the IE windows
        For Each ie In shShell.Windows
            'check to see if the URL's match
            If InStr(ie.LocationURL, sUrl) Then

            Dim hDoc As HTMLDocument
            'get the webpage document
            Set hDoc = ie.document
                'check to see if it has focus
                If Not hDoc.hasFocus Then
                    ThisWorkbook.Activate
                    '''''''''''''
                    ' DO EVENTS '
                    '''''''''''''
                    Exit Sub
                End If
            Set hDoc = Nothing
            End If
        Next ie
    Loop
End Sub
于 2013-02-01T00:42:07.833 回答
0

这就是我最终要做的。我从这篇文章中借了很多东西:如何制作一个在 Excel 中定期执行的宏?

当用户单击超链接时,代码开始定期检查 Excel 是否是他们的活动窗口。我发现如果用户不在 Excel 应用程序中,GetActiveWindow 函数返回零,如果是,则返回一些正数。如果代码发现用户从不同的窗口返回到 Excel(之前的检查发现他们在不同的窗口中,而当前的检查发现他们在 Excel 中)然后我的表会更新并且计时器停止检查活动窗口.

这样做的好处是适用于任何网络浏览器。

Option Explicit

Dim ExcelIsActive As Boolean
Private Declare Function GetActiveWindow Lib "user32" () As Long
Dim m_dtNextTime As Date
Dim m_dtInterval As Date
Dim DisableFlag As Boolean

Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)
    Call start
End Sub

Public Sub Enable(Interval As Date)
    Call Disable
    m_dtInterval = Interval
    Call starttimer
End Sub

Private Sub starttimer()
    m_dtNextTime = Now + m_dtInterval
    Application.OnTime m_dtNextTime, "TestActive"
End Sub

Public Sub TestActive()
    If GetActiveWindow > 0 Then
        If ExcelIsActive = False Then
            ExcelIsActive = True
            Call RefreshQuery
        End If
    Else
        ExcelIsActive = False
    End If
    If Not DisableFlag Then
        Call starttimer
    Else
        Call Disable
    End If
End Sub

Public Sub Disable()
    Dim dtZero As Date
    If m_dtNextTime <> dtZero Then
        ' Stop timer if it is running
        On Error Resume Next
        Application.OnTime m_dtNextTime, "TestActive", , False
        On Error GoTo 0
        m_dtNextTime = dtZero
    End If
    m_dtInterval = dtZero
End Sub

Sub start()
    'Start the timer
    DisableFlag = False
    ExcelIsActive = True
    'Sets the interval to be three seconds
    Call Enable(#12:00:03 AM#)
End Sub

Public Sub RefreshQuery()

'I do my stuff here

'Stop the timer until the next time the user launches the browser
DisableFlag = True

End Sub
于 2013-02-04T22:25:48.453 回答