通过 VBScript 打开并激活 Outlook
Open and Activate Outlook via VBScript
我正在使用 VBS 来控制一个进程,其中我需要打开 Outlook 并且 activate/set 关注 window。我 运行 正在解决将焦点设置在 window 上的问题 - 当它 运行 时,window 焦点仍然在我拥有的资源管理器 window 上打开以双击并 运行 VBS 文件。
根据我的阅读,打开一个新的 Outlook 实例应该获得焦点,如果我 运行 脚本没有关注资源管理器 window(例如使用 Sendkeys)它工作得很好,但如果资源管理器 window 有焦点,它就不起作用。这很重要,因为它将通过 Task Scheduler 设置为 运行,因此当任务 运行s.
时,无论当前焦点在哪里,它都需要工作
这是现有的 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
因此,实验找到了解决方法 - 将 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 ("Activation") 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
如果有人知道更好的解决方案或为什么需要这样做,我仍然很好奇。
这可能不是一个理想的解决方案,但我决定走这条路:
制作一本 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''
'''''''''''''''''''''''''''''''
我正在使用 VBS 来控制一个进程,其中我需要打开 Outlook 并且 activate/set 关注 window。我 运行 正在解决将焦点设置在 window 上的问题 - 当它 运行 时,window 焦点仍然在我拥有的资源管理器 window 上打开以双击并 运行 VBS 文件。
根据我的阅读,打开一个新的 Outlook 实例应该获得焦点,如果我 运行 脚本没有关注资源管理器 window(例如使用 Sendkeys)它工作得很好,但如果资源管理器 window 有焦点,它就不起作用。这很重要,因为它将通过 Task Scheduler 设置为 运行,因此当任务 运行s.
时,无论当前焦点在哪里,它都需要工作这是现有的 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
因此,实验找到了解决方法 - 将 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 ("Activation") 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
如果有人知道更好的解决方案或为什么需要这样做,我仍然很好奇。
这可能不是一个理想的解决方案,但我决定走这条路:
制作一本 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''
'''''''''''''''''''''''''''''''