VBA IE 11 Outlook如何抑制“您正在查看的网页正在尝试关闭标签页”。

时间:2016-08-22 04:44:00

标签: vba excel-vba internet-explorer-11 outlook-vba excel

我有一个Outlook VBA脚本,它从电子邮件中的超链接获取URL,然后打开将我带到受密码保护的Intranet站点并下载文件的URL。该项目非常挑剔。当我从excel运行它时似乎运行正常,但是当我从outlook运行脚本时,我得到了下面的消息。这包括从outlook运行excel脚本。

This is the message I am getting.

为了避免发送密钥方法,我已经走了很长的路。有没有人有任何想法如何压制这个?

我发现了这个post,但我不相信同样的规则适用于vba即自动化。

1 个答案:

答案 0 :(得分:0)

给它一个镜头,它使用Windows API函数首先找到窗口,然后向该窗口发送一条消息来关闭它。

Option Explicit
Public Const WM_CLOSE = &H10

'Api Declarations
#If VBA7 Then
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
#Else
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    Private Declare Function SendMessage Lib "user32" Alias "SendMessage" (ByVal hwnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
#End If

Public Sub CloseWindow()
    Dim Ret        As Long ' A return value
    Dim Windowname As String: Windowname = "Windows Internet Explorer" ' the name of the window you want to close

    Ret = FindWindow(vbNullString, Windowname) ' find the handle of the window
    If Ret <> 0 Then SendMessage Ret, WM_CLOSE, 0&, 0& 'close the window
End Sub