Change Attachments from XLSM to XLSX in mail

KlausW

Active Member
Joined
Sep 9, 2020
Messages
458
Office Version
  1. 2016
Platform
  1. Windows
Hi every one
I use this VBA code to send the Workbook I'm in by email. The file is sent as a file containing macro (xlsm), but I would like it to be sent without macro (xlsx). Some who can help.

Any help will be appreciated
Best Regards
Klaus W

VBA Code:
Sub Rektangelafrundedehjørner2_Klik()

Dim OutlookApp As Object
Dim OutlookMail As Object
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
On Error Resume Next
With OutlookMail
    .To = Range("o1").Text
    .CC = ""
    .BCC = ""
    .Subject = Range("k9").Value
    .Body = "17:10"
    .Attachments.Add Application.ActiveWorkbook.FullName
    .Send
End With
Set OutlookMail = Nothing
Set OutlookApp = Nothing

End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try (of the top of my head)
Dim strFileName As String

strFileName = Application.ActiveWorkbook.FullName
strFileName = Replace(strFileName,".xlsm", ".xlsx")

then .Attachments.Add strFileName

If it works, strFileName could probably be created on 1 line if preferred.
EDIT - corrected missing dot for .xlsx
I think you can get away with changing the extension to create the xlsx file, but I don't know what that will mean to your users. I'd test by sending myself a file before rolling it out. You might find that if anyone knows/discovers there is code in the file, they will be able to access it by changing the extension back. You might want to verify that too.
 
Upvote 0
Try (of the top of my head)
Dim strFileName As String

strFileName = Application.ActiveWorkbook.FullName
strFileName = Replace(strFileName,".xlsm", ".xlsx")

then .Attachments.Add strFileName

If it works, strFileName could probably be created on 1 line if preferred.
EDIT - corrected missing dot for .xlsx
I think you can get away with changing the extension to create the xlsx file, but I don't know what that will mean to your users. I'd test by sending myself a file before rolling it out. You might find that if anyone knows/discovers there is code in the file, they will be able to access it by changing the extension back. You might want to verify that too.
Hi Micron, I've tried putting in your code but it won't take the file. I can't figure out where to put your code. Apparently. Klaus W
 
Upvote 0
Totally overlooked the fact that the filename would have to exist in order to attach it. If you could change the name after attaching I don't know how, but I seriously doubt you can. You would have to do a SaveAs then attach that. Presumably you'll use vba to save a file with the properties you need. Here is what I came up with - seems to work. If the file exists, it still works (presumably it overwrites it). I had to test with my own email, then remove that from the code.
VBA Code:
Sub Rektangelafrundedehjørner2_Klik()
Dim strPath As String
Dim OutlookApp As Object, OutlookMail As Object

On Error GoTo errHandler
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
strPath = Replace(ThisWorkbook.FullName, ".xlsm", ".xlsx")
Application.DisplayAlerts = False
ThisWorkbook.SaveAs strPath, 51
Application.DisplayAlerts = True

With OutlookMail
    .To = Range("o1").Text
    .CC = ""
    .BCC = ""
    .Subject = Range("k9").Value
    .Body = "17:10"
    .Attachments.Add strPath
    .Send
End With

exitHere:
Set OutlookMail = Nothing
Set OutlookApp = Nothing
Application.DisplayAlerts = True
Exit Sub

errHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume exitHere
End Sub
EDIT - be careful of where you use Resume Next - it will often mask issues. An error handler is usually better.
 
Upvote 0
Solution
Totally overlooked the fact that the filename would have to exist in order to attach it. If you could change the name after attaching I don't know how, but I seriously doubt you can. You would have to do a SaveAs then attach that. Presumably you'll use vba to save a file with the properties you need. Here is what I came up with - seems to work. If the file exists, it still works (presumably it overwrites it). I had to test with my own email, then remove that from the code.
VBA Code:
Sub Rektangelafrundedehjørner2_Klik()
Dim strPath As String
Dim OutlookApp As Object, OutlookMail As Object

On Error GoTo errHandler
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
strPath = Replace(ThisWorkbook.FullName, ".xlsm", ".xlsx")
Application.DisplayAlerts = False
ThisWorkbook.SaveAs strPath, 51
Application.DisplayAlerts = True

With OutlookMail
    .To = Range("o1").Text
    .CC = ""
    .BCC = ""
    .Subject = Range("k9").Value
    .Body = "17:10"
    .Attachments.Add strPath
    .Send
End With

exitHere:
Set OutlookMail = Nothing
Set OutlookApp = Nothing
Application.DisplayAlerts = True
Exit Sub

errHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume exitHere
End Sub
EDIT - be careful of where you use Resume Next - it will often mask issues. An error handler is usually better.
Totally overlooked the fact that the filename would have to exist in order to attach it. If you could change the name after attaching I don't know how, but I seriously doubt you can. You would have to do a SaveAs then attach that. Presumably you'll use vba to save a file with the properties you need. Here is what I came up with - seems to work. If the file exists, it still works (presumably it overwrites it). I had to test with my own email, then remove that from the code.
VBA Code:
Sub Rektangelafrundedehjørner2_Klik()
Dim strPath As String
Dim OutlookApp As Object, OutlookMail As Object

On Error GoTo errHandler
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
strPath = Replace(ThisWorkbook.FullName, ".xlsm", ".xlsx")
Application.DisplayAlerts = False
ThisWorkbook.SaveAs strPath, 51
Application.DisplayAlerts = True

With OutlookMail
    .To = Range("o1").Text
    .CC = ""
    .BCC = ""
    .Subject = Range("k9").Value
    .Body = "17:10"
    .Attachments.Add strPath
    .Send
End With

exitHere:
Set OutlookMail = Nothing
Set OutlookApp = Nothing
Application.DisplayAlerts = True
Exit Sub

errHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume exitHere
End Sub
EDIT - be careful of where you use Resume Next - it will often mask issues. An error handler is usually better.
Hi Micron
thank you very much for your help. Just as it should be. Good day. Best Regards Klaus W
 
Upvote 0
Glad I could help & thanks for the recognition.
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,096
Members
453,021
Latest member
Justyna P

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
Back
Top