【问题标题】:Open and Activate Outlook via VBScript通过 VBScript 打开和激活 Outlook
【发布时间】:2016-02-09 16:55:21
【问题描述】:

我正在使用 VBS 来控制我需要打开 Outlook 并在窗口上激活/设置焦点的过程。我在将焦点设置在窗口上时遇到了问题 - 当它运行时,窗口焦点仍保留在我打开的资源管理器窗口上,以便双击并在 VBS 文件上运行。

根据我的阅读,打开一个新的 Outlook 实例应该成为焦点,如果我在没有将焦点放在资源管理器窗口的情况下运行脚本(例如使用 Sendkeys),它工作得非常好,但如果资源管理器窗口具有焦点。这很重要,因为它将通过任务计划程序设置为运行,因此无论任务运行时当前焦点在哪里,它都需要工作。

这是现有的 VBS:

Option Explicit

OpenOutlook

Sub OpenOutlook()

  Dim oApp
  Dim oName
  Dim oFolder
  Dim WShell

  Set WShell = WScript.CreateObject("Wscript.Shell")
  Set oApp = CreateObject("Outlook.Application") 
  Set oName = oApp.GetNamespace("MAPI")
  OName.Logon "Default Outlook Profile",, False, True
  Set oFolder = oName.GetDefaultFolder(6)
  oFolder.Display
  OApp.ActiveExplorer.Activate
  WShell.AppActivate "Inbox - myemail@mydomain.com - Microsoft Outlook"

End Sub

【问题讨论】:

    标签: vbscript outlook focus


    【解决方案1】:

    因此,实验找到了一种解决方法 - 将 WShell 命令移出此 VBS 宏并移至单独的 VBS 宏中,然后从第三个宏中背靠背调用这两个命令。这是最终的布局:

    壳牌VBS:

    Option Explicit
    
    SendTLShell
    
    Sub SendTLShell()
    
      Dim filepath
      Dim oShell
      Set oShell = CreateObject("Wscript.Shell")
    
      filepath = Chr(34) & "\\myfilepath\OutlookControl.vbs" & Chr(34)
      oShell.Run "wscript " & filepath, , True
    
      filepath = Chr(34) & "\\myfilepath\SendReports.vbs" & Chr(34)
      oShell.Run "wscript " & filepath, , True
    
      Set oShell = Nothing
    
     End Sub
    

    Outlook 控件 VBS:

    Option Explicit
    
    OpenOutlook
    
    Sub OpenOutlook()
    
      Dim oApp
      Dim oName
      Dim oFolder
    
      Set oApp = CreateObject("Outlook.Application") 
      Set oName = oApp.GetNamespace("MAPI")
      OName.Logon "Default Outlook Profile",, False, True
      Set oFolder = oName.GetDefaultFolder(6)
      oFolder.Display
      OApp.ActiveExplorer.Activate
    
      Set oApp = Nothing
      Set oName = Nothing
      Set oFolder = Nothing
    
    End Sub
    

    SendReports(“激活”)VBS,也做一些 Excel 的事情:

    Option Explicit
    
    RunFilePullMacro
    
    Sub RunFilePullMacro() 
    
      Dim xlApp 
      Dim xlBook 
      Dim oShell
      Dim wShell
    
      Set wShell = WScript.CreateObject("Wscript.Shell")
      Set oShell = CreateObject("Shell.Application")
      oShell.MinimizeAll
    
      wShell.AppActivate "Inbox - myusername@mydomain.com - Microsoft Outlook"
      Set xlApp = CreateObject("Excel.Application")
      xlApp.Application.Visible = True
      xlApp.DisplayAlerts = False 
      Set xlBook = xlApp.Workbooks.Open("\\myfilepath\myexcelfile.xlsm", 0, True) 
      xlApp.Run "FeedbackCheck"
      xlApp.ActiveWorkbook.Close
      xlApp.DisplayAlerts = True
      xlApp.Quit 
    
      Set xlBook = Nothing 
      Set xlApp = Nothing 
      Set oShell = Nothing
      Set wShell = Nothing
    
    End Sub 
    

    如果有人知道更好的解决方案或为什么需要这样做,我会很好奇。

    【讨论】:

      【解决方案2】:

      这可能不是一个理想的解决方案,但我决定走这条路:

      制作一个包含以下宏的excel书:

      Sub Open_Outlook()
      
      Shell ("OUTLOOK")
      
      End Sub
      

      然后,您可以制作一个 VBS 脚本来打开该 Excel 工作簿并运行打开 Outlook 的宏。

      不优雅,但实用:)


      Dim objExcel
      Set objExcel = CreateObject("Excel.Application")
      
      'the line below opens an excel book
      
      objExcel.Workbooks.Open("C:\Users\bal01483\Desktop\AUTOMATION\EMAIL\OutlookBook.xlsm") 
      
      objExcel.Visible = True
      
      'the line below runs a macro inside the excel book which will open outlook
      
      objExcel.Run "ThisWorkbook.Open_Outlook"  'this macro opens opens outlook
      
      objExcel.Quit
      
      '''''''''''''''''''''''''''''''
      ''BEGIN OUTLOOK EMAIL SEGMENT''
      '''''''''''''''''''''''''''''''
      

      【讨论】:

        猜你喜欢
        • 2023-03-06
        • 1970-01-01
        • 2022-08-03
        • 1970-01-01
        • 1970-01-01
        • 2011-03-27
        • 1970-01-01
        • 1970-01-01
        • 2015-01-05
        相关资源
        最近更新 更多