Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 38

Thread: Select Case Oddity

  1. #1
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location

    Select Case Oddity

    Hi,

    I prepared a template for another person and everything worked fine on my end, but on their end they were not getting a prefix text as required.

    I isolated the problem to a Select Case statement, but I can't for the life of me figure out what the issue could be. As sample of the code is provided below. First a document variable is set to either "Test 1" or "Test 2" and then depending on that variable, two prefix strings are defined.

    For some reason on the other person's computer, the Select Case statement isn't working correctly. The expected value of the variable is present and returned, but on their system it just blows right passed the Case "Test 1" statements yet works like a champ using If ElseIf End If:-(

    Anyone seen anything like this before or have an explanation? Thanks.

    Sub CaseStatementOddity()
    Dim strPrefix1 As String
    Dim strPrefix2 As String
      Debug.Print ActiveDocument.Variables("varTest").Value 'Returned "Test 1"
      Select Case ActiveDocument.Variables("varTest").Value
        Case "Test 1"
          strPrefix1 = "A - "
          strPrefix2 = "B - "
        Case "Test 2"
          strPrefix1 = "C - "
          strPrefix2 = "D - "
      End Select
      Debug.Print strPrefix & " " & strPrefix2 'Returns "A -  B -" on my system.  Returns "" on other system.
    End Sub
    Sub WordAround()
    Dim strPrefix1 As String
    Dim strPrefix2 As String
      If ActiveDocument.Variables("varTest").Value = "Test 1" Then
        strPrefix1 = "A - "
        strPrefix2 = "B - "
      ElseIf ActiveDocument.Variables("varTest").Value = "Test 2" Then
        strPrefix1 = "C - "
        strPrefix2 = "D - "
      End If
      Debug.Print strPrefix & " " & strPrefix2 'Returns "A -  B -" on both systems.
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Weird. If you give strPrefix1 and strPrefix2 a value BEFORE the Select Case, do they end up the same, or do they change to ""

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Are we comparing the same strings ?
    Select case seems to be non case sensitive.

    Sub M_snb()
        MsgBox ActiveDocument.FullName
        MsgBox ThisDocument.FullName
    
        MsgBox ThisDocument.Variables("varTest")
        MsgBox ActiveDocument.Variables("varTest")
    
        MsgBox Len(ThisDocument.Variables("varTest"))
        MsgBox Len(ActiveDocument.Variables("varTest"))
    
        MsgBox StrComp(ActiveDocument.Variables("varTest"), "Test 1", vbBinaryCompare)
        
        Select Case lcase(ThisDocument.Variables("varTest"))
        Case "test 1"
            strPrefix1 = "A - "
            strPrefix2 = "B - "
        Case "test 2"
            strPrefix1 = "C - "
            strPrefix2 = "D - "
        End Select
    
        msgbox strprefix1 & strprefix2
    End Sub

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Gerry,

    I'll have to confirm with the guy on the other end, but I don't thing they would change back to "". I watched him step through his code line by line. When Case "Test 1" is highlighted and then executed, the highlight jumps to Case "Test 2" completely bypassing what it should normally do.

    Now there is more information and I really should have reported this from the beginning.

    If he creates a new document based on the template then all works as expected. The Case statement executes as required. However, when he attaches the template to existing file created with a different template, the problem occurs. However, he did send me one of these pre-existing files and when I attach the template is still works as expected.

    Baaaaafulled ;-)
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    snb,

    PSTW.jpg
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location
    Select Case Trim(ActiveDocument.Variables("varTest").Value)
    
    Maybe there's a trailing space??


    Paul

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Exactly what I 'see' using:

       ActiveDocument.Variables("varTest") = "B767 "
        MsgBox StrComp(ActiveDocument.Variables("varTest"), "B767", vbBinaryCompare)
        Debug.Print ActiveDocument.Variables("varTest")
    That's why I introduced strcomp

  8. #8
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Guys (Paul, snb),

    You're kind of losing me. There is no trailing space in the document variable. If there were, then I don't thing the If statement wouldn't work either, right?

    The document variable value is "Test 1"
    The result of the Debug.Print is "Test 1"
    The If condition is "Test 1" and it works on my system and his.
    The Case is "Test 1" and it works on my system and his if he creates a new document from the template, but it doesn't work if he attaches the template to an existing document.

    I'm completely stumped, but unless someone has seen this before, I'm willing to concede to the adage that Word never cease to perplex ;-)
    Greg

    Visit my website: http://gregmaxey.com

  9. #9
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    So it is NOT a factor of your system versus another system, but a factor of a document created from the template versus ATTACHING the template. Hmmmm. That speaks to the document variable. But...you say Debug.Print returns the correct value. Hmmmm. yup another Word perplex.

  10. #10
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Gerry,

    Yes and no. I can repeat the steps that he does to produce the error on his end but it doesn't reproduce it on my end. Well, I don't have the original template on my end so that might be a factor. Regardless, it definitely twists the rope. When the guy first reported it and looking at the code, my initial reaction was "impossible." Well, I ate that crow and started picking feathers out of my teeth when I watched him step through the code on his PC via Skype and observed execution blow right past the Case statement. Fortunately the If statement worked or I would still be bouncing off the walls ;-)
    Greg

    Visit my website: http://gregmaxey.com

  11. #11
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    What happens if you change
      Select Case ActiveDocument.Variables("varTest").Value 
        Case "Test 1"
    to
    Dim MyVar As [Variant|String]
    MyVar = ActiveDocument.Variables("varTest").Value
    Select Case MyVar
        Case "Test 1"
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  12. #12
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Sam,

    Nothing happens here of course, because it works either way. I'm not positive I can get the guy on the other end to troubleshoot further or not. He has a working solution (the If Elseif End If method) and may have lost interest. I'll try though. Thanks.
    Greg

    Visit my website: http://gregmaxey.com

  13. #13
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Yes and no. I can repeat the steps that he does to produce the error on his end but it doesn't reproduce it on my end. Well, I don't have the original template on my end so that might be a factor.
    You do not have the actual file that is attached as a template? Hmmm. Bummer.

  14. #14
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Without seeing the 'culprit' (nor the if..elseif solution) it's hard to tell.

  15. #15
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Well, like in the original post, the workaround (misspelled wordaround) I posted here:

    Sub WordAround() 
        Dim strPrefix1 As String 
        Dim strPrefix2 As String 
        If ActiveDocument.Variables("varTest").Value = "Test 1" Then 
            strPrefix1 = "A - " 
            strPrefix2 = "B - " 
        ElseIf ActiveDocument.Variables("varTest").Value = "Test 2" Then 
            strPrefix1 = "C - " 
            strPrefix2 = "D - " 
        End If 
        Debug.Print strPrefix & " " & strPrefix2 'Returns "A -  B -" on both systems.
    End Sub
    So you can see that the variables value actually is "Test 1" and VBA is quiet content to evaluate it as such.
    Greg

    Visit my website: http://gregmaxey.com

  16. #16
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    VBA is quiet content to evaluate it as such
    Better that than standing up and screaming about it...

  17. #17
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Sam,

    No difference on the other end either. To make matters worse, he reported that the If ElseIf End If work around we implemented yesterday is now failing in a similar manner!!!

    I totally stumped and he is losing interest in furthering troubleshooting so I guess I'll have to fine this one off as a freak experience.

    Thanks to everyone who offered comment and suggestions.
    Greg

    Visit my website: http://gregmaxey.com

  18. #18
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    And there is no way to get a hold of that actual template file?

  19. #19
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Gerry,

    Considered asking, but the same thing happens when he creates a new document based on his normal.dotm and then attaches my template.

    If he creates a new document based on my template (which is what it was really designed for), then both the Case and If statement methods of evaluating and document variable work fine.

    Then two days I observed the Case method fail when he attached my template to one of his existing documents. That is when I proposed and wrote the If method and watched as he confirmed it worked.

    Then he attached my template to another one of his documents yesterday and the IF method was failing. It was at this point that he told me that if he creates a new blank document and then attaches my template both the Case and If methods fail to set the prefix values.


    I certainly can't reproduce the behavior here with my normal template so I'm thinking now it must be something else. The guy said it was no big deal as he rarely has to attach the template and I think he just wants to move on.
    Greg

    Visit my website: http://gregmaxey.com

  20. #20
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Maybe so, but it nevertheless would be interesting to get a hold of that template so we can try and duplicate things in multiple scenarios. It is a unusual and interesting situation; it does not make any sense. There has to be SOME reason...OK, maybe not considering it is Word and VBA, but still. I have never heard of something making Select Case fail. I do not see how anything could as that is a logical operation. It is like something making 2 + 2 not equal 4.

Posting Permissions

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