Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 38 of 38

Thread: Select Case Oddity

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

    I've asked the template owner if he want to share it. Part of the issue I think is my actual template, his old template and the document contents may not be something that he wants to share with a wider audience.

    I agree with you it is a very unusual situation and I am completely baffled.
    Greg

    Visit my website: http://gregmaxey.com

  2. #22
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I think it's something on the other guy's office IT system.
    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

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

    I've also invited him to read this thread. If he does, he may see something that we've both overlooked that will explain it.
    Greg

    Visit my website: http://gregmaxey.com

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

    The template owner sent me his old template and a copy of his normal.dotm. I created new documents based on both, saved them, reopened and attached the new template and it worked fine here.

    We he does that, the Debug.Print variable value reports as expected but the Case Variable Value fails to execute.

    He may have read this post because he said that he reinstalled Office 365 and it had no effect.

    I’m afraid that unless some guy or gal smarter than me is sitting at his PC to see the behavior there isn’t much else that he or I can think of to do.

    Thanks.

    Best Regards,
    Greg Maxey
    Greg

    Visit my website: http://gregmaxey.com

  5. #25
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Office 365? Hrrm... I'll take a wild-guess that you're having an issue with the identified document object (especially since the IF...ElseIf...EndIf started failing too).

    You might want to break apart the code a good bit more... get a document object, use a function to return the value of a passed document object as well as the name of a variable... and if either thing fails, then have that function return an empty string.

    I don't trust that ActiveDocument is always reporting what you think during execution and stepping through. I'm betting something is getting confused, and you've got a null being thrown around.

    But all guesses

  6. #26
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Sorry, just to be clear-- I don't have any real ideas, except that I would break apart the actual line of code causing the issue into constituent parts and error trapped functions. Somewhat similar to SamT's suggestion of setting the value to a variable, and then testing the value of the variable. But I'd go further and get rid of the use of ActiveDocument, which could be the root of the problem for some reason.

    Are you testing the code on your end in Office 365 too?

  7. #27
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Certainly testing for a Set document object rather than ActiveDocument may be a good idea. I would be very surprised if the issue is Select Case itself. I just can not see it. It is a basic logical operation so deeply inherent to VBA that I just can not see how it can be broken. SOMETHING is screwing up the value of the variable.

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

    So how would you adapt this code? I'll ask him to try a modified version.


    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

  9. #29
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    That is a small part of the code you are using and we have pretty much eliminated it as a candidate for the problem.

    Why modify it? Except for the use of "ActiveDocument," it is as good as it gets.

    Show us the actual procedure and it's calling procedure, and, if any, the called functions and procedures.
    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

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

    I am not at liberty to post the complete template. It belongs to another person. I posted here thinking that someone might have encountered this issue before and could offer and explanation. He did report that he attempted the code on another machine that isn't using Office 365. Maybe it is a regional settings thing, but I just don't know.
    Greg

    Visit my website: http://gregmaxey.com

  11. #31
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Well at least try changing ActiveDocument to a set document object. Shrug.

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

    Do you mean

    Dim oDoc as Document
    Set oDoc = ActiveDocument
    Greg

    Visit my website: http://gregmaxey.com

  13. #33
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    He might, but what I mean is go up to the top calling routine, and whatever the active document is there that you want, set oDoc = ActiveDocument there, and then don't use active document anywhere else. Just pass the oDoc variable to any child routines. This eliminates the possibility that somehow, some way, the ActiveDocument is being changed by one of your subroutines in certain circumstances. That's how I'd troubleshoot next.

  14. #34
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Heck as it is we do not know what, if any, calling routines may be in use. But...yeah. I just can not believe that logical operations like Select Case or If..ElseIf...End if can be broken so (apparently) arbitrarily. Something is wonky and I for one find it hard to accept the blow to VBA if is Select Case or If themselves. Isolating the routine to the action I s good place to start.

  15. #35
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Gerry\Jason\Sam\Others,

    Setting ActiveDocument = a publicly declared p_oDoc variable made no difference on the other user's end.


    As reported, admittedly late, he is able to create new documents based on the template with no issues. However, if he attaches the template to and existing file, then it breaks down as described.

    I am not a liberty to post the complete template here. I wish I could because I am really interested in finding out what is causing the issue. The problem to the template owner is not so large that he wants to make public his file so there is nothing else I can do.

    Thanks again for your interest and for offering suggestions.
    Greg

    Visit my website: http://gregmaxey.com

  16. #36
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    If a template contains a variable "vartest".
    If you create a new document based on that template, or attach that template to a Word document you can read that variable using this macro in the document (not in the template):

    sub M_snb()
      Select Case activedocument.variables("vartest")
      Case "Test 1"
        msgbox "'correct"
      End Select
    End Sub
    What you are 'reading' exactly is the value of the variable 'vartest' in the template.
    If you also have a variable 'vartest' in the document, VBA has to decide which variable to take.
    It could result in unpredictable outcomes.

  17. #37
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Have you tested this, snb? Because it seems like you might be confusing/conflating what is supposed to be the difference in behavior between ThisDocument and ActiveDocument.

    ThisDocument (a document object, as opposed to the ThisDocument module) refers to the document which contains the calling code.
    ActiveDocument refers to the document currently "Active" (and visible-- invisible documents can't be active) in the Documents collection.

    This would be the first time I've seen the ActiveDocument object work like the ThisDocument object, which seems to be what you're describing. VBA doesn't have to decide which variable to read (in my experience) -- it reads the variable that it is supposed to, in the object that you've told it to look in (again, in my experience-- but I haven't used Office 2013 or Office 365 yet, and wouldn't be surprised to find some new bugs).

    In Word 2010 and prior... if a template contains a variable called "vartest" -- any documents created based on that template will also have a document variable called "vartest", with the same value as the template at the time the document is created.

    However, if a template contains a variable called "vartest", and you attach the activedocument (without that variable) to that template... then a document variable "vartest" looked for in ActiveDocument should return nothing or null.

  18. #38
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    But this does beg a question, Greg... where is the code stored? In a global addin, or is it stored in some kind of template. Because if you're running code and swapping templates which contain code, you're obviously going to have some issues. Without giving the whole template-- what's the actual structure of where code resides?

Posting Permissions

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