Transferring data cell by cell from Excel to VB

sridharprahlad

New Member
Joined
Sep 12, 2008
Messages
39
Hi.. I need to transfer a excel cell value in to my macro code. I'm trying from past 2 days, but not getting it.

I'm automating the process of sending an Excel book to the selected recipients through outlook. For that i need to put recipient email address in ".To" of macro code. How to accomplish this? The value is in cell "A52" in the excel file. Plz Help. :confused:
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi.. I got most of the code from the above url. But I just need the code to refer a particular cell in Excel through VB. :(
 
Upvote 0
Its not working. I think because of the file and sheet name.
Excel file name is "Report" and Sheet name is "MPR". The email id is in the Cell "A52". I have paste the entire code below. When i run this macro, it will open a new mail with the file as attachment. But "To" address is blank. Plz Suggest.


Sub Mail_workbook_1()<o:p></o:p>
<o:p> </o:p>
Dim OutApp As Object<o:p></o:p>
Dim OutMail As Object<o:p></o:p>
Dim strbody As String<o:p></o:p>
<o:p></o:p>
Dim oExcel As Object<o:p></o:p>
Dim oBook As Object<o:p></o:p>
Dim oSheet As Object<o:p></o:p>
<o:p> </o:p>
Set OutApp = CreateObject("Outlook.Application")<o:p></o:p>
OutApp.Session.Logon<o:p></o:p>
Set OutMail = OutApp.CreateItem(0)<o:p></o:p>
<o:p> </o:p>
'Start a new workbook in Excel<o:p></o:p>
Set oExcel = CreateObject("Excel.Application")<o:p></o:p>
Set oBook = oExcel.Workbooks.Add<o:p></o:p>
<o:p> </o:p>
Set oSheet = oBook.Worksheets(1)<o:p></o:p>
<o:p></o:p>
strbody = " Dear XYZ," & vbNewLine & " Please find attached the Report for your appoval." & vbNewLine & "Regards"<o:p></o:p>
<o:p> </o:p>
ActiveWorkbook.Save<o:p></o:p>
<o:p> </o:p>
On Error Resume Next<o:p></o:p>
With OutMail<o:p></o:p>
<o:p></o:p>
.To = oSheet.Range("A52").Value<o:p></o:p>
<o:p></o:p>
.CC = ""<o:p></o:p>
.BCC = ""<o:p></o:p>
.Subject = "Report"<o:p></o:p>
.Body = strbody & vbNewLine & vbNewLine & Signature<o:p></o:p>
<o:p></o:p>
.Attachments.Add ActiveWorkbook.FullName<o:p></o:p>
<o:p></o:p>
.Send 'or use .Display<o:p></o:p>
<o:p></o:p>
End With<o:p></o:p>
On Error GoTo 0<o:p></o:p>
<o:p> </o:p>
Set OutMail = Nothing<o:p></o:p>
Set OutApp = Nothing<o:p></o:p>
<o:p> </o:p>
End Sub<o:p></o:p>
<o:p> </o:p>
 
Upvote 0
Please use code tags for your code.

Maybe:
Code:
.To = oBook.oSheet.Range("A52").Value
 
Upvote 0
Oh No.. Its not working. :( The To field in the outlook mail is still empty. The ".To" in the code is not getting the email id which is present in the cell A52 of the excel sheet.
 
Upvote 0
Hi again,

oSheet is an empty sheet in a new Workbook. That thus cannot function. The following code works fine for me:

Code:
Sub Mail_workbook_1()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String
    Dim strSignature As String
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
    strbody = " Dear XYZ," & vbNewLine & _
        " Please find attached the Report for your appoval." & vbNewLine & "Regards"
    strSignature = "It's me" & vbNewLine & "Yep"
    With OutMail
        .To = ThisWorkbook.Worksheets(1).Range("A52").Text
        .CC = ""
        .BCC = ""
        .Subject = "Report"
        .Body = strbody & vbNewLine & vbNewLine & strSignature
        .Attachments.Add ActiveWorkbook.FullName
        .Send 'or use .Display
    End With
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
Case_Germany
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top