catscantuseexcel
New Member
- Joined
- Jul 24, 2023
- Messages
- 2
- Office Version
- 2019
- Platform
- Windows
Hi,
I currently have vba to send multiple sheets in an excel as a new excel file as seen below. However, I am trying to add the following function:
- Instead of putting the sheet names in the vba (Test Sheet, Sheet Test), I would like the vba to take the name from the open sheet. So for example the sheet names I want to send to "dad are all listed in row 2 side by side in different colums. I would want it to loop through the row to find the sheet names which is then send via mail. Is this possible?
I currently have vba to send multiple sheets in an excel as a new excel file as seen below. However, I am trying to add the following function:
- Instead of putting the sheet names in the vba (Test Sheet, Sheet Test), I would like the vba to take the name from the open sheet. So for example the sheet names I want to send to "dad are all listed in row 2 side by side in different colums. I would want it to loop through the row to find the sheet names which is then send via mail. Is this possible?
VBA Code:
Sub Mail_Sheets_Array()
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim TheActiveWindow As Window
Dim TempWindow As Window
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
dad = Sheets("Send Mail").Cells(9, 2)
mom = Sheets("Send Mail").Cells(20, 2)
With Sourcewb
Set TheActiveWindow = ActiveWindow
Set TempWindow = .NewWindow
.Sheets(Array("Test Sheet", "Sheet Test")).Copy
End With
TempWindow.Close
Set Destwb = ActiveWorkbook
With Destwb
If Val(Application.Version) < 12 Then
FileExtStr = ".xls": FileFormatNum = -4143
Else
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End With
TempFilePath = Environ$("temp") & "\"
TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
On Error Resume Next
With OutMail
.To = dad
.cc = mom
.BCC = ""
.subject = "This is the Subject line"
.HTMLBody = "Hi all, <br> <br> Please find the <b>BRC Report</b> attached."
.Attachments.Add Destwb.FullName
.Send
End With
On Error GoTo 0
.Close savechanges:=False
End With
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub