简体   繁体   中英

How to reference a range of cells?

I want to send a picture from an Excel file, to a list of email addresses in the same file.

How do I reference the range?

Sub Email_picture()
    
    Dim ol As Outlook.Application
    Dim mi As Outlook.MailItem
    Dim doc As Word.Document
    
    Set ol = New Outlook.Application
    Set mi = ol.CreateItem(olMailItem)
    
    mi.Display
    mi.To = ThisWorkbook.Worksheets("Sheet2").Range("A1:A3")
    mi.Subject = "Picture"
    
    Set doc = mi.GetInspector.WordEditor
    
    doc.Range(0, 0).InlineShapes.AddPicture "C:\Users\msr\Documents\Slide2.jpg"
    
End Sub

It is the line mi.To = ThisWorkbook.Worksheets("Sheet2").Range("A1:A3") .

Range is an Excel specific COM object. Outlook would not know what to do with it. You need to loop through all cells explicitly and use their values to either construct a ";" separated list of addresses that you can assign to the To property, or you can use the value of each cell to call Recipients.Add to add one address at a time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM