[Solved] Autofill Password in Macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
KCuHCeht
Posts: 12
Joined: Mon Apr 09, 2012 5:23 pm

[Solved] Autofill Password in Macro

Post by KCuHCeht »

Ok, This is my first post, and I'm fairly new to OpenOffice Macros. What I am trying to do is create a macro to protect a sheet after another macro is run that unprotects and copies a master sheet. I want it to automatically fill in the password so that the end user doesn't have the option to enter the password, or even know that it happened. For the sake of discussion, let's make the password "Pass1" EDIT: Also, what if the password were blank?

Here is what i have so far:

Code: Select all

sub ProtectSheet
dim document   as object
dim dispatcher as object
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Protect"
args1(0).Value = true
dispatcher.executeDispatch(document, ".uno:Protect", "", 0, args1())
end sub
I'm sure it is an easy solution, but I can't find it anywhere that I have looked. Please help.

Thanks in advance.
Last edited by Hagar Delest on Mon Apr 09, 2012 9:40 pm, edited 1 time in total.
Reason: tagged [Solved].
CHuCK

OpenOffice.org 3.2.1 OOO320m18 (Build:9502) Installed on Windows 7 Professional x64
User avatar
JohnSUN-Pensioner
Volunteer
Posts: 876
Joined: Fri Jan 14, 2011 1:21 pm
Location: Kyiv, Ukraine

Re: Autofill Password in Macro

Post by JohnSUN-Pensioner »

Hi, CHuCK! And welcome to the forum!

Code: Select all

Sub SimpleProtect
Dim oSheets As Variant
Dim oSheet As Variant
Const myPwd = "Pass1"
	oSheets = ThisComponent.getSheets()
	oSheet = oSheets.getByIndex(0)
	MsgBox ("Now we will protect the worksheet '" +oSheet.getName()+ "' with a password '" + myPwd + "'"
	oSheet.protect(myPwd)
	MsgBox ("Now the worksheet '" +oSheet.getName()+ "' protected with a password '" + myPwd + "'. Unprotect it..."
	oSheet.unprotect(myPwd)
End Sub
I may not have a lot to give but what I got I'll give to you...
Apache OpenOffice 4.1.5, LibreOffice 6.4.4.2 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
KCuHCeht
Posts: 12
Joined: Mon Apr 09, 2012 5:23 pm

Re: Autofill Password in Macro

Post by KCuHCeht »

Thanks. I think that is working.
is there a quick way to make that protect and unprotect all the sheets withing the workbook? I know with excel you could do a count and then a for-while loop...
CHuCK

OpenOffice.org 3.2.1 OOO320m18 (Build:9502) Installed on Windows 7 Professional x64
User avatar
JohnSUN-Pensioner
Volunteer
Posts: 876
Joined: Fri Jan 14, 2011 1:21 pm
Location: Kyiv, Ukraine

Re: Autofill Password in Macro

Post by JohnSUN-Pensioner »

Here is the same
All worksheets are protected by one password? Or every worksheet is protected for its unique password?

Code: Select all

Sub multProtect
Dim oSheets As Variant
Dim oSheet As Variant
Dim i&
Const myPwd = "Pass1"
	oSheets = ThisComponent.getSheets()
	MsgBox ("Now we will protect ALL worksheets with a password '" + myPwd + "'"
	For i = 0 to oSheets.getCount()-1
		oSheet = oSheets.getByIndex(i)
		oSheet.protect(myPwd)
	Next i
' And Unprotect:
	For i = 0 to oSheets.getCount()-1
		oSheet = oSheets.getByIndex(i)
		oSheet.Unprotect(myPwd)
	Next i
	MsgBox ("Now we will protect ALL worksheets with a tricky password"
	For i = 0 to oSheets.getCount()-1
		oSheet = oSheets.getByIndex(i)
		oSheet.protect(myPwd + oSheet.getName())
	Next i
' And Unprotect:
	For i = 0 to oSheets.getCount()-1
		oSheet = oSheets.getByIndex(i)
		oSheet.Unprotect(myPwd + oSheet.getName())
	Next i
End Sub
I may not have a lot to give but what I got I'll give to you...
Apache OpenOffice 4.1.5, LibreOffice 6.4.4.2 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
KCuHCeht
Posts: 12
Joined: Mon Apr 09, 2012 5:23 pm

Re: Autofill Password in Macro

Post by KCuHCeht »

Just one Password for all the sheets.

WORKS LIKE A CHARM. Thanks.
CHuCK

OpenOffice.org 3.2.1 OOO320m18 (Build:9502) Installed on Windows 7 Professional x64
Post Reply