【问题标题】:How to "Keyboard Focus" on Web Page just opened by ShellExecute?如何在ShellExecute刚刚打开的网页上“键盘焦点”?
【发布时间】:2020-12-18 21:33:59
【问题描述】:

我使用 ShellExecute 打开 Outlook 电子邮件中的所有超链接。

我想保存所有打开的网页(我的默认浏览器是 Chrome)。

我试过SendKeys。我怀疑我需要在ShellExecuteSendKeys 之前放置一些代码,以便它将打开的网页识别为“活动”。换句话说,我想将键盘焦点设置在打开的 Chrome 网页上。

Private Declare PtrSafe Function ShellExecute _
  Lib "shell32.dll" Alias "ShellExecuteA" ( _
  ByVal hWnd As Long, _
  ByVal Operation As String, _
  ByVal Filename As String, _
  Optional ByVal Parameters As String, _
  Optional ByVal Directory As String, _
  Optional ByVal WindowStyle As Long = vbMinimizedFocus _
  ) As Long

Sub OpenLinksMessage()
    Dim olMail As Outlook.MailItem
    Dim Reg1 As RegExp
    Dim M1 As MatchCollection
    Dim M As Match
    Dim strURL As String
    Dim lSuccess As Long
 
    Set olMail = Application.ActiveExplorer().Selection(1)

    Set Reg1 = New RegExp

    With Reg1
        .Pattern = "(https?[:]//([0-9a-z=\?:/\.&-^!#$%;_])*)"
        .Global = True
        .IgnoreCase = True
    End With

    If Reg1.Test(olMail.Body) Then

        Set M1 = Reg1.Execute(olMail.Body)
        For Each M In M1
            strURL = M.SubMatches(0)
            Debug.Print strURL
            If InStr(strURL, "unsubscribe") Then GoTo NextURL
            If Right(strURL, 1) = ">" Then strURL = Left(strURL, Len(strURL) - 1)

            lSuccess = ShellExecute(0, "Open", strURL)
            DoEvents

            'This is where I think I need codes

            SendKeys ("^S"), True
            SendKeys ("name"), True
            SendKeys "{Enter}", True

NextURL:
  
        Next
    End If

    Set Reg1 = Nothing
End Sub

弄清楚这一点后,我将调整代码以循环执行以打开并保存100多个链接。

【问题讨论】:

  • 我有这段代码使用 IE 对象,但我认为这不是你想要的,对吧?你到底想用 CTRL+S 保存什么?如果您说出目的,我们可以做得更好,然后为您发送密钥...
  • 欢迎您的回复。不幸的是,我无法使用 Internet Explorer (IE) - 我试图打开的超链接,仅受 Chrome 支持。澄清一下,我真正需要的是在我打开的 Chrome 网页上设置键盘焦点。

标签: vba google-chrome outlook


【解决方案1】:

我评论说我们可以使用 IE 对象,并做更好的方法来自动化这个过程

但是回答你的问题你可以试试

Dim i As Integer

SendKeys ("^S"), True
On Error Resume Next
For i = 1 To 100
    VBA.AppActivate ("Save As")
Next i
On Error GoTo 0
SendKeys ("name"), True
SendKeys "{Enter}", True

【讨论】:

  • 我添加了您的代码并得到“编译错误 - 预期:表达式”
猜你喜欢
  • 1970-01-01
  • 2011-06-11
  • 1970-01-01
  • 2011-07-07
  • 1970-01-01
  • 2017-12-19
  • 1970-01-01
  • 2022-12-01
  • 1970-01-01
相关资源
最近更新 更多