Do I need conditional compilation?

W

Walt

Hi,

I use the following in positioning a custom right-click menu (Win XP
XL2002 & 2003). Some users would like to use the application on Macs
and I don't know if this code would work.

'******************** CURSOR POSITION ***************
Private Type CursorPos
Lft As Long
Tp As Long
End Type

Private Declare Function GetCursorPos Lib "user32.dll" (lpPoint As
CursorPos) As Boolean

Function GetCursorTop()
Dim CurCursor As CursorPos
If GetCursorPos(CurCursor) Then GetCursorTop = CurCursor.Tp
End Function

Function GetCursorLeft()
Dim CurCursor As CursorPos
If GetCursorPos(CurCursor) Then GetCursorLeft = CurCursor.Lft
End Function

As I understand, there is a user32 library incliding GetCursorPos with
the Mac / Excel 2004.

******* Maybe something like the following would work?:

Private Type CursorPos
Lft As Long
Tp As Long
End Type

#If Mac Then
Private Declare Function GetCursorPos Lib "user32" (lpPoint As
CursorPos) As Long
Function GetCursorTop()
Dim CurCursor As CursorPos
GetCursorPos (CurCursor)
GetCursorTop = CurCursor.Tp
End Function
Function GetCursorLeft()
Dim CurCursor As CursorPos
GetCursorPos (CurCursor)
GetCursorTop = CurCursor.Lft
End Function
#Else
Private Declare Function GetCursorPos Lib "user32.dll" (lpPoint As
CursorPos) As Boolean
Function GetCursorTop()
Dim CurCursor As CursorPos
If GetCursorPos(CurCursor) Then GetCursorTop = CurCursor.Tp
End Function
Function GetCursorLeft()
Dim CurCursor As CursorPos
If GetCursorPos(CurCursor) Then GetCursorLeft = CurCursor.Lft
End Function
#End If


It seems to work on the PC side, but I can't test it now on the Mac.
Sub test()
MsgBox GetCursorLeft & " ---- " & GetCursorTop
End Sub

Any help with this will be appreciated.

Best Regards,
Walt Weber
 
W

Walt

Hi,

No response in a week & a half - am I in the wrong group?

Best Regards,
Walt Weber
 
C

CyberTaz

Hi Walt-

I'm not a coder, so did not reply to your first post, buy would like to
offer some thoughts on the more recent one.

First, keep in mind that this NG is just folks like you who volunteer their
time & expertise to help out when & where they can.

Second, your inquiry involves significantly more than the typical 'which
menu' question, & that these people try to offer as complete, _accurate_ &
_responsible_ a reply as they can.

That having been said, I would not be at all surprised if there are at least
two highly qualified individuals devoting whatever time they can to testing
your code & even rewriting and testing again before supplying you with a
reliable fix. They may even be trying on several different versions of Mac
OS X, as you don't mention which one Excel 2004 is running on.

Perhaps it is not socially acceptable that they haven't posted to inform you
of the fact that they are trying to help, but at least nobody told you to
get off your butt, take your code to a Mac & try it yourself rather than
asking someone else to do it for you.

Regards |:>)
 
W

Walt

Hi,
From the first post:
As I understand, there is a user32 library including GetCursorPos
with the Mac / Excel 2004.

I can't remember what I read that made me think that was so, but
it's NOT SO as I've since learned.

And, of course my attempt to run the code in the first post failed when
I was finally able to try it today on a Mac (Jaguar, Excel 2004).

Further search results indicate that reading the cursor position on a
Mac may not be possible from within VBA.

Best Regards,
Walt Weber
 
B

Bernard Rey

Walt:
From the first post:

No that kind of stuff is the typical Windows stuff.
I can't remember what I read that made me think that was so, but
it's NOT SO as I've since learned.
Right.

And, of course my attempt to run the code in the first post failed when
I was finally able to try it today on a Mac (Jaguar, Excel 2004).

Further search results indicate that reading the cursor position on a
Mac may not be possible from within VBA.

Well, there is a possibility, but not an "simple" one. You can get the mouse
position using an AppleScript (which can be run inside a VBA macro with the
"Macscript()" function). But to get the cursor position, you will need to
have the "Xtool" Scripting Addition (more or less comparable to a ".dll" on
the Windows side) installed in the "/System/Library/ScriptingAdditions"
folder. This can be downloaded from http://osaxen.com/files/xtool1.1.html

