2011-09-27 66 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',看到我對你的文章的評論。 –