使用 VBScript 调用 Outlook 程序 [英] Call Outlook procedure using VBScript

查看:63
本文介绍了使用 VBScript 调用 Outlook 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Outlook 中有一个程序,可以发送 Drafts 文件夹中所有保存的邮件.
代码如下:

I have a procedure in Outlook that sends all the saved messages in Drafts folder.
Below is the code:

Public Sub SendMail()

Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim olDraft As Outlook.MAPIFolder
Dim strfoldername As String
Dim i As Integer

Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(olFolderInbox)

strfoldername = olFolder.Parent

Set olDraft = olNS.Folders(strfoldername).Folders("Drafts")

If olDraft.Items.Count <> 0 Then
    For i = olDraft.Items.Count To 1 Step -1
        olDraft.Items.Item(i).Send
    Next
End If

End Sub

以上代码工作正常.

Above code works fine.

问题:

我想使用 Task Scheduler 在指定时间触发此过程.
1. 我将把程序放在 Outlook、Module 或 ThisOutlookSession 中的什么位置?
2.我不擅长vbscript所以我也不知道如何编码它来调用Outlook程序.我已经调用了 Excel 程序,但 Outlook 不支持 .Run 属性.

I want to use Task Scheduler to fire this procedure as a specified time.
1. Where will I put the procedure in Outlook, Module or ThisOutlookSession?
2. I am not good in vbscript so I also don't know how to code it to call the Outlook Procedure. I've done calling Excel Procedure but Outlook doesn't support .Run property.

所以这不起作用:

Dim olApp

Set olApp = CreateObject("Outlook.Application")
olApp.Run "ProcedureName"

Set olApp = Nothing

我也读过这样的 Session.Logon:

Dim olApp

Set olApp = CreateObject("Outlook.Application")
olApp.Session.Logon
olApp.ProcedureName

Set olApp = Nothing

但它抛出错误,说不支持对象 ProcedureName.
希望有人能有所启发.

But it throws up error saying object ProcedureName is not supported.
Hope somebody can shed some light.

解决方案:

好的,我想出了 2 个解决方法来避免或通过此弹出窗口.

Ok, I've figured out 2 work around to Avoid or get pass this pop-up.

第一个:正如 KazJaw 指出的那样.

1st one: is as KazJaw Pointed out.

假设您有另一个程序(例如 Excel、VBScript),其中包括在程序中通过 Outlook 发送邮件.
而不是使用 .Send,只需 .Save 邮件.
它将保存在 Outlook's Draft 文件夹中.
然后使用下面的代码,发送使用 Outlook Task Reminder 触发的草稿.

Assuming you have another program (eg. Excel, VBScript) which includes sending of mail via Outlook in the procedure.
Instead of using .Send, just .Save the mail.
It will be saved in the Outlook's Draft folder.
Then using below code, send the draft which fires using Outlook Task Reminder.

Option Explicit
Private WithEvents my_reminder As Outlook.Reminders

Private Sub Application_Reminder(ByVal Item As Object)

Dim myitem As TaskItem

If Item.Class = olTask Then 'This works the same as the next line but i prefer it since it automatically provides you the different item classes.
'If TypeName(Item) = "TaskItem" Then
    Set my_reminder = Outlook.Reminders
    Set myitem = Item
    If myitem.Subject = "Send Draft" Then
        Call SendMail
    End If
End If

End Sub

Private Sub my_reminder_BeforeReminderShow(Cancel As Boolean)

Cancel = True
Set my_reminder = Nothing

End Sub

Task Reminder 显示主题为发送草稿"时,将触发上述代码.
但是,我们不希望它显示出来,因为重点只是调用 SendMail 过程.
所以我们添加了一个程序,CancelsolTask​​ 类或TaskItem 类型的提醒的显示.

Above code fires when Task Reminder shows with a subject "Send Draft".
But, we don't want it showing since the whole point is just to call the SendMail procedure.
So we added a procedure that Cancels the display of reminder which is of olTask class or TaskItem Type.

这当然要求 Outlook 正在运行.
您可以像我一样保持它运行 24 小时,或者创建一个 VBscript 来打开它以通过 Task Scheduler 进行调度.

This requires that Outlook is running of course.
You can keep it running 24 hours as i did or, create a VBscript that opens it to be scheduled via Task Scheduler.

第二个:是在安全弹出窗口出现时使用API​​以编程方式点击允许按钮.
感谢 SiddarthRout 的帮助.
这是链接 这将帮助您以编程方式单击 允许 按钮.
当然,你必须稍微调整一下.

2nd one: is to use API to programatically click on Allow button when the security pop-up appears.
Credits to SiddarthRout for the help.
Here is the LINK which will help you programmatically click on the Allow button.
Of course you have to tweak it a bit.

推荐答案

尝试 &已测试!

假设您始终运行 Outlook 应用程序(根据您问题下方的评论),您可以按照以下步骤执行所需操作:

Assuming that you have Outlook Application always running (according to comment below your question) you can do what you need in the following steps:

  1. 在 Outlook 中添加一个新任务,将主题设置为:运行宏 YourMacroName"并设置宏应该启动的时间(加上周期).

  1. add a new task in Outlook, set subject to: "run macro YourMacroName" and set time (plus cycles) when your macro should start.

进入 VBA 编辑器,打开 ThisOutlookSession 模块 并在其中添加以下代码(并查看代码内的注释):

go to VBA Editor, open ThisOutlookSession module and add the following code inside (plus see the comments inside the code):

Private Sub Application_Reminder(ByVal Item As Object)

If TypeName(Item) = "TaskItem" Then
    Dim myItem As TaskItem
    Set myItem = Item
    If myItem.Subject = "run macro YourMacroName" Then

        Call YourMacroName    '...your macro name here

    End If
End If
End Sub

这篇关于使用 VBScript 调用 Outlook 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