Once this is done, the line:
temp = MacScript("get mouse location")
will return the cursor position on the Mac, as well as
temp = MacScript("get mouse x")
will return the x-coordinate, etc.

So you can use some conditional compilation and adapt your macro in order to
run on Macs as well as Windows version, I guess. These lines work as
expected on the Mac side (provided the Scripting Addition is installed):

Function GetCursorTop()
Dim CurCursor As Long
GetCursorTop = MacScript("get mouse y")
End Function
Function GetCursorLeft()
Dim CurCursor As Long
GetCursorLeft = MacScript("get mouse x")
End Function
 
W

Walt

Hi Bernard,

To be sure I understand correctly, the Scripting Addition would have to
be installed on every machine using my piece -- not just the
development machine. True? Do most Macs have the Scripting Addition
already installed? Is it a free download and is it permissable to
distribute it freely to other users? Are there any downsides or
special considerations to installing the Scripting Addition?

You've raised my hopes Bernard. Thank you!

Best Regards,
Walt Weber
 
B

Bernard Rey

Walt said:
To be sure I understand correctly, the Scripting Addition would have to
be installed on every machine using my piece -- not just the
development machine. True?
True


Do most Macs have the Scripting Addition already installed?

No. You'll have to advise them (and this is why I said it wasn't a "simple"
solution, the rest being easy)

Is it a free download and is it permissable to distribute it
freely to other users?

It is a free download (sorry, I could have mentioned that). As to the free
distribution, I guess you could ask the developer:
http://www.lestang.org/

Are there any downsides or
special considerations to installing the Scripting Addition?

I don't think so. A problem may rise if the user runs Mac OS 9 as this
addition runs with Mac OS X. There are additions for Mac OS 9 too, but they
will be different (and the syntax may differ). If you plan to give the macro
to pre-OS X users, let me know, I'll have a look.
 
W

Walt

Hi Bernard,

You've handed me a chunk of critical information. Thank you.

My plan is to post back to this thread with a functioning set of
conditional compilation code and instructions regarding the "Xtool"
Scripting Addition after I'm able to test it on the different
platforms. Unfortunately, it might be a little while before I can get
the needed access to a Mac for testing (Last weekend I was at a Mac
store in Seattle near the U. of Washington and the people there were
quite open to my plunking around on their machines).

As an aside, is it possible to embed Scripting Code within VBA to avoid
having to go to a reference external to the project and thereby keep
installations simpler?

Best Regards,
Walt Weber
 
B

Bernard Rey

Walt said:
My plan is to post back to this thread with a functioning set of
conditional compilation code and instructions regarding the "Xtool"
Scripting Addition after I'm able to test it on the different
platforms. Unfortunately, it might be a little while before I can get
the needed access to a Mac for testing (Last weekend I was at a Mac
store in Seattle near the U. of Washington and the people there were
quite open to my plunking around on their machines).

In case it can bring you some help: if I get back to your original post, the
following lines are running as expected on both Windows and Mac versions of
Excel.

----------------------------------------------------
#If Mac Then

Function GetCursorTop()
Dim CurCursor As Long
GetCursorTop = MacScript("get mouse y")
End Function

Function GetCursorLeft()
Dim CurCursor As Long
GetCursorLeft = MacScript("get mouse x")
End Function

#Else

Private Type CursorPos
Lft As Long
Tp As Long
End Type

Private Declare Function GetCursorPos Lib "user32.dll" _
(lpPoint As CursorPos) As Boolean

Function GetCursorTop()
Dim CurCursor As CursorPos
If GetCursorPos(CurCursor) Then GetCursorTop = CurCursor.Tp
End Function

Function GetCursorLeft()
Dim CurCursor As CursorPos
If GetCursorPos(CurCursor) Then GetCursorLeft = CurCursor.Lft
End Function

#End If

----------------------------------------------------

As an aside, is it possible to embed Scripting Code within VBA to avoid
having to go to a reference external to the project and thereby keep
installations simpler?

Not that I know (it's ways beyond my skills!)
 
W

Walt

Hi Bernard,

Well that certainly brings it all together in a definitive way.

Bernard, thank you.

Best Regards,
Walt Weber
 
C

CyberTaz

Hi Walt-

No intention of saying "I told you so", but just to confirm that these folks
are here to help. Glad Bernard was able to fill in the gaps!

Regards |:>)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top