remove all macros and forms in file and save the file as xlsx

Alaa mg

Active Member
Joined
May 29, 2021
Messages
343
Office Version
  1. 2019
hi

is there macro remove all macros and forms , shapes and buttons in file XLSM and save the file as XLSX based on date(30/12/2021) .

the file should work until before this date . if I open file in 30/12/2021 then should delete all macros and forms and shapes , buttons in all the sheets

with keep the data contains value, letters ,formatting , borders ,formulas...etc ain all sheets when save the file as xlsx . but before does all what I mentioned should save the file as xlsm in this directory c:\temp just when open file in 30/12/2021 .
I hope my thread is clear .
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Another option to make the macro not run would be to add a custom document property perhaps?

I haven't used it before so can not say if there is a downside, but just made a test. In the immediate window:

VBA Code:
thisworkbook.CustomDocumentProperties.add name:="Test",LinkToContent:=false,type:=msoPropertyTypeString,Value:="processed"

? thisworkbook.CustomDocumentProperties("test")
processed

So check for this in the if statement (since cells may change etc.)?

Edit: Persists after re-opening of the file.

Edit2: Throws an error when no such property exists, so error handling required.
 
Upvote 0
Oh well I did modify Zot's code, try on a copy of course.

VBA Code:
Private Sub Workbook_Open()

' Check if file was processed before
Dim strProcessed as String

On Error Resume Next

strProcessed = ThisWorkbook.CustomDocumentProperties("Processed")

' If no error while reading "Processed" property (we don't care about its value, just that it is there), then exit sub
If Err.Number = 0 Then
    On Error GoTo 0
    Exit Sub
End If

Err.Clear
On Error GoTo 0

Dim FName As String, Path As String, cPath As String
Dim wb As Workbook
Dim d As Date
Dim shp As Shape

d = DateSerial(2021, 9, 21)
Path = "C:\Temp\"

Set wb = ActiveWorkbook
cPath = wb.Path & "\"
FName = Replace(wb.Name, ".xlsm", "")
If Date >= d Then
    ' Set "Processed" property
    ThisWorkbook.CustomDocumentProperties.Add Name:="Processed",LinkToContent:=False,Type:=msoPropertyTypeString,Value:="Processed"
    For Each shp In ActiveSheet.Shapes
        shp.Delete
    Next shp
    Application.DisplayAlerts = False
    ' SAve macro file in Temp
    ActiveWorkbook.SaveAs Filename:=Path & FName, FileFormat:=52 ' or xlOpenXMLWorkbookMacroEnabled
    ' Delete macro file in original folder
    Kill cPath & FName & ".xlsm"
    ' Save non-macro file in original location
    ActiveWorkbook.SaveAs Filename:=cPath & FName, FileFormat:=51 ' or xlOpenXMLWorkbook
    Application.DisplayAlerts = True
End If

End Sub
 
Upvote 0
@Gokhan Aycan ,GWteB ,Zot based on post#27 last thing when save file as xlsm should not remove the buttons and shapes
thanks for your cooperation
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,436
Members
449,083
Latest member
Ava19

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