Track copying excel file - vba

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

Related

Get the saved file name in VBA for Word

I need your help to unlock me.
I would like to retrieve the name of the file saved by the user with Word in VBA. I'm using the BeforeSave method, but I can not seem to catch the name chosen by the user.
Thank you in advance for your help !
With the save as pane not triggering the SaveAs() sub, I think the only reliable way to do this is an OnTime event triggered by DocumentBeforeSave.
Something like:
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI = True Then
Word.Application.OnTime Now + TimeValue("00:00:01"), "AfterSave"
Else
DoStuff()
End if
End Sub
Sub AfterSave()
CheckSaveOccured()
DoStuff()
End Sub
See this post for a more detailed explanation.

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

Workbook_BeforeSave with every active workbook ActiveWorkbook

As I very often have to problem, that the tick vanishes in the settings: Calculate before save. (I don't know the exact term as my office version is in German).
That's why I tried to use VBA to solve the problem. I used the following code in my Excel file:
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
If Application.CalculateBeforeSave = False Then
If MsgBox("Caution! Should >calculatebeforesave< be activated?", vbYesNo) = vbYes Then
Application.CalculateBeforeSave = True
Else
End If
Else
End If
End Sub
I put this into "Thisworkbook". But I would like this code to be ran in every workbook I work with (at least all these which allow for macros).
My suggestion was to write ActiveWorkbook_BeforeSave... instead of Workbook_BeforeSave and then put the code in a module in the PERSONAL Macro file. But this doesn't work.
I think you need to use the Excel Applications events rather than workbook events to achieve this, such as in this example
In your PERSONAL workbook right click and insert a a Class Module (Class 1)
Add something similar to below to Class 1:
Public WithEvents appevent As Application
Private Sub appevent_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
'Add what you would like to happen before a workbook closes
End Sub
Next open ThisWorksheet and add code along these lines (I think the PERSONAL workbook opens automatically when Excel starts):
Dim myobject As New Class1
Private Sub Workbook_Open()
Set myobject = Application
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 disable SaveAs in Ms word in vb.net

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