简体   繁体   English

VBA脚本未在Outlook 2007中运行

[英]VBA scripts not running in Outlook 2007

I placed a new VBA script to autobcc myself on every message that is sent through Outlook 2007. The code seems to work great. 我放置了一个新的VBA脚本,以便自动处理通过Outlook 2007发送的每封邮件。该代码似乎很好用。 The problem I have is that every time I restart my computer and load Outlook 2007 the code is not engaged until after I open the VBA editor. 我的问题是,每次我重新启动计算机并加载Outlook 2007时,直到打开VBA编辑器后,代码才会使用。 It will continue to run properly this way even if I close the editor. 即使关闭编辑器,它也将继续以这种方式正常运行。 Do I have to open and then close the VBA editor every time I open Outlook 2007? 每次打开Outlook 2007时都必须打开然后关闭VBA编辑器吗? Is there a way to force VBA scripts to engage when Outlook 2007 is opened and loaded? 有没有一种方法可以在打开和加载Outlook 2007时强制使用VBA脚本?

Here is the script I'm using: 这是我正在使用的脚本:

Private Sub Application_ItemSend(ByVal Item As Object, _
                                 Cancel As Boolean)
    Dim objRecip As Recipient
    Dim strMsg As String
    Dim res As Integer
    Dim strBcc As String
    On Error Resume Next

    ' #### USER OPTIONS ####
    ' address for Bcc -- must be SMTP address
    ' or resolvable to a name in the address book
    strBcc = "email@domain.com"

    Set objRecip = Item.Recipients.Add(strBcc)
    objRecip.Type = olBCC
    If Not objRecip.Resolve Then
        strMsg = "Could not resolve the Bcc recipient. " & _
                "Do you want to send the message?"
        res = MsgBox(strMsg, vbYesNo + vbDefaultButton1, _
                "Could Not Resolve Bcc")
        If res = vbNo Then
            Cancel = True
        End If
    End If

    Set objRecip = Nothing
End Sub

First, check your macro security settings in Outlook. 首先,在Outlook中检查您的宏安全设置。 If they are set to " No warnings and disable all macros " (the highest security setting), you can still run code directly from the VBA IDE, but because macros are disabled, your code won't run from Outlook's explorer when the VBA IDE is not open. 如果将它们设置为“ 无警告并禁用所有宏 ”(最高安全性设置),您仍然可以直接从VBA IDE运行代码,但是由于禁用了宏,因此当VBA IDE时,您的代码将无法从Outlook的资源管理器中运行不开放。

If your security setting is low enough to allow macros to run, then the next step is to try and nail down where the error occurs. 如果您的安全设置足够低,无法运行宏,那么下一步就是尝试确定发生错误的位置。 Start by removing the " On Error Resume Next " line (that's a horrible thing to do, BTW), and replace it with " On Error GoTo ErrTrap ". 首先删除“ On Error Resume Next ”行(这是一件可怕的事,顺便说一句),然后将其替换为“ On Error GoTo ErrTrap ”。

Then, write a subroutine called ErrTrap() to process the error. 然后,编写一个名为ErrTrap()的子例程来处理该错误。 You can use the Err.Number and Err.Description objects to feed a meaningful error message into a MessageBox. 您可以使用Err.NumberErr.Description对象将有意义的错误消息输入到MessageBox中。 Post the message here, and we may be able to help you troubleshoot from there. 在此处发布消息,我们也许可以帮助您从那里进行故障排除。

Cheers, 干杯,

-=Cameron -=卡梅隆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM