Log in

View Full Version : Solved: Shorter better code



gibbo1715
11-02-2005, 07:48 AM
I have three buttons on a user from that lock different parts of a document dependant on which button is pressed, what I would like to do is ensure my code is as efficient as it can be, can anyone look at the code below and suggest any improvements please, or a way of shortening my code

cheers

gibbo

Private Sub CommandButton1_Click()
On Error GoTo Err
ActiveDocument.Unprotect Password:="test"
ActiveDocument.Bookmarks("Log").Range.Text = CreateObject("WScript.Network").UserName _
& " Saved the application at " _
& Format(Now, "hh:mm dd/mm/yy") & vbNewLine
ActiveDocument.Sections(1).ProtectedForForms = True
ActiveDocument.Sections(2).ProtectedForForms = False
ActiveDocument.Sections(3).ProtectedForForms = False
ActiveDocument.Sections(4).ProtectedForForms = True
ActiveDocument.Protect Password:="test", noreset:=False, Type:= _
wdAllowOnlyFormFields
UserForm1.Show
Exit Sub
Err:
MsgBox "This Document cannot be sent, Please ensure you are pressing the correct button", vbCritical + vbOKOnly, "Error"
End Sub
Private Sub CommandButton2_Click()
On Error GoTo Err
If ActiveDocument.Sections(1).ProtectedForForms = True Then
ActiveDocument.Unprotect Password:="test"
ActiveDocument.Bookmarks("Log").Range.Text = CreateObject("WScript.Network").UserName _
& " Saved the application at " _
& Format(Now, "hh:mm dd/mm/yy") & vbNewLine
ActiveDocument.Sections(1).ProtectedForForms = True
ActiveDocument.Sections(2).ProtectedForForms = True
ActiveDocument.Sections(3).ProtectedForForms = False
ActiveDocument.Sections(4).ProtectedForForms = True
ActiveDocument.Protect Password:="test", noreset:=False, Type:= _
wdAllowOnlyFormFields
UserForm2.Show
End If
Exit Sub
Err:
MsgBox " This Document cannot be sent, Please ensure you are pressing the correct button", vbCritical + vbOKOnly, "Error"
End Sub
Private Sub CommandButton3_Click()
On Error GoTo Err
If ActiveDocument.Sections(1).ProtectedForForms = True And ActiveDocument.Sections(2).ProtectedForForms = True Then
ActiveDocument.Unprotect Password:="test"
ActiveDocument.Bookmarks("Log").Range.Text = CreateObject("WScript.Network").UserName _
& " Saved the application at " _
& Format(Now, "hh:mm dd/mm/yy") & vbNewLine
ActiveDocument.Sections(1).ProtectedForForms = True
ActiveDocument.Sections(2).ProtectedForForms = True
ActiveDocument.Sections(3).ProtectedForForms = True
ActiveDocument.Sections(4).ProtectedForForms = True
ActiveDocument.Protect Password:="test", noreset:=False, Type:= _
wdAllowOnlyFormFields
UserForm3.Show
Else
MsgBox "Parts 1 and 2 Must be completed first", vbCritical + vbOKOnly, "Error"
End If
Exit Sub
Err:
MsgBox "This Document cannot be sent, Please ensure you are pressing the correct button", vbCritical + vbOKOnly, "Error", vbCritical + vbOKOnly, "Error"
End Sub

Norie
11-02-2005, 08:42 AM
Perhaps something like this?

Private Sub CommandButton1_Click()
On Error GoTo Err

With ActiveDocument
.Unprotect Password:="test"
.Bookmarks("Log").Range.Text = CreateObject("WScript.Network").UserName _
& " Saved the application at " _
& Format(Now, "hh:mm dd/mm/yy") & vbNewLine
.Sections(1).ProtectedForForms = True
.Sections(2).ProtectedForForms = False
.Sections(3).ProtectedForForms = False
.Sections(4).ProtectedForForms = True
.Protect Password:="test", noreset:=False, Type:=wdAllowOnlyFormFields
End With

UserForm1.Show

Exit Sub
Err:
MsgBox "This Document cannot be sent, Please ensure you are pressing the correct button", vbCritical + vbOKOnly, "Error"

