Attach hidden worksheet in email, or copy data from hidden worksheet into a new workbook and attach to email

Marcfar

New Member
Joined
May 28, 2020
Messages
7
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2010
HI - I have code that sends an email when using the command button.

Is there a way to attach only the Sheet 2 (which is hidden) in an outlook email as an attachment? File save name doesn't matter.
Or can we copy the data to a new workbook and attach that workbook to email?

I don't want to have to worry about location where users have saved the file or where they are accessing it from.

Here is the code I have for the email command button (bottom 2/3 of the code including the "function range to html")
Thanks!


VBA Code:
ThisWorkbook.Worksheets("Form_Data").Range("B2").Value = TextBox1
ThisWorkbook.Worksheets("Form_Data").Range("B3").Value = TextBox2
ThisWorkbook.Worksheets("Form_Data").Range("B10").Value = TextBox6
ThisWorkbook.Worksheets("Form_Data").Range("B11").Value = TextBox7
ThisWorkbook.Worksheets("Form_Data").Range("B8").Value = TextBox8
ThisWorkbook.Worksheets("Form_Data").Range("B6").Value = TextBox9

ThisWorkbook.Worksheets("Form_Data").Range("B5").Value = ComboBox1
ThisWorkbook.Worksheets("Form_Data").Range("B7").Value = ComboBox2


Dim x
x = Me.ListBox4.List
ThisWorkbook.Worksheets("Form_Data").Range("A14").Resize(UBound(x) + 1, 1).Value = x

x = Me.ListBox5.List
ThisWorkbook.Worksheets("Form_Data").Range("b14").Resize(UBound(x) + 1, 1).Value = x


 If OptionButton4.Value = True Then
         ThisWorkbook.Worksheets("Form_Data").Range("B9").Value = "No"
     End If
 If OptionButton5.Value = True Then
          ThisWorkbook.Worksheets("Form_Data").Range("B9").Value = "Yes"
     End If
 If OptionButton6.Value = True Then
         ThisWorkbook.Worksheets("Form_Data").Range("B4").Value = "Change an Existing User"
     End If
  If OptionButton7.Value = True Then
          ThisWorkbook.Worksheets("Form_Data").Range("B4").Value = "Add a New User"
     End If
 If OptionButton8.Value = True Then
          ThisWorkbook.Worksheets("Form_Data").Range("B4").Value = "Inactivate an Existing User"
     End If
    
    
'copy to email in outlook and display send window
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object

    Set rng = Nothing
    On Error Resume Next
    'Only the visible cells in the selection
    Set rng = Sheets("Form_Data").Range("a1:b30").SpecialCells(xlCellTypeVisible)
    'You can also use a fixed range if you want
    'Set rng = Sheets("YourSheet").Range("D4:D12").SpecialCells(xlCellTypeVisible)
    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)

    On Error Resume Next
    With OutMail
        .To = "mfarooqui2@wrha.mb.ca"
        .CC = ""
        .BCC = ""
        .Subject = "ESP User Request Form"
        .HTMLBody = RangetoHTML(rng)
        .Display  'or use .send
    End With
    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub


Function RangetoHTML(rng As Range)
'part of copy to email in outlook and display send window
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With

    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

    'Close TempWB
    TempWB.Close savechanges:=False

    'Delete the htm file we used in this function
    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function
 

Some videos you may like

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Marcfar

New Member
Joined
May 28, 2020
Messages
7
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2010
bump. any advise or existing threads that people are aware of?
 

Watch MrExcel Video

Forum statistics

Threads
1,127,888
Messages
5,627,474
Members
416,250
Latest member
darius_rebelo

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
Top