Prevent a workbook being saved to OneDrive with VBA - excel

I have an Excel macro-enabled workbook that is created from a template. Although the template contains no sensitive data, once the user opens the workbook and is authenticated, some sensitive data is added to the workbook from a remote server. I need to prevent the workbook from being saved anywhere (the vba code communicates with a server to save the needed data). I can write code on the "before save" event to prevent the workbook from being saved to the local file system, but if an Office 365 user turns on "autosave" a copy of the workbook will be saved to OneDrive and I can't find a way to prevent this. By periodically checking the workbook's path, I can notice that the file has been saved to OneDrive, and delete the sensitive data but by then it's too late because the originally saved file will be available in the file's version history.
Any help and suggestions are appreciated.
In response to a question by Cyril below, here is the code on the workbook's before_save event:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
autoSaveOff ' a sub procedure to turn off autosave
Cancel = True ' prevents Excel from saving
frmMessage.lbllMessage.Caption = "Attempting to save to the Server."
frmMessage.Show False
message_to_addin "save" ' this code cooperates with an Office.js addin to interact with the sercver
Application.OnTime DateAdd("s", 1, Now), "read_message_from_addin"
End Sub

Related

Connecting password-protected Excel to Access

I want to link a password-protected Excel document to Access - this is not working due to the it being password-protected.
Does anyone have an alternative method to do this?
It has to be password-protected unfortunately and cannot be moved to a secured folder so not sure how to do it.
Any advice?
Try using a hidden form to automatically open the Excel file when the database file is opened.
Add this to a new Module:
Option Compare Database
Public xl As Object
Function OpenExcelFile()
xl.Workbooks.Open "path to file.xlsx", , , , "password"
End Function
Function CloseExcelFile()
xl.Quit
set xl=nothing
End Function
Create a blank form and set the HasModule property to true. then add the following to the form's code module.
Private Sub Form_Load()
OpenExcelFile
End Sub
Private Sub Form_Close()
CloseExcelFile
End Sub
Now create a new macro with an OpenForm task to open your form. Set the window mode to 'Hidden'. Save the macro with the name 'AutoExec'. This makes it run when the db is opened.
When the db is opened the macro will run and open the form hidden. The form load event will fire, creating a public Excel.Application object that opens your excel file (you should be able to remove the password from the code if you want the user to be prompted for it). The Excel application will remain open until the hidden form closes (when you close the database). At that point the form close event will fire, causing the Excel Application to quit. As long as the Excel file is open, you should be able to use linked tables and queries.
**You could add xl.Visible=true to the OpenExcelFile function if you want it to be visible to the user.
This link as a similar idea: https://www.connectionstrings.com/how-to-open-password-protected-excel-workbook/

Excel VB - insivible work sheet

