0
votes

I have a macro-enabled workbook that I have to send out to others via email after the code is run. The problem is with the macros the workbook is to large to send. I have been manually saving a copy of the workbook as .xlsx. I'm now trying to do this automatically.

My code to save is as follows:

Application.DisplayAlerts = False

ActiveWorkbook.Save
ActiveWorkbook.SaveAs Filename:="Workbook_No_Macros" 
FileFormat=xlWorkbookNormal

Application.DisplayAlerts = True

The problem is when I go and look at the one that is supposed to not have the VBA code is still too large because the code is being saved with the workbook even though it is being saved a workbook that is not macro-enabled. This also results in Excel seeing the file as corrupted and not wanting to open it outside of compatibility mode.

Any ideas on how to save the workbook without the code embedded in it?

Edit: I have also tried

FileFormat:=56
2

2 Answers

1
votes

You can save a copy of it. Code taken from here:

Sub M_snb()
    Application.DisplayAlerts = False

    ThisWorkbook.Sheets.Copy
    With Workbooks(Workbooks.Count)
        .SaveAs ThisWorkbook.Path & "\copy_001.xlsx", 51
        .Close
    End With

    Application.DisplayAlerts = True
End Sub
0
votes

Assuming you are not dealing with Excel 2003 or older, you should use

ActiveWorkbook.SaveAs Filename:="Workbook_No_Macros", FileFormat:=xlOpenXMLWorkbook    ' 51

FileFormat:=xlWorkbookNormal (-4143) and FileFormat:=xlExcel8 (56) both store the file in xls-format (old Excel versions) with Macros.

However, I doubt that the code itself makes the file so large. Maybe check the content of all worksheets in the file (eg check UsedRange) if one of the sheets contains data that shoudn't be there.