2016-03-06 71 views
2

我必須通過郵件每月創建併發送excel文件給我的老闆。 我想用VBA代碼作爲附件發送文件。但我的VBA代碼不起作用,並在確認後要求調試。如何通過電子郵件發送excel文件

我的代碼是: -

Sub EMail() 
ActiveWorkbook.SendMail Recipients:="[email protected]" 
End Sub 
+0

https://msdn.microsoft.com/en-in/library/office/ff869553.aspx – newguy

+0

請說明您是「不工作」和「確認後索要調試」的意思。此代碼適用於我。 –

回答

0

這裏是如何應對的信用是由於發送活動工作簿作爲附件

Option Explicit 
Sub EmailFile() 
    Dim olApp As Object 
    Dim olMail As Object 
    Dim olSubject As String 

' // Turn off screen updating 
    Application.ScreenUpdating = False 

    Set olApp = CreateObject("Outlook.Application") 
    Set olMail = olApp.CreateItem(olMailItem) 

    olSubject = "This Subject Line" 

    With olMail 
     .Display 
    End With 

    With olMail 
     .To = "[email protected]" 
     .CC = "" 
     .BCC = "" 
     .Subject = olSubject 
     .HTMLBody = "This Body Text " & .HTMLBody 
     .Attachments.Add ActiveWorkbook.FullName 
     '.Attachments.Add ("C:\test.txt") ' add other file 
'  .Send 'or use .Display 
     .Display 
    End With 

' // Restore screen updating 
    Application.ScreenUpdating = True 

    Set olMail = Nothing 
    Set olApp = Nothing 

End Sub 
0

您可以使用如下例所示的VBA代碼片段:

Sub SendEmailWithAttachment() 
Dim myItem As Outlook.MailItem 
Dim myAttachments As Outlook.Attachments 

Set myItem = Application.CreateItem(olMailItem) 
Set myAttachments = myItem.Attachments 
myAttachments.Add "C:\MyExcelFile.xls", olByValue, 1, "Test" 
myItem.To = "Recipient Address" 
myItem.Send 

'alternatively, you may display the item before sending 
'myItem.Display 
End Sub 

希望這有助於。

+0

對不起,不能解決相同的問題打開調試。 –

2

信用的例子...這是直接從rondebruin網站。

Sub Mail_workbook_Outlook_1() 
'Working in Excel 2000-2016 
'This example send the last saved version of the Activeworkbook 
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm 
    Dim OutApp As Object 
    Dim OutMail As Object 

    Set OutApp = CreateObject("Outlook.Application") 
    Set OutMail = OutApp.CreateItem(0) 

    On Error Resume Next 
    With OutMail 
     .to = "[email protected]" 
     .CC = "" 
     .BCC = "" 
     .Subject = "This is the Subject line" 
     .Body = "Hi there" 
     .Attachments.Add ActiveWorkbook.FullName 
     'You can add other files also like this 
     '.Attachments.Add ("C:\test.txt") 
     .Send 'or use .Display 
    End With 
    On Error GoTo 0 

    Set OutMail = Nothing 
    Set OutApp = Nothing 
End Sub