2015-12-04 80 views
1

我有一個鎖定的Word形式,只提供,用戶可以填寫字段當他們到達終點,有一個提交按鈕,這是用下面的代碼的Active X控件:如何在提交Word表單後添加成功消息? 。

Private Sub CommandButton1_Click() 
Dim bStarted As Boolean 
Dim oOutlookApp As Outlook.Application 
Dim oItem As Outlook.MailItem 

On Error Resume Next 

If Len(ActiveDocument.Path) = 0 Then 
    MsgBox "Document needs to be saved first" 
    Exit Sub 
End If 

Set oOutlookApp = GetObject(, "Outlook.Application") 
If Err <> 0 Then 
    Set oOutlookApp = CreateObject("Outlook.Application") 
    bStarted = True 
End If 

Set oItem = oOutlookApp.CreateItem(olMailItem) 

With oItem 
    .To = "email address" 
    .Subject = "New subject" 
    'Add the document as an attachment, you can use the .displayname property 
    'to set the description that's used in the message 
    .Attachments.Add Source:=ActiveDocument.FullName, Type:=olByValue, _ 
     DisplayName:="Document as attachment" 
    .Send 
End With 

If bStarted Then 
    oOutlookApp.Quit 
End If 

Set oItem = Nothing 
Set oOutlookApp = Nothing 

End Sub 

我將Outlook庫添加到引用中,因此,當我單擊該按鈕時,它會按照預期以Word文檔作爲附件發送電子郵件。問題是沒有告訴用戶它已經工作的消息。我擔心用戶只會一遍又一遍地按下按鈕發送多封電子郵件。我通常不會做VBA,但是對於我目前的任務來說這是必要的,所以任何幫助都將值得讚賞。

+1

'MsgBox'你的消息是如何發送的? – xidgel

回答

5

在我看來,在過程的末尾添加一個MsgBox行應該可以完成這項工作。例如:

MsgBox "Document sent as e-mail" 

如果需要,可以格式化消息,如更改標題,按鈕和圖標。查看VBA語言幫助中的方法以獲取更多信息。

我也看到您的代碼可能存在問題。在開始時你有:

On Error Resume Next 

這基本上會關閉所有錯誤 - 如果出現任何錯誤,將不會有通知。這通常是一個非常糟糕的想法。如果使用,它應該只是幾行,然後你要麼恢復正常的錯誤通知或打開錯誤處理。我假設它最初在代碼中是關於啓動Outlook應用程序的部分。這很好,這是或多或少的標準......但是結束後如果要添加一行如:

On Error GoTo 0 
1

可以(「消息發送」)後,或。發送後ooutlookapp.quit添加一個MsgBox

相關問題