Consulting

Results 1 to 18 of 18

Thread: Conditional Compilation Quick Question (Well, at least hopefully)

  1. #1
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location

    Conditional Compilation Quick Question (Well, at least hopefully)

    Greetings All,

    From the "This ain't in the help files, least as I can find" category, here's a question for those of you who actually have skills with this stuff (vs. those trying to not start the PC on fire, like yours truly here).

    I am muddling through writing some code that needs to work in at least VB7 with both 32|64 bit, and preferably VB6 and VB7 (and still covering both 32|64). So... I just noticed that if I write a function two ways (#If VB7 #Else...), and I am in VB7 (Excel 2010), then I cannot put a breakpoint in the #Else section.

    Am I taking it correctly (okay, I mean guessing) that even in design time, VBA simply prevents looking at any of the declarations/functions/etc that will not be in play under the current conditions? That is to ask: Let us say I was even more goofy than I think I am, and thus chose to not use Option Explicit. To properly debug the code, would I be correct that the code needs run in each environment?

    Hope that makes sense, some of us are a wee bit sleep deprived at the moment...


    Thank you so much,

    Mark

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Hi, Mark,

    What would be the point in setting a breakpoint in code that cannot possibly run or be compiled?
    Be as you wish to seem

  3. #3
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by Aflatoon View Post
    What would be the point in setting a breakpoint in code that cannot possibly run or be compiled?
    If Question = FriendlyJab then
       Answer = "Ouch! Ouch! Ouch! Okay, I suppose I deserved that one."
    ElseIf Question = ProvokeThinkingOnMyEnd
      Answer = LotsOfLuckWithThat
    Else
      Answer = SeeBelow
    Hi Aflatoon,
    Well yeh, I wasn't really thinking it all the way through. The minimal brain waves available came up with "Huh? I wonder if I misspell a variable, whether even that would be caught?". Thinking it through, I imagine not, as it either compiles or does not...

    Okay, so myabe you provoked a little bit of thought :-)

    well, I am going to "cheat" a bit and ask an "extra" question. Disregarding any missing declared procedures or missing written procedures, does this look correct? Soecifically, I copied just enough of what I had written tonight at work, to cover how I am providing conditional:

    Declared procedures
    Variable declaration

    Written procedures

    #If VBA7 Then
      Public Declare PtrSafe Function GetWindowText Lib "user32" _
                               Alias "GetWindowTextA" (ByVal hwnd As LongPtr, _
                                                       ByVal lpString As String, _
                                                       ByVal cch As Long _
                                                       ) As Long
    #Else
      Public Declare Function GetWindowText Lib "user32" _
                       Alias "GetWindowTextA" (ByVal hwnd As Long, _
                                               ByVal lpString As String, _
                                               ByVal cch As Long _
                                               ) As Long
    #End If
      
    Sub test()
    Dim lRet          As Long
    #If VBA7 Then
      Dim hwndDesktop As LongPtr
      Dim lParam      As LongPtr
    #Else
      Dim hwndDesktop As Long
      Dim lParam      As Long
    #End If
      
      hwndDesktop = GetDesktopWindow()
      
      lRet = EnumChildWindows(hwndDesktop, AddressOf EnumChildProc, lParam)
      
      MsgBox ETermRunning("E-Term32.exe")
      
    End Sub
      
    #If VBA7 Then
    Private Function EnumChildProc(ByVal lpHwnd As LongPtr, ByVal lpParam As LongPtr) As Long
    Dim lRet          As Long
    Dim sWinClassBuff As String * 255
    Dim sWinTitleBuff As String * 255
    Dim sWinClass     As String
    Dim sWinTitle     As String
      
      lRet = GetClassName(lpHwnd, sWinClassBuff, 255)
      sWinClass = StripNulls(sWinClassBuff)
      lRet = GetWindowText(lpHwnd, sWinTitleBuff, 255)
      sWinTitle = StripNulls(sWinTitleBuff)
      
      Debug.Print "   Child Class = "; sWinClass; ", Title = "; sWinTitle
      EnumChildProc = True
      
    End Function
    #Else
    Function EnumChildProc(ByVal lhWnd As Long, ByVal lParam As Long) As Long
    Dim lRet          As Long
    Dim sWinClassBuff As String * 255
    Dim sWinTitleBuff As String * 255
    Dim sWinClass     As String
    Dim sWinTitle     As String
      
      lRet = GetClassName(lhWnd, WinClassBuf, 255)
      sWinClass = StripNulls(sWinClassBuff)
      lRet = GetWindowText(lhWnd, sWinTitleBuff, 255)
      sWinTitle = StripNulls(sWinTitleBuff)
      Debug.Print "   Child Class = "; WinClass; ", Title = "; WinTitle
      EnumChildProc = True
      
    End Function
    #End If
    In close, I am unfortunately, quite the 'babe in the woods' reference API. In this particular case, I did manage to get Excel (though I used Word's DDE) to extract data from a terminal emulator. When due to a new goal, I ran some the old code, it pretty much blew up. I'm just starting to sort the causes, but appears to be related to the OS we previously used (XP 32-bit) and what is slowly replacing (WIN7 64-bit). (And frankly, I have no idea currently as to why we are loading 64-bit currently)

    Of course, thank you so much for your guidance and help,

    Mark

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    It looks OK to me. (I assume you know you only need this if you will be running a 64 bit version of Office? 64bit windows on its own does not require these changes)

    You can simplify the last part and only make the declaration conditional, since all the rest is the same:
    [vba]#If VBA7 Then
    Private Function EnumChildProc(ByVal lpHwnd As LongPtr, ByVal lpParam As LongPtr) As Long
    #Else
    Function EnumChildProc(ByVal lhWnd As Long, ByVal lParam As Long) As Long
    #End If
    Dim lRet As Long
    Dim sWinClassBuff As String * 255
    Dim sWinTitleBuff As String * 255
    Dim sWinClass As String
    Dim sWinTitle As String

    lRet = GetClassName(lpHwnd, sWinClassBuff, 255)
    sWinClass = StripNulls(sWinClassBuff)
    lRet = GetWindowText(lpHwnd, sWinTitleBuff, 255)
    sWinTitle = StripNulls(sWinTitleBuff)

    Debug.Print " Child Class = "; sWinClass; ", Title = "; sWinTitle
    EnumChildProc = True

    End Function

    [/vba]
    Be as you wish to seem

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Module Level Constant:
    #Const Design32 = False|True
    Conditional Code:
    #If Design32 Then
        Function EnumChildProc(ByVal lhWnd As Long, ByVal lParam As Long) As Long
    #ElseIf VBA7 Then
        Private Function EnumChildProc(ByVal lpHwnd As LongPtr, ByVal lpParam As LongPtr) As Long
    #Else
        Function EnumChildProc(ByVal lhWnd As Long, ByVal lParam As Long) As Long
    #End If
    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

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    @Aflatoon:

    Quote Originally Posted by Aflatoon View Post
    It looks OK to me. (I assume you know you only need this if you will be running a 64 bit version of Office? 64bit windows on its own does not require these changes)

    You can simplify the last part and only make the declaration conditional, since all the rest is the same:...
    Hmmm... Well, embarrassing to admit, but to be forthright - nope, I did not realize this.

    I am no doubt too oft guilty of reading only what seems necessary in order to put it to use, but of course this can lead to too much left to chance. I am also guilty of reading (think skimming) too quickly sometimes. After reading your comment (and thank you much; both for the information and the time you just saved me), I re-checked what I had read (er... glossed over). Being a family-friendly site, I cannot relay the words that were falling out of my mouth, but needless to say, when I re-checked Jan Karel Pieterse's article (http://www.jkp-ads.com/Articles/apideclarations.asp) and spotted, in large font, in bold, in all UPPERCASE, this teeny-tiny hint:

    Declaring API functions in 64 bit Office

    ...well now, don't I feel silly. Especially given that farther down in Jan Karel's article is another tiny hint, to wit: "If your code needs to run on both 32 bit and 64 bit Excel..."

    I realize that at this point, you would just have to take a leap of faith to believe I am not usually quite so "thick"... Anyways, certainly not an excuse, but I think part of my confusion has been in seeing that user32 is still referred to in either case. Finally I tried coming out of my coma and found out (http://en.wikipedia.org/wiki/Windows_User) that it's not really the same file (which IMO is just plain goofy)!

    Thanks again. (BTW, I re-ran what was KABOOMing last night, and it may be timing related, but not yet sure.)

    @SamT:

    Quote Originally Posted by SamT View Post
    Module Level Constant:
    #Const Design32 = False|True
    Conditional Code:
    #If Design32 Then
    I am going to need counseling if this keeps up. I've seen examples of testing, but by heaven's not a thought in my head this week I guess. Thank you so much Sam.

    Cheers to you both,

    Mark

  7. #7
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Mark,
    Quote Originally Posted by GTO View Post
    I realize that at this point, you would just have to take a leap of faith to believe I am not usually quite so "thick"...
    I've seen enough of your posts in various forums to recognise this as an aberration.
    Be as you wish to seem

  8. #8
    Quote Originally Posted by SamT View Post
    Module Level Constant:
    #Const Design32 = False|True
    Great, learned something new today.
    I thought compile constants could only be added by adding them to the bottom box of the VBAProject properties window.
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That's the worst place Jan Karel, you have to set them to 1 or 0 there which I have always struggled with

    The beauty of declaring them is they can be module specific, I have frequently used this.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    @Aflatoon:

    I forgot to add: Could we all swear secrecy and forget this thread?

    Seriously, thanks. In closing to "the thread I wish I could delete" (just kidding), if you or Sam or Jan Karel (or for that matter, where's Bob?) were to suggest a book reference API and/or (in laymen's terms) "This is what's going on under-the-hood" (or possibly bonnet?), what would it be?

    If nothing strikes to suggest, no problem; just thought to ask. The one-and-only book I have purchased so far is an EBay deal, 'Win32 API Programming with Visual Basic', publisher: O'Reilly, author: Steven Roman, circa: 2000. Whether it is/was one of the better ones or not, I'd probably get a lot more out of the purchase if I actually read/studied it (referenced aforementioned lacking).

    Thank you again brother.

    @Jan Karel Pieterse:

    While I obviously would better benefit if I slowed down a bit and actually studied it, I wanted to thank you for the efforts you expend in sharing knowledge. I don't think we've exchanged directly but a time or two, but it occasionally strikes me as to the efforts good folks put in, in sharing a "thing" they are fond of. Thank you so very much.

    Mark Wayne Stump

  11. #11
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    ACK. Reference #9, I obviously stepped away too many times in typing #10. No fair being Ninja-like!

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by GTO View Post
    If nothing strikes to suggest, no problem; just thought to ask. The one-and-only book I have purchased so far is an EBay deal, 'Win32 API Programming with Visual Basic', publisher: O'Reilly, author: Steven Roman, circa: 2000. Whether it is/was one of the better ones or not, I'd probably get a lot more out of the purchase if I actually read/studied it (referenced aforementioned lacking).
    The only API book I have is 'Visual Basic Programmer's Guide to the Win32 API' by Dan Appleman, which is a brick, and if I am honest, very boring. I don't know the Steve Roman book, but he is a good author, so I would assume it is alright, and I bet it is a far better read than mine . But I thought this thread was not an API thread, more one on programming practices and techniques. I remember bringing up conditional compilation in a thread in MrExcel some years ago, it was called 'tricks of gurus' in the Lounge. The CC chat had a little mileage, but quickly degenerated into a discussion about alcohol between Richard Schollar and myself

    Quote Originally Posted by GTO View Post
    While I obviously would better benefit if I slowed down a bit and actually studied it, I wanted to thank you for the efforts you expend in sharing knowledge. I don't think we've exchanged directly but a time or two, but it occasionally strikes me as to the efforts good folks put in, in sharing a "thing" they are fond of. Thank you so very much.
    There are many ways to absorb knowledge. I would have said that discussing within a specialised forum is one of the best. Don't beat yourself up for being smart and raising the topic.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  13. #13
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi Bob,

    A better reply tomorrow (it's 0503 Hrs, I should have hit the rack NLT 0330 - I am beat!), but I found it! http://www.mrexcel.com/forum/lounge-...cks-gurus.html

    Good info, which I will re-read, as tired/giddy, I started laughing too much. Not a bad way to end the day though :-)

    Til "tomorrow",

    Mark

  14. #14
    GTO: You're welcome.
    In the end all my posts and publications are marketing (which is just an excuse to hide the fact that I just love to share stuff and help people master stuff in Excel).

    I like the API viewer, it has many API functions and as a bonus contains lost of (VB6) examples which translate into VBA relatively easy.

    And then there is the one great big programming resource: Google.

    Steve bullen has a very nice example file on his website to have fun with Userforms using API's. www.oaltd.co.uk
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  15. #15
    xld: but of you declare them in a module, they're local to the module, so if you need a "global" compile constant, the project properties is the only way, right?
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  16. #16
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    @Mark,

    I have the same book as Bob - though I find it quite interesting. It's probably not something you'd want to sit down and read in one sitting, but it's about as good a coverage of the subject as I've seen, for those of us that don't speak C. I'll check out the one you mention though - always nice to have something new to read.

    @Bob,

    In my experience, Mr Schollar isn't much cop at drinking, so I wouldn't really listen to anything he has to say on the subject.
    Be as you wish to seem

  17. #17
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Aflatoon View Post
    @Bob,

    In my experience, Mr Schollar isn't much cop at drinking, so I wouldn't really listen to anything he has to say on the subject.
    As I said, real drinkers consider me a woos when it comes to drinking. And from what I hear, none of us are much cop compared to yourself .
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  18. #18
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Oh, I don't know about that. Andy can certainly more than hold his own on that front!
    Be as you wish to seem

Posting Permissions

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