Open & Close CD Tray

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Open & Close CD Tray

Post by adam »

Hi Anyone,

The following code when placed in a standard module creates two separate macros to open and close a CD tray.

Any help would be kindly appreciated if this code could be changed so that one macro button does both the tasks.

Meaning, if the macro button is clicked the CD tray closes if its open and opens if it is closed.

Thanks in advance.

Code: Select all

Declare Sub mciSendStringA Lib "winmm.dll" (ByVal lpstrCommand As String, _
ByVal lpstrReturnString As Any, ByVal uReturnLength As Long, _
ByVal hWndCallback As Long)
Sub OpenCDTray()
    mciSendStringA "Set CDAudio Door Open", 0&, 0, 0
End Sub
Sub CloseCDTray()
    mciSendStringA "Set CDAudio Door Closed", 0&, 0, 0
End Sub
The code opens & close the tray within seconds without allowing the user to put in CD if the code is modified as follows.

Code: Select all

Declare Sub mciSendStringA Lib "winmm.dll" (ByVal lpstrCommand As String, _
ByVal lpstrReturnString As Any, ByVal uReturnLength As Long, _
ByVal hWndCallback As Long)
Sub OpenAndCloseCDTray()
    mciSendStringA "Set CDAudio Door Open", 0&, 0, 0
    mciSendStringA "Set CDAudio Door Closed", 0&, 0, 0
End Sub
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78705
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Open & Close CD Tray

Post by HansV »

Try

Code: Select all

Declare Sub mciSendStringA Lib "winmm.dll" (ByVal lpstrCommand As String, _
  ByVal lpstrReturnString As Any, ByVal uReturnLength As Long, _
  ByVal hWndCallback As Long)

Sub OpenAndCloseCDTray()
  Static blnState As Boolean
  If blnState Then
    mciSendStringA "Set CDAudio Door Open", 0&, 0, 0
  Else
    mciSendStringA "Set CDAudio Door Closed", 0&, 0, 0
  End If
  blnState = Not blnState
End Sub
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Open & Close CD Tray

Post by adam »

Thanks Hans. It worked like a dream come true.
Best Regards,
Adam

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: Open & Close CD Tray

Post by VegasNath »

No offence intended, but what a bizarre vba requirement. :scratch:

Opening works for me, but not closing, I guess that is to do with my laptop.
:wales: Nathan :uk:
There's no place like home.....

User avatar
HansV
Administrator
Posts: 78705
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Open & Close CD Tray

Post by HansV »

Many laptops have a CD drive that can automatically open to eject a CD, but that have to be closed manually.
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15677
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Open & Close CD Tray

Post by ChrisGreaves »

HansV wrote:Many laptops have a CD drive that can automatically open to eject a CD, but that have to be closed manually.
I agree with Adam. Neat bit of code.
Now ...
Picture (Small).jpg
...if I mount small casters on my laptop stand, can you please dash off a few lines to simulate the old assembly-language instruction SLR (Shift Right Laptop )?
That would allow us to automate the process of closing the drawer.
Thanks in advancing, sideways.
You do not have the required permissions to view the files attached to this post.
By definition, educating the client is the consultant’s first objective

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Open & Close CD Tray

Post by adam »

Opening works for me, but not closing, I guess that is to do with my laptop.
The code works fine with desktops.
Best Regards,
Adam