2011-09-27 65 views
0

我有一段脚本,随着时间的推移拼凑起来。最近我们需要在Outlook中使用一个插件导致了一些问题。基本上我们会得到两次提示,因为当使用插件时,电子邮件最终会被销毁并用特定的附件文件名重新创建。此时会再次提示用户。我试图在For循环中工作,如果它找到了这个附件,就跳过脚本。但是,当我添加For循环时,它似乎忽略了整个脚本。我对VBA的经验有限,所以我确信这是我的语法或用法的问题。请参见下面的脚本:VBA展望脚本

Private Sub Application_ItemSend _ 
(ByVal Item As Object, Cancel As Boolean) 
Dim strMsg As String 
Dim Atmt As Variant 

'strMsg = Item.Class 
If Item.Class = "43" Then 
    For Each Atmt In Item.Attachments 
     If VBA.Right(Atmt.FileName, 3) = ".sf" Then 
      GoTo NonEmailError 
     End If 
    Next Atmt 
    If Item.CC = "" Then 
     strMsg = "To recipients: " & Item.To & vbCrLf & _ 
     "Are you sure you want to send this message?" 
    Else 
     strMsg = "To recipients: " & Item.To & vbCrLf & _ 
     "Cc recipients: " & Item.CC & vbCrLf & _ 
     "Bcc recipients: " & Item.BCC & vbCrLf & _ 
     "Are you sure you want to send this message?" 
    End If 
Else 
    GoTo NonEmailError 
End If 
' Exit Sub 

' Ignore errors for now. 
On Error GoTo NonEmailError 

' Prompt user to fill in subject 
If Item.Subject = "" Then 
    MsgBox "You must enter a subject.", 48, "Empty Subject" 
    Cancel = True 
    GoTo NonEmailError 
End If 
' Exit Sub 

' Prompt user to verify E-Mails 
If MsgBox(strMsg, vbYesNo + vbQuestion _ 
    , "Send Confirmation") = vbNo Then 
    Cancel = True 
End If 
Exit Sub 

NonEmailError: 
' The item being sent was not an e-mail and so don't prompt the user anything 
Exit Sub 

End Sub 
+0

有几点意见:1)不管电子邮件是否有附件,这两个提示都会显示出来吗? 2)哪一行是与第一个提示相关联的,哪一行是第二个? 3)它可能是一个好主意,而不是一个'对象'的工作,'暗淡olMI作为Outlook.MailItem ---如果TypeName(Item)=“MailItem”然后---设置olMI =项目---'做你的东西---如果“。 –

回答

0

我不是那个熟悉的Outlook和VBA,但我在这种情况下,第一个猜测是,item.class应该是比较数值43而不是字符串字面值"43"

让我停下来的一件事是,我期望该语句会抛出错误,因为"43"对于Item.Class是错误的类型,但这可能只是反映了我对材质的陌生性。

根据MSDN参考,item.class(在这种情况下的MailItem)是OlObjectClass常数,43olMail。或许,这将是明智的改变:

If Item.Class = "43" Then

If Item.Class = olMail Then

,或者至少:

If Item.Class = 43 Then

(参见:http://msdn.microsoft.com/en-us/library/bb208118%28v=office.12%29.aspxhttp://msdn.microsoft.com/en-us/library/bb207137%28v=office.12%29.aspx

+0

在创建“For循环”来测试附件类型之前,脚本工作正常,类设置为43. – Untalented

+0

@Untalented - 即使如此,我也会同意horatio。我建议使用'Item.Class'或'TypeName(Item)'测试一个'Outlook.MailItem'而不是'Object',看到我对你的文章的评论。 –