.send function with email script...

chefdt

Board Regular
Joined
Jul 1, 2008
Messages
163
I have a nice little email script that I typically run from the MACRO Editor on the Developer ribbon. I store the macro in my PERSONAL file, and it runs perfectly.

When I try to attach it to a button in another workbook, and activate that WB to run it on, it says it can't find who I want to send to.

Code:
Private Sub CommandButton2_Click()


'Works FINE without this code
'________________________________________________________
[B]Dim wb As Workbook, x As String[/B]
[B]For Each wb In Workbooks[/B]
[B]If wb.Name <> ThisWorkbook.Name Then x = wb.Name[/B]
[B]Next wb[/B]
[B]Workbooks(x).Activate[/B]
'_________________________________________________________


'LOOPER
Dim sht As Object
  For Each sht In Sheets
   If sht.Name <> "Summary" And sht.Name <> "Emails" Then
    sht.Activate
      With sht






Dim oApp As Object
Dim oMail As Object
Dim LWorkbook As Workbook
Dim LFileName As String








'Turn off screen updating
Application.ScreenUpdating = False


'Copy the active worksheet and save to a temporary workbook
ActiveSheet.Copy
Set LWorkbook = ActiveWorkbook


'Create a temporary file in your current directory that uses the name
' of the sheet as the filename
LFileName = LWorkbook.Worksheets(1).Name
On Error Resume Next
'Delete the file if it already exists
Kill LFileName
On Error GoTo 0
'Save temporary file
LWorkbook.SaveAs Filename:=LFileName


'Create an Outlook object and new mail message
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0)


'Set mail attributes (uncomment lines to enter attributes)
' In this example, only the attachment is being added to the mail message
With oMail
.To = [b1].Value
.Subject = "RevMan Pricing WorkSheet TEST- " & LFileName
.body = "Jeff Benedek - RevMan Lead" & vbCrLf & vbCrLf & _
"Attached is the RevMan file!"
.Attachments.Add LWorkbook.FullName
[B].Send[/B]
End With


'Delete the temporary file and close temporary Workbook
LWorkbook.ChangeFileAccess Mode:=xlReadOnly
Kill LWorkbook.FullName
LWorkbook.Close SaveChanges:=False


'Turn back on screen updating
Application.ScreenUpdating = True
Set oMail = Nothing
Set oApp = Nothing




    
End With
    End If
Next sht






End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hello chefdt,

Change This line...
Code:
    .To = [b1].Value

To This ...
Code:
    .To = .Range("B1").Value
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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