Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving new Excel document as macro-free workbook without prompt

I'm using Excel 2010. I have an Excel macro-enabled template that has a data connection to a text file that is set to automatically refresh when a new document is created using this template.

The following macro is within the "ThisWorkbook" object to remove the data connection before saving the new document:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    Do While ActiveWorkbook.Connections.Count > 0
        ActiveWorkbook.Connections.Item(ActiveWorkbook.Connections.Count).Delete
    Loop

End Sub

When a user clicks the save icon / hits ctrl+S, inputs a filename and then clicks save to save as a macro-free Excel workbook (as is the default and required filetype) they are prompted with a message stating:

The following features cannot be saved in macro-free workbooks:

• VB project

To save a file with these features, click No, and then choose a macro-enabled file type in the File Type list.

To continue saving as a macro-free workbook, click Yes.

Is it possible to prevent this message from appearing and have Excel assume that the user wants to continue with a macro-free workbook?

I've searched all over and understand that I may be able to add code to the workbook object that removes itself so that Excel has no VB project to cause this message but this would require each user to change Trust Center Settings (Trust access to the VBA project object model) which I want to avoid.

I've also seen suggestions of using:

Application.DisplayAlerts = False

but can't get this to work. Every example of it's use seems to be within a sub that is also handling the saving of the document whereas in my situation the BeforeSave sub ends before the document is saved in the default, non-vba way which is perhaps why it does not work?

Does this property reset to a default True after the sub has ended / before the save actually occurs?

Apologies for any nonsense I may have dispensed, my experience with VBA is very limited.

like image 300
Tom Turner Avatar asked Aug 09 '17 16:08

Tom Turner


People also ask

How do you stop Excel from prompting to save?

Excel always prompts me to SAVE! But I didn't DO anything! File > Options > Advanced, under General make sure that Ignore other applications is not ticked. Do you want to save the changes to Filename.

How do you get rid of the following features Cannot be saved in macro-free workbook?

If you save under the default . xlsx format you get the warning The following features cannot be saved in macro-free workbooks: VB project. If you do what you usually do, clicking Yes, you loose the macros you just created, so click No and save again as . xlsm.


1 Answers

I cannot test on Excel 2010, but at least for 2016, it's working fine:

Sub SaveAsRegularWorkbook()

    Dim wb As Workbook
    Dim Path As String

    Set wb = ThisWorkbook
    Path = "T:\he\Path\you\prefer\"
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    wb.SaveAs Filename:=Path & "Test.xlsx", FileFormat:=51
    Application.DisplayAlerts = True
    Application.EnableEvents = True

End Sub

Give it a try.

like image 192
EarlyBird2 Avatar answered Sep 22 '22 23:09

EarlyBird2