2015-03-03 109 views
0

我想開始在Excel中創建一個宏,它允許我發送帶有活動行內容的電子郵件(例如,單擊一行,選擇其所有內容) 。我需要從該行上的特定單元格獲取電子郵件,並將其分配給「發送電子郵件到」字段。主題字段應該有一個標題並在其末尾添加特定單元格的數據(在選定的行上)。在正文中,我需要包含選定行的內容。在選擇每個單元格的內容之前,我需要輸入標題。我可以直接通過Excel發送電子郵件,還是至少可以通過打開Outlook發送電子郵件並使用宏準備好所有數據?Excel宏從活動行發送電子郵件信息

這是我需要什麼(在電子表格中顯示):

FirstName LastName PhoneNumber 
Dan  Daniels 123 
Jim  Jameson 321 

如果第二行是選擇行。電子郵件應該是這樣的:

To: [email protected] 
Subject: Random text - Dan Daniels 
Body: 

Hello, 

FirstName: Dan 
LastName: Daniels 
PhoneNumber: 123 

任何建議我應該如何開始構建這個宏?謝謝!!!

更新!

下面是我修改了代碼,並給了我編譯錯誤:

Sub SendMail() 
Dim OutlookApp As Object: Set OutlookApp =  CreateObject("Outlook.Application") 
Dim var As Variant: var = Selection.Value 
Set MyMail = OutlookApp.CreateItem(0) 
     With MyMail 
       .To = var(1, 35) 
       .Subject = "Email for Random Text Text Text Here -" & " " & var(1, 17) & " " & var(1, 18) 
       .body = "This is an email informing you that random text aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & vbNewLine & vbNewLine & "Created by: " & var(1, 4) & vbNewLine & "Date Information Posted: " & var(1, 6) & vbNewLine & "Random Data One Showing: " & var(1, 28) & vbNewLine & "Another Random Data: " & var(1, 29) & vbNewLine & "Requester Random First Name: " & var(1, 13) & vbNewLine & "Requester Random Last Name: " & var(1, 14) & vbNewLine & "Requester Random Here E-mail: " & var(1, 15) & vbNewLine & "Requester Random Phone Number: " & var(1, 16) & vbNewLine & "Person's Random First Name: " & var(1, 17) & vbNewLine & "Person's Random Last Name: " & var(1, 18) & vbNewLine & "Person's Random Location: " & var(1, 21) & vbNewLine & "Person's Random Occupation: " & var(1, 22) & vbNewLine & "Person's Random Hometown: " & var(1, 23) & vbNewLine & "Where he is located: " & var(1, 24) & vbNewLine & "PErson's Email Address: " & var(1, 19) & vbNewLine & "Number of items requested: " & var(1, 8) & vbNewLine & "Requested Items Number: " & var(1, 31) & vbNewLine & "Name of Item: " & var(1, 22) & vbNewLine & vbNewLine & "This is a random text I need to add at the end of the mail, text being quite looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooog random text random text random text random text random text random text" 
      End With 
      MyMail.send 
End Sub 

從列中的數據只是簡單的字符,名稱,數量或emails.And其實我有35列,但只需要上述那些。謝謝!

回答

1

那麼,我會推薦第三欄的人的電子郵件。

讓我們假設你已經同意補充。你的數據會看起來像:

FirstName LastName PhoneNumber Email 
Dan  Daniels 123   [email protected] 

我們將創建一個Outlook應用程序對象(使用後綁定)

Dim OutlookApp As Object: Set OutlookApp = CreateObject("Outlook.Application") 

,現在我們需要填寫MyMail對象的,我們」屬性重新創建Outlook。我們正在使用選擇而不是活動單元格。

Dim var as variant: var = selection.value 
Set MyMail = OutlookApp.CreateItem(0) 

現在的填充性能:

 With MyMail 
       .To = var(1,4) 
       .Subject = "Random Text" &" "& var(1,1) & " " & var(1,2) 
       .body = "Hello," & vbNewLine & "First name: " & var(1, 1) & vbNewLine & "Last Name: " & var(1, 1) & vbNewLine & "Phone number: " & var(1, 2) 
      End With 

,我們將用我們的MyMail對象的發送方法發送此。

  MyMail.send 

編輯:下面是完整的子代碼。我測試了它,它工作。

Sub SendMail() 
Dim OutlookApp As Object: Set OutlookApp = CreateObject("Outlook.Application") 
Dim var As Variant: var = Selection.Value 
Set MyMail = OutlookApp.CreateItem(0) 
      With MyMail 
        .To = var(1, 4) 
        .Subject = "Random Text" & " " & var(1, 1) & " " & var(1, 2) 
        .body = "Hello," & vbNewLine & "First name: " & var(1, 1) & vbNewLine & "Last Name: " & var(1, 2) & vbNewLine & "Phone number: " & var(1, 3) 
       End With 
       MyMail.send 
End Sub 

前提條件:

運行宏之前選擇的行。這是我的數據的預覽:

enter image description here

我的Outlook發件箱:

enter image description here

EDIT1:這是正確的.body屬性:

.body = "This is an email informing you that random text aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & vbNewLine & vbNewLine & "Created by: " & Var(1, 4) & vbNewLine & "Date Information Posted: " & Var(1, 6) & vbNewLine & "Random Data One Showing: " & Var(1, 28) & vbNewLine & "Another Random Data: " & Var(1, 29) & vbNewLine & _ 
"Requester Random First Name: " & Var(1, 13) & vbNewLine & "Requester Random Last Name: " & Var(1, 14) & vbNewLine & "Requester Random Here E-mail: " & Var(1, 15) & vbNewLine & "Requester Random Phone Number: " & Var(1, 16) & vbNewLine & "Person's Random First Name: " & Var(1, 17) & vbNewLine & "Person's Random Last Name: " & Var(1, 18) & vbNewLine & "Person's Random Location: " & Var(1, 21) & vbNewLine & "Person's Random Occupation: " & _ 
Var(1, 22) & vbNewLine & "Person's Random Hometown: " & Var(1, 23) & vbNewLine & "Where he is located: " & Var(1, 24) & vbNewLine & "PErson's Email Address: " & Var(1, 19) & vbNewLine & "Number of items requested: " & Var(1, 8) & vbNewLine & "Requested Items Number: " & Var(1, 31) & vbNewLine & "Name of Item: " & Var(1, 22) & vbNewLine & vbNewLine & _ 
"This is a random text I need to add at the end of the mail, text being quite looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooog random text random text random text random text random text random text" 
+0

謝謝!我是從這些事情開始的,但是我想我必須在Excel的宏工具中創建這個宏。關於數據,我有三個以上的列,我只是將這些列作爲示例添加,以便我可以擴展其餘部分的代碼。我會用你提供的代碼嘗試一些東西,然後回頭看看我可以如何處理它。再次感謝! – 2015-03-03 13:57:42

+0

沒問題@DantèsCristo!我們一起解決吧! :) – 2015-03-03 14:00:46

+0

我試圖在VDB中創建一個宏,但是當試圖保存您提供給我的確切代碼時,它給了我這個錯誤:以下功能無法保存在無宏工作簿中: VB Project To使用這些功能保存文件,單擊否,然後在「文件類型」列表中選擇啓用宏的文件類型。任何想法我做錯了什麼?我啓用了宏。 – 2015-03-04 11:32:44