End Sub
By the way do you really need to create a scripting object to get the username?

gibbo1715
11-02-2005, 08:54 AM
Thanks Norie, Thats the only way I ve ever known how to get the user name so if theres a simpler method can you tell me what it is,

Also ammended my code as follows now, still looking for imporvements though if you ve got any ideas

cheers

Private Sub CommandButton1_Click()
On Error GoTo Err
Call Locking
With ActiveDocument
.Sections(2).ProtectedForForms = False
.Sections(3).ProtectedForForms = False
.Protect Password:="test", noreset:=False, Type:=wdAllowOnlyFormFields
End With
UserForm1.CommandButton1.Visible = True
UserForm1.Show
Exit Sub
Err:
MsgBox "This Document cannot be sent, Please ensure you are pressing the correct button", vbCritical + vbOKOnly, "Error"
End Sub
Private Sub CommandButton2_Click()
On Error GoTo Err
If ActiveDocument.Sections(1).ProtectedForForms = True Then
Call Locking
With ActiveDocument
.Sections(2).ProtectedForForms = True
.Sections(3).ProtectedForForms = False
.Protect Password:="test", noreset:=False, Type:=wdAllowOnlyFormFields
End With
UserForm1.CommandButton3.Visible = True
UserForm1.Show
Else
MsgBox "Part 1 Must be completed first", vbCritical + vbOKOnly, "Error"
End If
Exit Sub
Err:
MsgBox " This Document cannot be sent, Please ensure you are pressing the correct button", vbCritical + vbOKOnly, "Error"
End Sub
Private Sub CommandButton3_Click()
On Error GoTo Err
If ActiveDocument.Sections(1).ProtectedForForms = True And ActiveDocument.Sections(2).ProtectedForForms = True Then
Call Locking
With ActiveDocument
.Sections(2).ProtectedForForms = True
.Sections(3).ProtectedForForms = True
.Protect Password:="test", noreset:=False, Type:=wdAllowOnlyFormFields
End With
UserForm1.CommandButton2.Visible = True
UserForm1.Show
Else
MsgBox "Parts 1 and 2 Must be completed first", vbCritical + vbOKOnly, "Error"
End If
Exit Sub
Err:
MsgBox "This Document cannot be sent, Please ensure you are pressing the correct button", vbCritical + vbOKOnly, "Error", vbCritical + vbOKOnly, "Error"
End Sub
Sub Locking()
ActiveDocument.Unprotect Password:="test"
ActiveDocument.Bookmarks("Log").Range.Text = CreateObject("WScript.Network").UserName _
& " Saved the application at " _
& Format(Now, "hh:mm dd/mm/yy") & vbNewLine
ActiveDocument.Sections(1).ProtectedForForms = True
ActiveDocument.Sections(4).ProtectedForForms = True
End Sub
gibbo.

Killian
11-02-2005, 09:15 AM
Environ("USERNAME")

Tommy
11-02-2005, 09:25 AM
Hi gibbo,

This may or may not be what you are looking for. :) I thought I would give it a shot anyway:)

Environ$("USERNAME") will return the username

