簡體   English   中英

Excel VBA輸入框

[英]excel vba inputbox

我有以下適用於excel vba的代碼,它將通過電子郵件發送工作表中的一系列地址。 Howeve,我希望使用輸入框來確定我想通過電子郵件發送的范圍。 我遇到的麻煩是使輸入成為mailid函數可以理解的值。 有什么建議么?

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

要復制當前代碼的效果,請使用

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

發出InputBox之前不要設置Application.ScreenUpdating = False ,因為這將防止用戶與屏幕進行交互。

Dim說一句,您的代碼錯誤地使用了DimDim一個沒有As子句的變量將其聲明為`Variant。
例如

Dim oApp, oMail As Object

實際上將oApp聲明為Variant ,使用

Dim oApp As Object, oMail As Object

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM