Consulting

Results 1 to 11 of 11

Thread: Solved: Shorter better code

  1. #1
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location

    Solved: Shorter better code

    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

    [VBA] 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 [/VBA]

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Perhaps something like this?
    [vba]
    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[/vba]
    By the way do you really need to create a scripting object to get the username?

  3. #3
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    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

    [VBA]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
    [/VBA] gibbo.

  4. #4
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    [VBA]Environ("USERNAME")[/VBA]
    K :-)

  5. #5
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    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
    [VBA]
    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


    [/VBA]
    Last edited by Tommy; 11-02-2005 at 09:29 AM. Reason: fixed code for max of 60 +- characters in a row :)

  6. #6
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    Thanks All,

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

    [VBA]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[/VBA]

    Gibbo

  7. #7
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  8. #8
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    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

  9. #9
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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?)
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  10. #10
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    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

  11. #11
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •