简体   繁体   中英

Excel macro to send selected cells in email not working

I'm trying to automate a report by being able to send a selected range of cells, that includes hidden cells, by making a macro. When I try to run the macro, it is blank. Please note that the text not written as a code are instructions. Macro is shown below:

Sub SendforApproval()
'
' SendforApproval Macro
'

' Generate Email

Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim rng As Range

If ActiveWorkbook.Path <> "" Then
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    Set myOlApp = CreateObject("Outlook.Application")

    'This is where the body of the email is populated - you can point to cells in your worksheet to pull text from them

strbody = "<font size=""3"" face=""Cambria"">" & _
               "Hi " & Range("B5") & ",<br>" & _
               "<br>Please note finance request #" & Range("a7") & " has been accepted. Upon review, please use voting buttons to Approve or Send for Rework.<br>"


' The range belows designates which portion of your sheet will be inserted in the body of the email

Set rng = Sheets("SheetName").Range("A7:T22").SpecialCells(xlCellTypeVisible)

'You can also use a fixed range if you want or flex range
'Set rng = Sheets("YourSheet").Range("D4:D12").SpecialCells(xlCellTypeVisible)
'Set rng = Range("f7").Resize(Application.CountA(Range("f7:f" & Rows.Count)), 12)
On Error GoTo 0

If rng Is Nothing Then
    MsgBox "The selection is not a range or the sheet is protected" & _
           vbNewLine & "please correct and try again.", vbOKOnly
    Exit Sub
End If

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)


' This section defines receipants, Subject
    On Error Resume Next
    With OutMail

        .To = Range("D7")
        .CC = "yourname@email.com"
        .BCC = ""
        .Subject = "Finance Request #" & Range("a7")

  ' This section adds Voting buttons

        .Display   'or use .Send to automatically send without giving opportunity to review final product
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
 ' End of email process


    End If

End Sub

you should use "on error resume next" with more caution. You dont see the exception that occurs.

your failure is that you want to get the value of the cells D7 and A7 (string) but you take instead the range (object).

Try the following:

With OutMail
    .To = Range("D7").Value
    .CC = "yourname@email.com"
    .BCC = ""
    .Subject = "Finance Request #" & Range("A7").Value
    ' This section adds Voting buttons
    .Display   'or use .Send to automatically send without giving opportunity to review final product
End With

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