2012-08-25 114 views
0

我有以下excel vba代碼,它將通過電子郵件發送我的工作表中的一系列地址。 Howeve,我正在尋找可能使用輸入框來確定我想要發送電子郵件的範圍。我碰到的麻煩是讓輸入變成函數mailid理解的值。有什麼建議麼?excel vba inputbox

Sub EmailActiveSheetWithOutlook2() 

Dim oApp, oMail As Object, _ 
tWB, cWB As Workbook, _ 
FileName, FilePath As String 

Application.ScreenUpdating = False 

'Set email id here, it may be a range in case you have email id on your worksheet 

Sheets("Sheet1").Select 
mailId = Range("b4:b5").Value 


'Write your email message body here , add more lines using & vbLf _ at the end of each line 

Body = "Hello, it appears you have not yet filled out the transportation contact information excel sheet. This sheet was emailed to you, please complete this and send to me saved as your firstnamelastname.xls at your earliest convience." & vbLf _ 
& vbLf _ 
& "Thanks & Regards" & vbLf _ 
& vbLf _ 
& "-Ryan " & vbLf _ 

「通過Outlook

Set oApp = CreateObject("Outlook.Application") 
Set oMail = oApp.CreateItem(0) 
With oMail 
    .To = mailid 
    .Subject = "Transportation Committee Notice" 
    .Body = Body 
    '.Attachments.Add tWB.FullName 
    .send 
End With 


End Sub 

回答

2

發送電子郵件,以複製當前的代碼使用

mailid = Application.InputBox(Prompt:="Select Range", Type:=8) 

其中Type:=8指定Range返回類型的效果。這將返回Value屬性選擇範圍的成mailid

或者使用

Dim rng as Range 
Set rng = Application.InputBox(Prompt:="Select Range", Type:=8) 
mailid = rng.Value 

rng然後設置爲選定的範圍內,並且在使用前必須被確認

請注意,您應該添加錯誤處理考慮,例如用戶取消輸入框

在發出InputBox之前不要設置Application.ScreenUpdating = False,因爲這樣做會阻止用戶與屏幕進行交互。

順便說一句,您的代碼錯誤地使用了DimDim'如果沒有As子句的變量聲明它爲'Variant'。

Dim oApp, oMail As Object 

實際上宣告oAppVariant,使用

Dim oApp As Object, oMail As Object 
+0

chris-非常感謝你,我試圖瞭解在運行VBA我繼續實現其全部潛力。我真的很感激你指出我犯的那個昏暗的錯誤! –

+0

克里斯,我得到一個運行時錯誤「數組下限必須爲零」任何想法爲什麼發生這種情況/靈魂? –

+0

錯誤是哪一行?如果它的'mailid'''mailid''昏暗'怎麼樣? –