我有一个锁定的Word表单,只有用户可以填写的字段.当它们到达结尾时,有一个提交按钮,它是一个活动的x控件,代码如下:
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
Run Code Online (Sandbox Code Playgroud)
我将Outlook库添加到引用中,因此当我单击按钮时,它会按预期发送电子邮件,并将Word文档作为附件发送.问题是没有消息告诉用户它已经工作.我担心用户会一次又一次地点击按钮发送多封电子邮件.我通常不会做VBA,但这对我目前的任务来说是必要的,所以任何帮助都会受到赞赏.
在我看来,在程序结束时添加一个MsgBox行应该可以完成这项工作.例如:
MsgBox "Document sent as e-mail"
Run Code Online (Sandbox Code Playgroud)
如果您愿意,可以格式化消息,例如更改标题,按钮和图标.有关详细信息,请在VBA语言帮助中查找该方法.
我还发现您的代码可能存在问题.在一开始你有:
On Error Resume Next
Run Code Online (Sandbox Code Playgroud)
这基本上会关闭所有错误 - 如果出现任何问题,将不会发出通知.这通常是一个非常糟糕的想法.如果使用它,它应该只有几行,然后你要么恢复正常的错误通知,要么打开错误处理.我假设它最初在您的代码中有关启动Outlook应用程序的部分.那很好,它或多或少标准......但是在结束之后如果你应该添加一行如:
On Error GoTo 0
Run Code Online (Sandbox Code Playgroud)