Word 和Excel中如何把当前文档作为附件进行邮件发送
Office中,必须安装了Outlook之后,才能把当前文档作为附件方式进行发送,而如果没有安装Outlook,那么就没有办法作为附件发送了,因此我们需要一个简单的方式来达到这个实用的功能!
使用附件的模版可以轻松做到。
安装方法:把 发送附件.dot 解压缩,存储到Word的Startup目录,解压缩 sendmail.xla 到某个目录,然后启动Excel,使用 工具-->模版和加载项,浏览,加载这个模版即可。
核心代码如下:
Declare Function MAPISendDocuments Lib "mapi32.dll" (ByVal UIParam As Long, ByVal FileDelimChar As String, ByVal FilePaths As String, ByVal Subject As String, ByVal Reserved As Long) As Integer
Sub 发送附件()
On Error Resume Next
If Workbooks.Count = 0 Then
MsgBox CSAbout & "当前没有打开的文件,无法发送邮件。", vbOKOnly + vbExclamation
Else
ActiveDocument.Save
MAPISendDocuments 0, ";", ActiveWorkbook.Path + "\" + ActiveWorkbook.Name, "请查收附件:" + ActiveWorkbook.Name, 0
End If
End Sub
Const CSToolbarName = "附件"
Const CSBuiltInMailID = 3738
Private Sub Workbook_AddinInstall()
On Error Resume Next
Dim a, b
Set a = Application.CommandBars("Standard").FindControl(msoControlButton, CSBuiltInMailID)
Set b = Application.CommandBars("Standard").Controls(CSToolbarName)
If b Is Nothing Then
Set b = Application.CommandBars("Standard").Controls.Add(msoControlButton, , , a.Index + 1)
With b
.OnAction = "发送附件"
.Style = msoButtonIconAndCaption
.Caption = CSToolbarName
.FaceId = a.FaceId
End With
End If
End Sub
Private Sub Workbook_AddinUninstall()
On Error Resume Next
Application.CommandBars("Standard").Controls(CSToolbarName).Delete
End Sub
Declare Function MAPISendDocuments Lib "mapi32.dll" (ByVal UIParam As Long, ByVal FileDelimChar As String, ByVal FilePaths As String, ByVal Subject As String, ByVal Reserved As Long) As Integer
Sub 发送附件()
On Error Resume Next
If Documents.Count = 0 Then
MsgBox CSAbout & "当前没有打开的文件,无法发送邮件。", vbOKOnly + vbExclamation
Else
ActiveDocument.Save
MAPISendDocuments 0, ";", ActiveDocument.Path + "\" + ActiveDocument.Name, "请查收附件:" + ActiveDocument.Name, 0
End If
End Sub
Const CSToolbarName = "附件"
Const CSBuiltInMailID = 3738
Private Sub AutoExec()
On Error Resume Next
Dim a, b
Set a = Application.CommandBars("Standard").FindControl(msoControlButton, CSBuiltInMailID)
Set b = Application.CommandBars("Standard").Controls(CSToolbarName)
If b Is Nothing Then
Set b = Application.CommandBars("Standard").Controls.Add(msoControlButton, , , a.Index + 1)
With b
.OnAction = "发送附件"
.Style = msoButtonIconAndCaption
.Caption = CSToolbarName
.FaceId = a.FaceId
End With
End If
End Sub
Private Sub AutoExit()
On Error Resume Next
Application.CommandBars("Standard").Controls(CSToolbarName).Delete
End Sub