Private Sub CommandButton1_Click()
On Error GoTo Err
Call Locking
Call Protect
Exit Sub
Err:
MsgBox "This Document cannot be sent, Please ensure you" & _
" are pressing the correct button", vbCritical + vbOKOnly, "Error"
End Sub
Private Sub CommandButton2_Click()
On Error GoTo Err
If ActiveDocument.Sections(1).ProtectedForForms = True Then
Call Locking
Call Protect(True)
Else
MsgBox "Part 1 Must be completed first", vbCritical + vbOKOnly, "Error"
End If
Exit Sub
Err:
MsgBox " This Document cannot be sent, Please ensure you are " & _
"pressing the correct button", vbCritical + vbOKOnly, "Error"
End Sub
Private Sub CommandButton3_Click()
On Error GoTo Err
If ActiveDocument.Sections(1).ProtectedForForms = True And ActiveDocument.Sections(2).ProtectedForForms = True Then
Call Locking
Call Protect(True, True)
Else
MsgBox "Parts 1 and 2 Must be completed first", vbCritical + vbOKOnly, "Error"
End If
Exit Sub
Err:
MsgBox "This Document cannot be sent, Please ensure you are pressing " & _
"the correct button", vbCritical + vbOKOnly, "Error", vbCritical + vbOKOnly, "Error"
End Sub
Sub Locking()
With ActiveDocument
.Unprotect Password:="test"
.Bookmarks("Log").Range.Text = Environ$("USERNAME") _
& " Saved the application at " _
& Format(Now, "hh:mm dd/mm/yy") & vbNewLine
.Sections(1).ProtectedForForms = True
.Sections(4).ProtectedForForms = True
End With
End Sub
Sub Protect(Optional Sect2 = False, Optional Sect3 = False)
With ActiveDocument
.Sections(2).ProtectedForForms = Sect2
.Sections(3).ProtectedForForms = Sect3
.Protect Password:="test", noreset:=False, Type:=wdAllowOnlyFormFields
End With
If Not Sect2 And Not Sect3 Then
UserForm1.CommandButton1.Visible = True
ElseIf Sect2 And Not Sect3 Then
UserForm1.CommandButton3.Visible = True
ElseIf Sect2 And Sect3 Then
UserForm1.CommandButton2.Visible = True
End If
UserForm1.Show
End Sub

gibbo1715
11-02-2005, 09:50 AM
Thanks All,

Gives me loads of ideas
think I can merge tommy s idea as well to

Sub Protect(Optional Sect2 = False, Optional Sect3 = False)
With ActiveDocument
.Unprotect Password:="test"
.Bookmarks("Log").Range.Text = Environ$("USERNAME") _
& " Saved the application at " _
& Format(Now, "hh:mm dd/mm/yy") & vbNewLine
.Sections(1).ProtectedForForms = True
.Sections(4).ProtectedForForms = True
.Sections(2).ProtectedForForms = Sect2
.Sections(3).ProtectedForForms = Sect3
.Protect Password:="test", noreset:=False, Type:=wdAllowOnlyFormFields
End With
If Not Sect2 And Not Sect3 Then
UserForm1.CommandButton1.Visible = True
ElseIf Sect2 And Not Sect3 Then
UserForm1.CommandButton3.Visible = True
ElseIf Sect2 And Sect3 Then
UserForm1.CommandButton2.Visible = True
End If
UserForm1.Show
End Sub

Gibbo

TonyJollans
11-02-2005, 11:01 AM
Hi gibbo,

I'm confused as to what you're doing with the document. ProtectedForForms means that the user can only type in designated Form Fields.

So you are originally setting sections 1 and 4 as restricted and sections 2 and 3 as freely updateable - and then showing a UserForm so the user can't update the document at all till they've dispatched it.

At some point later you add protection for forms to section 2, etc.

You seem to have a document with form fields and yet are manipulating the document so that the users can sometimes only enter data in the form fields and can sometimes freely edit a section. When they can freely edit, they can delete (quite possibly accidentally) one or more of your form fields - or even add new ones. I don't see, how, under those circumstances you can end up with anything certain to be what you want.

gibbo1715
11-02-2005, 11:45 AM
Tony

thanks for the reply

I have a form that needs to work its way through a process of authorisation and as each section is completed it needs to be locked, before it moves on the next stage and person. sections1,2 and 3.

I am using section 4 to keep a log of the changes, ( I know I havnt written the save document part yet)

My users wont accidently change anything they shouldnt as we ve been using the same unprotected form for about 2 years now and never had any problems

Hence the weird approach

Hope that makes sense more or less

Gibbo

TonyJollans
11-02-2005, 11:56 AM
Hi gibbo,

Thanks, but protecting for forms doesn't lock the form fields. So either you have well behaved users or you don't have form fields (in which case why protect for forms?)

gibbo1715
11-02-2005, 12:06 PM
I have no form fields but thought this was the only way i could lock part of my document using continuos breaks?

I assume I should be using a different form of protect then?

Gibbo

TonyJollans
11-02-2005, 01:36 PM
Well .. it depends on your Word version - and I suppose what you have does what you want if you have no form fields. I just didn't register it when I saw the code.