How to disable SaveAs in Ms word in vb.net - vba

How can i disable Save as in MS word or in any other office using vb.net?

You need to use VBA Macro to disable Save As Option.
Code to disable in Excel is :
Private Sub Workbook_BeforeSave (ByVal SaveAsUI As Boolean, Cancel As Boolean)
'This macro disables the "Save As" Feature in Excel
'This means that a user will not be able to save this
'workbook(file) under a different name or in a different location
'
'This MUST be placed in "ThisWorkbook" and NOT in a Module.
'
If SaveAsUI = True Then Cancel = True
End Sub

Related

Track copying excel file

Is it possible to track if anyone saving as an excel file using VBA?
Thank you :)
Yes it is possible.
Use the Workbook.BeforeSave Event which occurs before the workbook is saved.
Then write a log in whatever way you prefer. Eg log into a text file or in a worksheet of the workbook itself.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI Then
' do Stuff for SaveAs
Else
' do stuff for Save
End If
End Sub

VBA excel macros: Run only from a location

I have a few Excel sheets with macros which I want any user to be able to run only from a particular location, in my case a particular sharepoint.
Should a user "SaveAs" the Excel file to any alternative location - he should get an error message.
I am looking for a VBA script to be put in these workbooks to allow me meet my objectives.
Can someone help please?
You can there display an error message and set the Cancel parameter to true if someone wants to save it to another location.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not ThisWorkbook.Path Like "//sharepoint/path/*" Then
MsgBox "You can't save the excel", vbCritical
Cancel = True
End if
End Sub
In the same way you can check in the Workbook_Open function if the Workbook is opened in a correct location:
Private Sub Workbook_Open()
If ThisWorkbook.Path Like "//sharepoint/path/*" Then
'Do things
Else
'Error message and close
End If
End Sub

Disable Save As In A Batch Of Excel Files

I've been researching how to disable "Save As" in Excel and it seems possible. I have a macro that loops through a bunch of Excel files making changes. In that macro I would like to disable "Save As" for all of the files. Is this possible?
Using application object. You just add this lines to the code that loops in every file or workbook so it disable "save as" for each one :
Application.CommandBars("Worksheet Menu Bar").Controls("File").Controls("Save As...").Enabled = False
Application.CommandBars("Worksheet Menu Bar").Controls("File").Controls("Save").Enabled = False
Tell me how it goes.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "You can't save this workbook!"
Cancel = True
End Sub

Disable Excel save option but allow macro save

I'm creating an excel file and I want to disable the 'save' and 'save as...' option.
I found a lot of solutions on the internet, like this one in VBA:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "You can't save this workbook!"
Cancel = True
End Sub
It prevents user from saving changes, but I can't save my changes and that's the problem because I need to do more changes in the VBA code.
Is there a way to save my macro changes ? Like an administrator mode etc... ?
Thank you for your future answers.
Use a global variable to override the Save disable:
Dim override as Boolean
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
if Not(override) then
MsgBox "You can't save this workbook!"
Cancel = True
end if
End Sub
Sub SaveMyChanges()
override = true
ActiveWorkbook.Save
override = false
End Sub
You can also save while in Design Mode in VBA.
Sorry for the necro, but this works also and is what I do.

How to run a macro in Word before save?

How to make Microsoft Word to run a VBA macro every time before any document is saved? Could it be done without adding macros into the document itself?
You can subscribe to application events in Document_Open by using WithEvents variable and conventional method names (VariableName_EventName). Works in templates as well.
You can put this code into ThisDocument object, or make a separate class module as described here.
Private WithEvents App As Word.Application
Private Sub Document_Open()
Set App = Word.Application
End Sub
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
MsgBox("BeforeSave")
End Sub
List of all application events.
You must add the code bits at the correct place.
This must be at the top of your code page amongst your public variables or constant declerations
Private WithEvents App As Word.Application
Then add this as an open document event.
Private Sub Document_Open()
Set App = Word.Application
End Sub
This is the event that fires on save command Ctrl+s or save icon. I added my own save format and print as I saw It most useful in the case of people filling out forms and you don't want them to overwrite the initial template.
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
''save file with the saveas2 command.
ActiveDocument.SaveAs2 FileName:="YourDocumentNameORVariable" + _
" Date_" + Format(Now(), "yy-mm-dd"), _
FileFormat:=wdFormatDocumentDefault, _
SaveFormsData:=True
''addition to print file upon save
ActiveDocument.PrintOut Background:=True, Range:=wdPrintAllDocument, Copies:=1, Collate:=True
End Sub
Read more about Printout methods: Microsoft VBA - PrintOut
Read more about SaveAs2: Microsoft VBA - SaveAs2
Read more about FileFormat for Saving: Microsoft VBA - FileFormat for Saving
try saving your file in .xlsm, then close, open and save it again. it should work fine.