通过VBScript打开并激活Outlook

时间:2016-02-09 16:55:22

标签: vbscript outlook focus

我使用VBS控制一个进程,我需要打开Outlook并激活/设置焦点在窗口上。我在设置窗口焦点时遇到了问题 - 当它运行时,窗口焦点仍然在我打开的资源管理器窗口中双击并在VBS文件上运行。

从我读过的内容来看,打开一个新的Outlook实例应该成为焦点,如果我在没有关注资源管理器窗口的情况下运行脚本(例如使用Sendkeys),它的工作原理非常好,但它没有如果Explorer窗口具有焦点,则工作。这很重要,因为它将通过任务计划程序设置来运行,因此无论当任务运行时当前的焦点位于何处都需要工作。

这是现有的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

2 个答案:

答案 0 :(得分:0)

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

Shell 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 

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

答案 1 :(得分:0)

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

制作包含以下宏的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''
'''''''''''''''''''''''''''''''