Link to home
Start Free TrialLog in
Avatar of kmgingee
kmgingeeFlag for United States of America

asked on

Hidden rows showing up in my email attachment

Hi Everyone:

Sorry if this is a really stupid question,but here goes:

I want to send a worksheet in the body of the mail,in the worksheet I have hidden cells:I unhide them but they still show up in the body of the email I send,Can some please show me how I unhide them in my macro? my code is pretty simple:


Sub Send_Range()

   ActiveWorkbook.EnvelopeVisible = True
   ' Set the optional introduction field thats adds
   ' some header text to the email body. It also sets
   ' the To and Subject lines. Finally the message
   ' is sent.
   With ActiveSheet.MailEnvelope
      .Introduction = "This is a sample worksheet."
      .Item.To = "E-Mail_Address_Here"
      .Item.Subject = "My subject"
      .Item.Send
   End With
End Sub


if it helps any: my hidden cells are from column F : column H,but i want the values in column I to be displayed.


if this is not doable my other (much disliked) option is have the the activesheet saved on the desktop and then the user sends it as an attachment.i really really dont want to do this

Sub MailSheet()
    Dim shtName As String
    shtName = ActiveSheet.Name
    ActiveSheet.Copy
    ActiveWorkbook.SaveAs FileName:=Application.GetSaveAsFilename("Copy of " & shtName, "Microsoft Excel File, *.xls")
    Application.DisplayAlerts = False
    Application.Dialogs(xlDialogSendMail).Show
    With ActiveWorkbook
        .ChangeFileAccess xlReadOnly
        Kill .FullName
        .Close False
    End With

    Application.DisplayAlerts = True
End Sub


so my request is either:

1)i am able to remove the hidden cells from the email body OR
2)send the activesheet as an attachment without saving on the users PC

Can someone please guide me?

Regards

Arun






Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)
Flag of United States of America image

Use this code. It creates a copy of the worksheet, converts all formulas to values, deletes the hidden columns, sends it, then deletes it.

Sub Send_Range()

    Dim SourceWorksheet As Worksheet
    Dim ColumnWidth As Double
    Dim Column As Long
    ActiveWorkbook.EnvelopeVisible = True
    ' Set the optional introduction field thats adds
    ' some header text to the email body. It also sets
    ' the To and Subject lines. Finally the message
    ' is sent.
    Application.DisplayAlerts = False
    ThisWorkbook.ActiveSheet.Copy After:=ThisWorkbook.ActiveSheet
    Set SourceWorksheet = ActiveSheet
    SourceWorksheet.UsedRange.Value = SourceWorksheet.UsedRange.Value
    With SourceWorksheet.UsedRange
        For Column = .Column To .Column + .Columns.Count - 1
             If .Columns(Column).Hidden Then
                 ColumnWidth = .Columns(Column).Offset(0, 1).ColumnWidth
                 .Columns(Column).Delete
                 .Columns(Column).ColumnWidth = ColumnWidth
             End If
        Next Column
    End With
    With SourceWorksheet.MailEnvelope
       .Introduction = "This is a sample worksheet."
       .Item.To = "E-Mail_Address_Here"
       .Item.Subject = "My subject"
       .Item.Send
    End With
    SourceWorksheet.Delete
    Application.DisplayAlerts = True
End Sub

Kevin
Avatar of kmgingee

ASKER

thanks kevin,but i get this error when i run your code:

Run-time error '1004'

Application-defined or object-defined error


Thanks

Arun
What line?

Kevin
I Get the error in this line:

Run-time error '1004'

Cannot enter a null value as an item or field name in a pivot table report


it is coming on this line of your code:

Application.DisplayAlerts = False


I have attached the file for your reference



2009-Vacation-Tracking-Schedule.xls
Fixed:

Sub read()
   
    Dim SourceWorksheet As Worksheet
    Dim Column As Long
    Dim Calculation As Long
    Application.ScreenUpdating = False
    Calculation = Application.Calculation
    Application.Calculation = xlCalculationManual
    ActiveWorkbook.EnvelopeVisible = True
    ' Set the optional introduction field thats adds
    ' some header text to the email body. It also sets
    ' the To and Subject lines. Finally the message
    ' is sent.
    Application.DisplayAlerts = False
    ThisWorkbook.ActiveSheet.Copy After:=ThisWorkbook.ActiveSheet
    Set SourceWorksheet = ActiveSheet
    SourceWorksheet.UsedRange.Copy
    SourceWorksheet.UsedRange.PasteSpecial xlPasteValues
    With SourceWorksheet.UsedRange
        Column = .Column + .Columns.Count - 1
        Do
            If .Columns(Column).ColumnWidth = 0 Then
                .Columns(Column).ColumnWidth = .Columns(Column + 1).ColumnWidth
                .Columns(Column).Delete
            End If
            Column = Column - 1
        Loop While Column >= .Column
    End With
    With SourceWorksheet.MailEnvelope
       .Introduction = "This is a sample worksheet."
       .Item.To = "E-Mail_Address_Here"
       .Item.Subject = "My subject"
       .Item.Send
    End With
    SourceWorksheet.Delete
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    Application.Calculation = Calculation
   
End Sub

Kevin
Thanks kevin,but unfortunately I still seem to be seeing the hidden cells showing up in the body of my mail:

I have attached a screenshot if it helps.

If this is not doable,i dont mind sending this activeworksheet as a excel attachment ( but it has to be automatic-no saving on the users PC)

any suggestions u might have?

untitled.JPG
ASKER CERTIFIED SOLUTION
Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
WOAH,it worked! - AWESOME!!
thank you soo very much!!!!
u the man :)