Run time error 1004 - Method 'SaveAs' of object _workbook failed

tigerdel

Board Regular
Joined
Oct 13, 2015
Messages
145
Office Version
  1. 365
Platform
  1. Windows
I am trying to use the code below to send mail to a client and save the sheet as both a PDF & an xslm file

The PDF works fine an it generates the email perfectly, but I get an error of either when it gets to the lines from FileName2 below to create the xlsm file:
Run time error 1004 - Method 'SaveAs' of object _workbook failed or the file cannot be saved with an xslm extension

Code:
Sub MailTo()
Dim EmailSubject As String
Dim EmailSignature As String
Dim PDFFile As String
Dim Email_To As String
Dim FileName As String
Dim FileName2 As String
Dim OutApp As Object
Dim OutMail As Object
Dim AlwaysOverwritePDF As Boolean
Dim OverwritePDF As VbMsgBoxResult
    Application.ScreenUpdating = False
    FileName = Sheets("Quotation").Range("A4").Value
    PDFFile = Environ$("userprofile") & "\OneDrive\MJM Services\2.0 Quotations\pdf Copy\" & FileName & ".pdf"
    Email_To = Sheets("Quotation").Range("C4").Value
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=FileName, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, OpenAfterPublish:=False
    AlwaysOverwritePDF = False
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
        .To = Sheets("Quotation").Range("C4").Value
        .Subject = " Quotation Attached "
        .Body = "Many thanks for your request for a quotation" & vbCrLf & vbCrLf & "Please find attached quotation for work detailed" & vbCrLf & vbCrLf & "Regards" & vbCrLf & vbCrLf & "Matt" & vbCrLf & vbCrLf & "M J Mayne Services"
        .attachments.Add PDFFile
        .Display
    End With
    ActiveSheet.Copy
    FileName2 = Environ$("userprofile") & "\OneDrive\MJM Services\2.0 Quotations\xlsm Copy\" & FileName2 & ".xlsm"
    ActiveWorkbook.SaveAs FileName2, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    Application.ScreenUpdating = True
End Sub

Really hope you experts can solve this because it is driving me nuts

Many thanks for looking
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
With this line
Rich (BB code):
FileName2 = Environ$("userprofile") & "\OneDrive\MJM Services\2.0 Quotations\xlsm Copy\" & FileName2 & ".xlsm"
FileName2 does not have a value, should the part in blue be FileName?
 
Upvote 0
Solution
With this line
Rich (BB code):
FileName2 = Environ$("userprofile") & "\OneDrive\MJM Services\2.0 Quotations\xlsm Copy\" & FileName2 & ".xlsm"
FileName2 does not have a value, should the part in blue be FileName?
Fluff

Thats brilliant

Thank you so much
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,881
Messages
6,122,074
Members
449,064
Latest member
MattDRT

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