I am using VBScript to make lots of calculations on different sheets on the same workbook. Now one concern is if somehow someone deleted any of the sheet, the script would then lead to massive data loss. Is there any way to protect it from users?
(Make sure you read the edit at the end of this answer)
I have similar workbooks I have made that use sheets like databases, so I understand your concern of protecting the information. If you are protecting the Workbook, it will prevent your macro from performing certain changes to the sheets/book. However, if you only want to protect the sheet contents, you can use the setting UserInterfaceOnly, which will allow your macro to run freely while still protecting the sheets from the users. You can detect if the sheets are protected when opened, and if not, protect them.
Sub test()
dim mySheet as worksheet
dim myPass as string
myPass = "password"
set mySheet = Excel.ActiveSheet
if mySheet.ProtectContents = False then
mySheet.Protect Password:=myPass, UserInterfaceOnly:=True
end if
'Enter your code here
mySheet.Unprotect Password:=myPass
End Sub
Furthermore, if you want to give the user the ability to unprotect the sheets after being warned, then you can use Custom UI Editor to remove the protect/unprotect sheets group from the review tab and then replace it with a customized button. I've done this in the past and it actually helped a lot with maintaining the sheets later. I've included a couple links on Custom UI Editor below.
Custom UI Editor Download
Custom UI Editor Tutorial - Change the Ribbon in Excel 2007 or
2010
EDIT:
At first glance I thought this question pertained to VBA (my bad). I believe the above code can still apply, just place the code within the tags and set the workbook and worksheet variables properly (not the way they are set above). If you don't already know how to set/reference your workbook/worksheetsheet objects, this forum post shows how. Albeit I don't normally work with VBscript, but the forum looks correct. Good luck!
i nw this was a few years ago, but i ran into it when searching for the answer, hopefully someone fins this helpfull.
First add a password to your workbook that way other users will only be able to open as read only.
http://www.k2e.com/tech-update/tips/728-tips-adding-a-password-to-make-an-excel-workbook-read-only
then to make as part of the vbscript when opening the excel file(s)
Set oExcel = CreateObject("Excel.Application")
Set oWorkbook = oExcel.Workbooks.Open(file.path,,,,"password","password",True,,True,True)
full list of switches can be found here:
http://msdn.microsoft.com/en-us/library/office/ff194819%28v=office.15%29.aspx
hope that helps, took me hours to figure this out.
Related
I have a VBA script which will unlock some protected cells in a collection of excel workbooks, and am getting that lovely elusive "1004" error. The workbook actually opens (so the path is valid), but comes up with this error somewhere afterwards, but still within the Workbooks.Open command.
Through various sources, I have tried various combinations of the following lines, but it always fails after the workbook has opened and before returning control to VBA (when stepping through). Interestingly, it opens all the macro windows in the VBA IDE, but it doesn't highlight anything to indicate what should be the problem.
Dim wb As Workbook
Application.AutomationSecurity = msoAutomationSecurityLow
Application.DisplayAlerts = False
Set wb = Workbooks.Open([[valid file name]], IgnoreReadOnlyRecommended:=True, Editable:=True, CorruptLoad:=xlNormalLoad, AddtoMRU:=False)
I'm not sure if its to do with macros and trusts, or with protected sheets, but apparently the application security line should have solved the first and CorruptLoad the second. I'm sure there's some little thing I've missed - I just can't find it.
I've looked through the Workbooks.Open documentation online but there's no reference to protected sheets (CorruptLoad was gleaned out of a couple of different posts on people having similar-but-different issues).
#p77u77n77k was definitely on the right track, and essentially answered the question. Ensuring that "Trust Access to the VBA Project Object Model" is on certainly worked. In the process of researching, I found this script which can programmatically turn it on, if needed. Just one for the toolkit:
Macro to Enable Trust
Start to get Excel catastrophic failure error
On OK opening debug windows, with auto creating each time new sheets, which is empty and strange structure
If I want something to do appears
So how to delete those sheets? or fix that error?
No background process started, file stored in xlsm and xlsb format do the same things. workbook and worksheets is not protected.
It looks like the file has been corrupted. It is unlikelly the problem can be easily reproduced from scratch.
Never the less you can script a vba macro to delete Sheets based on their names or not delete the sheets you want to keep.
sheetnametodelete= "sheetname"
With Application.Workbooks(ThisWorkbook.Name())
.Unprotect (yourpassword) ' required if protection is set
Dim wks As Worksheet
Set wks = .Sheets(sheetnametodelete)
If (Not wks Is Nothing) Then ' also check if wks belong to the defined blacklist
wks.Delete
End If
.Protect (yourpassword) ' required if protection is set
End With
Try to open the file from another computer in case your local Excel config is corrupted.
I had a similar problem (a fake workbook duplicated) in the past and decided to script a build process for my Excel vba based application.
See following links to learn more about module management.
https://www.rondebruin.nl/win/s9/win002.htm
http://www.cpearson.com/excel/vbe.aspx
you can also look at this post
Import a cls files and create a sheet
It provides code and comments from other contributors.
This is obviously not direct answer to your problem but if you intend to work on a consistent vba project I recommand to save your vba code out of your Excel file once in a while and setup a build of your Excel app.
What is the best way of protecting a specific Excel workbook?
I have an inherited script that includes the following common lines at the end:
ActiveSheet.Protect "my-password"
ActiveWorkbook.Protect "my-password"
However, I've noticed that as the script can take a few minutes to run users often switch to a new unrelated workbook whilst it solves - whatever else they are working on. The password protection is then inherited by the unrelated workbook upon the completion of the macro - since whatever other Excel file the user is working within is now "Active" (presumably? this is my reading of the problem).
The above script is in a workbook that can be renamed to whatever the user chooses, and be saved in any number of directories. How can I ensure that only the original excel file is locked/unlocked by the Macro, when other workbooks are in use?
I am sure there are many ways to do this, but which is the most foolproof method?
NOTE: using office 365
Thanks Dean's answers in the comments:
Early in the code (and in Worksheet_Change if appropriate) enter the following to define your sheet as an object (named default_ws in my case):
Set default_ws = ActiveSheet
When you are ready to lock your sheet or workbook you can then use:
default_ws.Protect "password-here" 'protect your sheet
ThisWorkbook.Protect "password-here" 'protect your workbook
Also note:
You could also define your workbook as an object as follows if desired:
Set default_wb = ActiveWorkbook
I have a workbook in which i have several sheets according to requirement. In this workbook i have used macros and to lock some particular cells and columns i have used unprotect and protect options. Everything works fine on my machine but when i make it as SharedWorkBook it is giving me errors particularly the ThisWorkBook.Sheets("PSE").Unprotect and ThisWorkBook.Sheets("PSE").Protect statements. So in place of them i used ThisWorkBook.Sheets("PSE").UnprotectSharing and ThisWorkBook.Sheets("PSE").ProtectSharing . Even now also i am getting errors with those lines.
Sub SheetHider()
ThisWorkbook.Sheets("SheetA").UnprotectSharing
Cuser = ThisWorkbook.Sheets("SheetA").Range("A2").Value
and the error is Runtime error '438' Object doesn't support this property or method.
My Requirement: 1)I have to use Macros, Locking property so that i have to use Unprotect and Protect statements and these things should work when i make it as SharedWorkBook.
I have been searching many sites for this for a week. Atleast give me some alternatives to do this.
Any help will be appreciated greatly. Thanks
UnprotectSharing and ProtectSharing both belong to the Workbook object. So you can't do what you are trying to do.
You can do ThisWorkbook.UnprotectSharing and ThisWorkbook.ProtectSharing but that is only design to protect the Shared Workbook setting so that people can't remove it without a password.
Secondly you can't unprotect or protect sheets when a workbook is shared. You would have to unshare the workbook, unprotect the sheet and then share the workbook again. Which isn't very practicable.
You must protect sheets before protecting a shared workbook, unprotecting goes in the reverse order.
Actually, I'm not a beginner. I know nothing about them, though I do have some programming background.
This is the thing: I have a couple of Word documents and an Excel spreadsheets. The documents need to grab some data from the spreadsheet and then print. There needs to be one document per spreadsheet row, and they need to be printed all together.
I'm not asking for code or anything; I just want to know what's the right tool for the job, and if someone could point me to a tutorial or reference or something.
This is for Office 2003 (or XP, I'm not sure).
EDIT: It seems like there are many ways to do this, so it'd be great if someone listed the pros and cons of each solution. Keep in mind that it's something that will be done many times, and once programmed/recorded/whatever it should be easy to use for someone who is not a programmer.
Javier,
Couldn't find a good tutorial, but something like this should help you get going:
You can enable the developer toolbar, if it's not available from Word options. Then, click on the Visual Basic button and add a procedure or function that can be called from your document or a command button in the Word UI.
Sample showing some super basic Excel integration:
Public Function GetValue()
Dim myExcel As Excel.Application
Dim myWorkbook As Excel.Workbook
Dim myWorkSheet As Excel.Worksheet
Set myExcel = CreateObject("Excel.Application")
Set myWorkbook = myExcel.Workbooks.Open("c:\temp\myworkbook.xlsx")
Set myWorkSheet = myWorkbook.Worksheets(1)
Dim cellValue As String
cellValue = myWorkSheet.Cells(1, 1).Value
GetValue = cellValue
End Function
This will require you add a reference to Excel object library (type library) from the Excel developer IDE.
You can load your Excel spreadsheets via VBA in an ADODB and read the ADODB row by row.
How To Use ADO with Excel Data from Visual Basic or VBA
You can accomplish this with the built-in mail merge facility in Word. There's a walkthrough of how to use it in Word 2003 here
edit: further to the question in the comments, once you have set up the mail merge document, you can save it complete with its link to the data source. This means that when the document is opened again the user just needs to say "Yes" to the choice of data being merged.
The user can (independently) also choose to have the mail merge toolbar displayed. Clicking on the "Merge to new document" button on the Mail Merge toolbar would cause the merged letters to be generated. If the toolbar isn't displayed then they need to go Tools > Letters and Mailings > Mail Merge and use the wizard to complete the job