Hi again Semaj
Your question currently includes the wording of your code (but unformatted and so confusing) and does not contain your routine macro_one - please edit to correct.
However the problem arises with your use of the Dir function and the variable newFilename.
If you have a folder C:\Test\Export\ and it contains no files say, the line with an * wildcard (e.g. in macro_two):
newFilename = Dir("C:\Test\Export\*")
returns nothing.
If there are files in that folder, it would return the name of the first file it finds e.g. "ABC.xlsx" (if that is the first file alphabetically).
The problem is that your line:
emailItem.Attachments.Add newFilename
gives Outlook the string "ABC.xlsx" (which it can't find and so fails), rather than the necessary full path to the file e.g. it would need ."C:\Test\Export\ABC.xlsx".
That could be corrected e.g. using:
' Add attachments
If Dir("C:\Test\Export\" & newFilename) <> vbNullString Then
emailItem.Attachments.Add "C:\Test\Export" & newFilename
End If
but there are further problems:
newFilename = Dir("C:\Test\Export\*")
might return the name of a pdf file when you are trying to send an Excel file (and vice versa)Kill newFilename
attempts to remove and existing file (and eventually that folder will contain no files) but it doesn't have the full path so would failI suggest you instead create a new (dated) file name which suits the file you want to export e.g for pdf in macro_two:
newFilename = "C:\Test\Export\" & "Report for " & Format(Date, "dd mmm yyyy") & ".pdf"
which would produce the full path C:\Test\Export\Report for 21 Dec 2024.pdf: which would add correctly (and could be deleted too) using your existing code.
You would need to change the ending for .xlxs files.
Hope this fixes your problem and you can select this Answer (but please don't forget to tidy up your question to help others).
Worksheets(Array(wsDashboard.Name, wsActionReg.Name)).Copy
. Set wbNew = ActiveWorkbook
'Define the file path and name
newFilename = strFolderName & "\MultiSet Action Register " & Format(Date, "yyyy-mm-dd") & ".xlsx"
'Save new workbook (wbNew)
wbNew.SaveAs _
Filename:=newFilename, _
FileFormat:=51
'Assign the file path to the global variable
filePath = newFilename
'Close the new workbook
wbNew.Close False
.Attachments.Add filePath
.Display 'Use .Send to send automatically
End With
ExitSub:
'Cleanup
Set OutMail = Nothing
Set OutApp = Nothing
'Delete temp file
If filePath = "" Then Exit Sub
'Reset the global variable
Kill filePath
'Clear Gloabal Variable
filePath = ""
Exit Sub