Curious about odd construction: "#If OutlookEnabled"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prn
    Recognized Expert Contributor
    • Apr 2007
    • 254

    Curious about odd construction: "#If OutlookEnabled"

    I have some inherited code that I don't understand properly. It involves sending email from Access using OLE and Outlook. I've stripped it down to the relevant bits here. Specifically, the construction that (I know that) I don't understand is this:

    [code=vb]
    #If OutlookEnabled Then
    Dim olApp As Outlook.Applica tion
    Dim olNewMaiItem As MailItem
    Set olApp = CreateObject("O utLook.Applicat ion")
    MsgBox "olApp created - Version = " & olApp.Version
    #Else
    MsgBox "Outlook not enabled"
    #End If
    [/code]

    (Of course, instead of just displaying the "olApp created" MsgBox, it really does a bunch of other stuff, but I think I mostly understand that part.)

    The boolean "OutlookEnabled " is not defined or set anywhere in the module, but it does seem to have a value. At least, if I change its spelling and run the routine, I get the message box saying "Outlook not enabled", so it looks like it somehow gets the value True.

    Now, of course, I would expect that OutlookEnabled would really depend on the status outside of Access so I'm not really surprised that it's not set by VBA within Access. The question is where? Pressing F2 gives me the Object Browser, where I can select the Outlook library, for example and browse around for useful objects and properties so that I can refer, e.g. to:

    olApp.Version

    But OutlookEnabled does not seem to be among the properties (including globals) that I can find there. So where does it come from?

    The other obvious quesion I have about the quoted code snippet is: what's up with the "#If" The hash symbol does appear to be meaningful and necessary. But in my admittedly limited library, the "#If" construction is supposed to be stuff that is conditionally compiled, e.g, for debugging, which would lead me to expect that without the #s the whole If-Then-Else-EndIf would be compiled and used all the time. However, if I remove the #s then I get the "Outlook not enabled" message instead of the version message.

    In sum, it appears that there is a (system-wide?) variable OutlookEnabled that can only be referred to within a condition using #If.

    Why should this be? And what else works like this? Obviously I can simply leave it as it is and use it this way, but somehow, it seems like it might possbly be useful to me in the future if I had some clue about where this came from and what else I might learn from it. :)

    Any clues will be gratefully accepted.

    Thanks,
    Paul
  • FishVal
    Recognized Expert Specialist
    • Jun 2007
    • 2656

    #2
    Hello, prn.

    "OutlookEnabled " could be nothing but conditional compiler constant declared with "#Const" conditional compiler directive. Check Access help for this.

    Regards,
    Fish

    Comment

    • prn
      Recognized Expert Contributor
      • Apr 2007
      • 254

      #3
      Thanks, FishVal. I'm not sure what to make of this, but I appreciate it.

      There are at least two tests that I see for the hypothesis that it is a conditional compilation constant.

      First, I have searched the entire project. Each and every occurrence of the string "OutlookEnabled " is in the "#If OutlookEnabled" construction, There is not a single instance of "#Const OutlookEnabled" or even of "#Const" anywhere, in that module or any of the others.

      Second, if I construct a clear case of a conditional constant:

      Code:
          #Const Foo = True
          #If Foo Then
              MsgBox "Foo is conditionally true"
          #Else
              MsgBox "Foo is conditionally false"
          #End If
          
          If Foo Then
              MsgBox "Foo is UNconditionally true"
          Else
              MsgBox "Foo is UNconditionally false"
          End If
      Running this gives two message boxes (as expected) with the results:
      "Foo is conditionally true"
      and
      "Foo is UNconditionally false"

      So the normal If-Then construction (without #) does appear to treat Foo the same way OutlookEnabled appears to work, that is as False.

      So I have two tests that seem to give different results. OutlookEnabled does behave the same way as a conditionally defined constant (or variable?), but does not seem to be defined within the vba that Access shows me.

      Any ideas?

      Thanks,
      Paul

      Comment

      • FishVal
        Recognized Expert Specialist
        • Jun 2007
        • 2656

        #4
        Originally posted by prn
        Thanks, FishVal. I'm not sure what to make of this, but I appreciate it.

        There are at least two tests that I see for the hypothesis that it is a conditional compilation constant.

        First, I have searched the entire project. Each and every occurrence of the string "OutlookEnabled " is in the "#If OutlookEnabled" construction, There is not a single instance of "#Const OutlookEnabled" or even of "#Const" anywhere, in that module or any of the others.
        :) Seek, seek. It must be.
        ... Sorry for the old joke.

        Second, if I construct a clear case of a conditional constant:

        Code:
            #Const Foo = True
            #If Foo Then
                MsgBox "Foo is conditionally true"
            #Else
                MsgBox "Foo is conditionally false"
            #End If
            
            If Foo Then
                MsgBox "Foo is UNconditionally true"
            Else
                MsgBox "Foo is UNconditionally false"
            End If
        Running this gives two message boxes (as expected) with the results:
        "Foo is conditionally true"
        and
        "Foo is UNconditionally false"

        So the normal If-Then construction (without #) does appear to treat Foo the same way OutlookEnabled appears to work, that is as False.

        So I have two tests that seem to give different results. OutlookEnabled does behave the same way as a conditionally defined constant (or variable?), but does not seem to be defined within the vba that Access shows me.
        The code behaves as expected presuming conditional compilation constans are accessible within conditional compilation statements only (which makes sense because they are used in compilation process only) and variables are allowed to be declared implicitely.

        If you want to see the difference you should run it in step-by-step mode.
        #If statement unlike If statement is not executed because it is not compiled to executable code.

        Comment

        • prn
          Recognized Expert Contributor
          • Apr 2007
          • 254

          #5
          Thanks again, FishVal!

          Originally posted by FishVal
          :) Seek, seek. It must be.
          ... Sorry for the old joke.
          OK. It is defined. Just not in the (searchable) VBA. :(

          In the Visual Basic window, there is a "Project" pane. (View -> Project Explorer) If you right-click on the topmost name in that pane (the name of the overall project) and select "Properties ..." you get a dialog box and on the "General" tab of that, there is a textbox called "Conditiona l Compilation Arguments" and in my case, that textbox contains: "OutlookEna bled = 1". This seems to be equivalent to:
          #Const OutlookEnabled = 1
          so everything is now clear except for why the original author did it this way. However, I don't expect to ever figure that out considering the overall condition of the code in thsi app. :(

          Originally posted by FishVal
          The code behaves as expected presuming conditional compilation constans are accessible within conditional compilation statements only (which makes sense because they are used in compilation process only) and variables are allowed to be declared implicitely.

          If you want to see the difference you should run it in step-by-step mode.
          #If statement unlike If statement is not executed because it is not compiled to executable code.
          I think I do (now, if not before) understand why conditional compilation (or execution) works as it does, but I admit that I didn't express myself at all well on the subject.

          Anyway, thanks, FishVal. You definitely helped my understanding.

          Paul

          Comment

          • FishVal
            Recognized Expert Specialist
            • Jun 2007
            • 2656

            #6
            Originally posted by prn
            ...
            so everything is now clear except for why the original author did it this way...
            Aha.

            Sorry, I didn't realized it from the very beginning since I've never used conditional compiling in VBA (didn't even knew about this option in VBA), but now I see a very good reason.

            When the application is being deployed on a machine without Outlook installed, #Outlook is set to false thus excluding from compilation code that uses Outlook object model which otherwise will generate compilation errors.

            Nice.

            Comment

            • prn
              Recognized Expert Contributor
              • Apr 2007
              • 254

              #7
              Originally posted by FishVal
              When the application is being deployed on a machine without Outlook installed, #Outlook is set to false thus excluding from compilation code that uses Outlook object model which otherwise will generate compilation errors.

              Nice.
              Do you mean that this can be done automatically? I can see how it can be set manually. Do you think it's more convenient to set it here (where the next guy to maintain the app can't find it in a search)?

              I'm still somewhat puzzled, but then I tend to prefer text that I can find in a search over somewhat obscure dialogs. That may just be me though.

              Thanks again, Fish.

              Paul

              Comment

              • FishVal
                Recognized Expert Specialist
                • Jun 2007
                • 2656

                #8
                What does mean "automatically" ?

                Comment

                • prn
                  Recognized Expert Contributor
                  • Apr 2007
                  • 254

                  #9
                  Originally posted by FishVal
                  What does mean "automatically" ?
                  "Without human intervention" is what I would mean by it. That is, it would be great if "[w]hen the application is being deployed on a machine without Outlook installed" the process simply detected that Outlook was not available. OTOH, if, as I suspect, Access cannot detect whether Outlook is available without a person changing that value, then my personal bias would be to have the #Const declaration somewhere searchable rather than in what seems to me an obscure dialog. But, that is my own bias and not necessarily shared by anyone else.

                  Best Regards,
                  Paul

                  Comment

                  • FishVal
                    Recognized Expert Specialist
                    • Jun 2007
                    • 2656

                    #10
                    Originally posted by prn
                    ... OTOH, if, as I suspect, Access cannot detect whether Outlook is available without a person changing that value, then my personal bias would be to have the #Const declaration somewhere searchable rather than in what seems to me an obscure dialog. But, that is my own bias and not necessarily shared by anyone else.
                    ...
                    Access can detect whether Outlook is installed.
                    • The easiest way is to detect whether Outllok type library reference is broken. If so code dealing with the library will not work, no matter ehether the reason is library incompatibility or no Outlook installed at all.
                    • Installed software could be detected via windows registry.


                    Once Outlook availability has been determined, VBA could add code line with conditional compiling constant declaration to module via Module class methods.

                    Regards,
                    Fish

                    Comment

                    • prn
                      Recognized Expert Contributor
                      • Apr 2007
                      • 254

                      #11
                      Interesting ideas. I'll look into them.

                      Thanks,
                      Paul

                      Comment

                      Working...