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

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Syntax – What Am I Doing Wrong? (Word 2K)

    Author
    Topic
    #420300

    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

    Else

    End If
    – – – – – – – VBA Code Ends – – – – –

    Viewing 0 reply threads
    Author
    Replies
    • #951300

      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
      If strUserName "User1" And strUserName " User2" Or strUserName " User3" Then
      this will give a TRUE result if strUsername is different to all of the possible names.

      StuartR

      • #951321

        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!

        • #951328

          The easiest way to do this would be something like…

          If strUserName "User1" And strUserName " User2" And strUserName " User3" Then
          msgbox "Invalid user", vbOKOnly, "User check"
          ActiveDocument.Close
          End If

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

          StuartR

        • #951352

          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. smile

        • #951388

          A simple Case statement would do the trick nicely:

          Select Case sUserName
              Case "User1", "User2", "User3"
                  MsgBox "OK" ' Open document
              Case Else
                  MsgBox "invalid user" ' Close document
          End Select
          

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

          • #951483

            (Edited by jscher2000 on 04-Jun-05 19:31. Tabs ( [tab] ) inserted to indent code for readability.)

            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!

            • #951485

              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().

            • #951491

              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

            • #951519

              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!

            • #951546

              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.)

    Viewing 0 reply threads
    Reply To: Syntax – What Am I Doing Wrong? (Word 2K)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: