vba 使用 VBScript 调用 Outlook 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21179750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:25:25  来源:igfitidea点击:

Call Outlook procedure using VBScript

vbavbscriptoutlook-vba

提问by L42

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

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

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.

上面的代码工作正常。

Question:

题:

I want to use Task Schedulerto 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 vbscriptso 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 .Runproperty.

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

So this doesn't work:

所以这不起作用:

Dim olApp

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

Set olApp = Nothing

I've also read about the Session.Logonlike this:

我也读过Session.Logon这样的:

Dim olApp

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

Set olApp = Nothing

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

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

SOLUTION:

解决方案:

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

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

popup

弹出

1st one:is as KazJaw Pointed out.

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

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

假设您有另一个程序(例如 Excel、VBScript),其中包括通过Outlook程序发送邮件。
而不是使用.Send,只是.Save邮件。
它将保存在Outlook's Draft文件夹中。
然后使用下面的代码,发送使用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

Above code fires when Task Remindershows with a subject "Send Draft".
But, we don't want it showing since the whole point is just to call the SendMailprocedure.
So we added a procedure that Cancelsthe display of reminder which is of olTaskclass or TaskItemType.

Task Reminder显示主题为“发送草稿”时,上面的代码会触发。
但是,我们不希望它显示出来,因为重点只是调用SendMail过程。
所以我们添加了一个程序,Cancels显示olTask类或TaskItem类型的提醒。

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

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

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

第二个:就是在Allow弹出安全弹窗时,使用API​​编程点击按钮。
感谢 SiddarthRout 的帮助。
这是LINK,它将帮助您以编程方式单击Allow按钮。
当然,你必须稍微调整一下。

采纳答案by Kazimierz Jawor

Tried & Tested!

尝试和测试!

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

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

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

  2. go to VBA Editor, open ThisOutlookSession moduleand 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
    
  1. 在 Outlook 中添加一个新任务,设置主题为:“运行宏 YourMacroName”并设置宏应该启动的时间(加上周期)。

  2. 转到 VBA 编辑器,打开ThisOutlookSession module并在其中添加以下代码(并查看代码中的注释):

    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
    

回答by Siddharth Rout

Where will I put the procedure in Outlook, Module or ThisOutlookSession?

我将把程序放在 Outlook、Module 或 ThisOutlookSession 中的什么位置?

Neither. Paste the below code in a Text File and save it as a .VBSfile. Then call this VBS file from the Task Scheduleras shown HERE

两者都不。将以下代码粘贴到文本文件中并将其另存为.VBS文件。然后调用从这个VBS文件任务计划程序,如图HERE

Dim olApp, olNS, olFolder, olDraft, strfoldername, i

Set olApp = GetObject(, "Outlook.Application")
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(6)

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

回答by NBSTL68

If you are using Outlook 2007 or newer I have found you can easily eliminate the security pop up you mentioned above when running your script by doing the following:

如果您使用的是 Outlook 2007 或更新版本,我发现您可以通过执行以下操作轻松消除您在运行脚本时上面提到的安全弹出窗口:

  1. In Outlook 2007 Trust Center, go to Macro Security - Select "No security Check for macros"

  2. In Outlook 2007 Trust Center, go to Programatic Access - Select "Never warn me abous suspicious activity.

  1. 在 Outlook 2007 信任中心中,转到宏安全性 - 选择“对宏不进行安全检查”

  2. 在 Outlook 2007 信任中心,转到程序访问 - 选择“从不警告我关于可疑活动。

Of course that technically leaves you open to the remote possibility for someone to email you some malicious email script or something of that nature I assume. I trust my company has that managed though and this works for me. I can use VBS scripts in Outlook, Access, Excel to send emails with no security pop up.

当然,从技术上讲,这让您对某人向您发送一些恶意电子邮件脚本或我假设的那种性质的东西的远程可能性持开放态度。我相信我的公司已经做到了这一点,这对我有用。我可以在 Outlook、Access、Excel 中使用 VBS 脚本来发送没有安全弹出窗口的电子邮件。

Another Option:

另外一个选项:

If you don't want to do that, another option that has worked well for me prior to this is here: http://www.dimastr.com/redemption/objects.htm

如果您不想这样做,在此之前对我来说效果很好的另一个选项是:http: //www.dimastr.com/redemption/objects.htm

Basically a dll redirect that does not include the popup. It leaves your other default security in place and you write \ call your VBA for it and send mail without the secutity pop-ups.

基本上是一个不包括弹出窗口的 dll 重定向。它保留了您的其他默认安全性,然后您编写\ 调用您的 VBA 并发送邮件而没有安全弹出窗口。