I want to open only the userform when I open the excel sheet. I used the below code but other excel also become invisible. I want to display other open excel file and only macro contained file should be disable.
Application.Visible = False
UserForm4.Show
If I see well what you mean, I think the best thing to do is to make your xlsm file an add-in. In order to do this:
1) Call the UserForm4.Show into the Workbook_Open event of your .xlsm;
2) Save your workbook as Excel Add-In (extensions: .xla or the newer .xlam);
3) Enable the add-in on Excel, so every time that a workbook is opened you can show the form and not the entire workbook, so avoiding also to let the EXCEL.EXE instance open (that will remain so if the Application is Visible=False, because no user would see it after closing the workbook they're working with).
Add-ins have a great power in terms of use and distribution, I suggest you to get started from here and go deeper into this very nice tool.
Add the following code to the user form...
Private Sub UserForm_Initialize()
ThisWorkbook.Windows(1).Visible = False
End Sub
Private Sub UserForm_Terminate()
ThisWorkbook.Windows(1).Visible = True
End Sub

Autorun Excel VBA script when outlook saves attachment

I have an outlook procedure that auto saves an excel attachment to a certain location on my hard drive.
I also have a VBA script in Excel that is designed to use the data from that file and send off emails to particular parties. Normally I would have to open the file and run the VBA script. Is there a way to have this VBA script auto-execute once the file is saved from outlook?
The macro in the excel file won't run just because you have saved it.
However, you can run the macro by opening the workbook and calling the macro at Workbook_Open event
1.Open the workbook from outlook programatically
2.Let's say your macro procedure is Sub runMe() then you could write following function in Workbook
Private Sub Workbook_Open()
Call runMe
End Sub
3.After the macro is completed, close the workbook programatically.

MS Access VBA How to I supress the "Do you want to save changes" dialog for excel row deletes?

Similar to a question in
MS Access VBA How to delete a row in Excel
However, the problem is Excel asks for "Do you want to save the changes you made to ...?"
Is there a way to force the saving of changes and disable any message box? I try DoCmd.SetWarnings False, but it does not work.
You'll need to add the following code to ThisWorkbook
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Save
End Sub
If you're running vba in Access and accessing an Excel object, just save the workbook (e.g. xlApp.ThisWorkbook.Save) in your code before closing

VBA To Create Backup

I have a question on the macro events here. Below is my code. This performs the below Operations’
Private Sub SaveAsTabDelimited(ByVal sFileName As String)
With ActiveWorkbook
.SaveAs Filename:=sFileName, FileFormat:=xlText, CreateBackup:=True
End With
End Sub
Public Sub test()
SaveAsTabDelimited "C:\Users\te160772\Desktop\Toad Test\Testsanthu.txt"
ActiveWorkbook.Close savechanges:=True
End Sub
It converts the excel file into tab delimited txt file
It creates a backup copy of the excel file
I wanted this action to be performed each day inorder the excel file to upload to Oracle tables on daily basis. The reason behind converting excel spreadsheet to tab delimited txt file is to preserve the format (To prevent the removal of leading zeros while exporting it to oracle)
But now I am trying to enhance this code so that the following actions should be performed without any glitch. I have tried to do this in all the possible ways .since I am very new to the programming I was not able to do this.
The code should convert the excel file to tab delimited save on my desktop (Hope that my current piece of code is sufficient for this action)
It should create a backup copy in a folder called “Repository” (A folder in a desktop). Each change to my workbook should result a backup copy with the version history if possible(Date and Time stamp in the file name)
the biggest problem associated with my code is that upon close, three separate dialogue boxes appear - one asking me if i want to save, the other asking me if i want to keep what i have copied in the clipboard, another one is replace the existing text file saved in the folder is there a way of answering these dialogue boxes (or suppressing them) from within the macro, so i do not need to manually click on yes or no each time the macro is run?
I have attached my macro to a “shape” in excel but my priority is to run my macro upon closing of my workbook. Every time when any changes happen to my workbook and when I save the workbook, it should create a tab delimited Text files which should replace the existing Txt File without any confirmation dialogue boxes.
Please help me on this. This is badly require for me
With a million thanks
The code should convert the excel file to tab delimited save on my desktop (Hope that my current piece of code is sufficient for this action)
Yes, either you can hardcode the path to the desktop or use this code to get the path to the desktop automatically
Sub GetDesktopPath()
Dim oWS As Object
Dim DskTopPath As String
Set oWS = CreateObject("WScript.Shell")
DskTopPath = oWS.SpecialFolders("Desktop")
Debug.Print DskTopPath
Set oWS = Nothing
End Sub
It should create a backup copy in a folder called “Repository” (A folder in a desktop). Each change to my workbook should result a backup copy with the version history if possible(Date and Time stamp in the file name)
You can use the Workbook_BeforeSave event to create a copy of the existing workbook.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
End Sub
You may want to explore ThisWorkbook.SaveCopyAs See Excel Help for more info.
the biggest problem associated with my code is that upon close, three separate dialogue boxes appear - one asking me if i want to save, the other asking me if i want to keep what i have copied in the clipboard, another one is replace the existing text file saved in the folder is there a way of answering these dialogue boxes (or suppressing them) from within the macro, so i do not need to manually click on yes or no each time the macro is run?
You can get rid of these alerts by simply sandwiching your code between
Application.DisplayAlerts = False
'~~> Your Code
Application.DisplayAlerts = True
If you are using clipboard a lot, then you might also want to clear it by using
Application.CutCopyMode = False
I have attached my macro to a “shape” in excel but my priority is to run my macro upon closing of my workbook. Every time when any changes happen to my workbook and when I save the workbook, it should create a tab delimited Text files which should replace the existing Txt File without any confirmation dialogue boxes.
You can use the Workbook_BeforeClose event to run your relevant code
Private Sub Workbook_BeforeClose(Cancel As Boolean)
End Sub
Regarding the alert, I have already explained that in the previous section.
Hope this sets you in the right path.

Resources