Take fil name from cell

KlausW

Active Member
Joined
Sep 9, 2020
Messages
445
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone, I use this VBA code to send the current file by email. But I have a challenge. I would like it to take the file name from cell K4. Some who can help.
Any help will be appreciated.
Best regards Klaus W

VBA Code:
Sub mail()

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("k1").Text
    .CC = ""
    .BCC = ""
    .Subject = Range("k9").Value
    .Body = Range("k2") & vbCrLf & " " & vbCrLf & Range("K3") & vbCrLf & Range("K4")
    .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
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try changing strPath formula from :
VBA Code:
strPath = Replace(ThisWorkbook.FullName, ".xlsm", ".xlsx")

to :
VBA Code:
strPath = Range("K4").text

where K4 includes the full filename including the path and file extension or you need to revise the above formula
 
Upvote 0
Solution
Try changing strPath formula from :
VBA Code:
strPath = Replace(ThisWorkbook.FullName, ".xlsm", ".xlsx")

to :
VBA Code:
strPath = Range("K4").text

where K4 includes the full filename including the path and file extension or you need to revise the above formula
Hi Sanjeev1976
Your VBA code works if I write D:\Peter Jones.xlsx in K4 but if I write C:\Peter Jones.xlsx then the VBA code does not work as intended and cannot find the file and I get an error code 1004. I do not get it.
Klaus W
 
Upvote 0
I believe when working with VBA, it’s essential to consider file system permissions. By default, non-administrators cannot write directly to the root of the C drive due to User Account Control (UAC) restrictions. You can try to access the file from a folder on C drive where you have permission instead from the root.
 
Upvote 0
I believe when working with VBA, it’s essential to consider file system permissions. By default, non-administrators cannot write directly to the root of the C drive due to User Account Control (UAC) restrictions. You can try to access the file from a folder on C drive where you have permission instead from the root.
Hi Sanjeev1976
I see what you mean, your solution works. And I'm really happy about that. I just want to find out if I can rename the file from cell K4. Also send it directly. Many thanks for the help, many greetings from Klaus Denmark
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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