Results 1 to 11 of 11
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sunrise, Florida
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Syntax - What Am I Doing Wrong? (Word 2K)

    If I use the following code, everything works OK. However, if I change the "=" character for "<>", it doesn't. What am I doing wrong? Any help appreciated. Thanks!

    - - - - - - - VBA Code Begins - - - - -
    If strUserName = "User1" Or strUserName = " User2" Or strUserName = " User3" Then

    <instructions>

    Else

    <instructions>

    End If
    - - - - - - - VBA Code Ends - - - - -

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Syntax - What Am I Doing Wrong? (Word 2K)

    Assume that strUsername is equal to one of these values, and not equal to any of the others, then your test is equivalent to

    If TRUE or TRUE or FALSE or TRUE which will always be true. This particular formula could NEVER be false, since this would require that strUsername was equal to all of the options.

    I suspect that the test you want is
    <code>If strUserName <> "User1" And strUserName <> " User2" Or strUserName <> " User3" Then</code>
    this will give a TRUE result if strUsername is different to all of the possible names.

    StuartR

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sunrise, Florida
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Syntax - What Am I Doing Wrong? (Word 2K)

    What I would like the macro to do when someone attempts to open an instance of a template is:
    1. check for the user ID
    2. if it is "X", "Y" or "Z" user, I want the instance (document) to open.
    if the user is not "X", "Y" or "Z", I want the document to close without allowing any editing.

    Your usual help is appreciated. Thanks!

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Syntax - What Am I Doing Wrong? (Word 2K)

    The easiest way to do this would be something like...
    <code>
    If strUserName <> "User1" And strUserName <> " User2" And strUserName <> " User3" Then
    msgbox "Invalid user", vbOKOnly, "User check"
    ActiveDocument.Close
    End If</code>

    You could read this If statement as strUsername isn't User1, and it isn't User2 and it isn't USer3.

    StuartR

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Syntax - What Am I Doing Wrong? (Word 2K)

    Another way is:

    If [user is authorized] then
    'proceed through to the next instruction for this authorized user
    Else
    'close the document now for this unauthorized user
    End If

    Really, the question is which seems the most logical to you: VBA doesn't care. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  6. #6
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Syntax - What Am I Doing Wrong? (Word 2K)

    A simple Case statement would do the trick nicely:
    <pre>Select Case sUserName
    Case "User1", "User2", "User3"
    MsgBox "OK" ' Open document
    Case Else
    MsgBox "invalid user" ' Close document
    End Select
    </pre>

    Modifying the allowed users is just a matter of editing the first Case.

  7. #7
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sunrise, Florida
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Syntax - What Am I Doing Wrong? (Word 2K)

    <P ID="edit" class=small>(Edited by jscher2000 on 04-Jun-05 19:31. Tabs ( [tab] ) inserted to indent code for readability.)</P>I coded the following. It works beautifully for authorized users. However, for the unauthorized ones, it gives me this message before closing the active doc: Runtime error '4198' Command failed
    - - - - - - - Code Begins - - - - - -
    Private Sub Document_Close()
    Dim strUserName As String
    strUserName = Environ("Username")
    Select Case strUserName
    Case "User1", "User2", "User3"
    MsgBox "You are an authorized user. Click to continue.", vbOKOnly + vbExclamation, "WELCOME, " & UCase(strUserName) & "!"
    Exit Sub
    Case Else
    MsgBox "UnAuthorized User"
    Documents(1).Close SaveChanges:=wdDoNotSaveChanges
    End Select
    End Sub
    - - - - - - - Code Ends - - - - - -
    Thanks in advance!

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Syntax - What Am I Doing Wrong? (Word 2K)

    Perhaps there is a problem trying to use Document.Close inside the Document_Close() event handler? If the goal is to avoid saving changes, one approach is to use ActiveDocument.Saved = True to fool Word into believing that there are no unsaved changes. I don't know whether that works in Document_Close().

  9. #9
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Syntax - What Am I Doing Wrong? (Word 2K)

    How is this macro called? You said that you want to take action when a document is opened, but the routine appears to be a Document_Close event handler.

    How do you know that Documents(1) is the correct document to close?

    StuartR

  10. #10
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sunrise, Florida
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Syntax - What Am I Doing Wrong? (Word 2K)

    I'm trying to cover all bases. Maybe that's my problem. I just don't want users to be able to hold the "Shift" key down and bypass the code in Document_New and Document_Open. In essence, what I want is to allow authorized users to create documents from a template at the same time that I prevent non-authorized users from even opening a document from that same template. I've tried several options but it seems that doing it in Document_New, Document_Open and Document_Close is the best chance I have. Thanks to everyone for their input. Any additional help is appreciated. Thanks!

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Syntax - What Am I Doing Wrong? (Word 2K)

    You could protect the document for forms and in your Document_Open or Document_New, unlock the protection for authorized users. Yes, someone who views the code can harvest the password. You can make it harder by declaring the password as a constant in an obfuscated form and then using a function to descramble it. The password also can be removed by saving to RTF or opening in OpenOffice.org. But it does give you a second technique to discourage use of the Shift key to bypass the auto macros.

    (Using NTFS permissions still seems the best approach to this problem.)

Posting Permissions

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