Can VBA issue a command to eject a CD?

D

Don Wiss

I have a VBA program that reads a file created from a DVD/CD. I then by
hand eject the disc. Can VBA issue a command to eject the disc?

Don <www.donwiss.com> (e-mail link at home page bottom).
 
R

RB Smissaert

Try this:

Option Explicit
Private Declare Function mciSendString _
Lib "winmm.dll" _
Alias "mciSendStringA" (ByVal lpstrCommand As
String, _
ByVal
lpstrReturnString As String, _
ByVal uReturnLength As
Long, _
ByVal hwndCallback As
Long) As Long

Sub OpenCDDrive()

mciSendString "Set CDAudio Door Open", 0&, 0, 0

End Sub


Sub CloseCDDrive()

mciSendString "Set CDAudio Door Closed", 0&, 0, 0

End Sub


RBS
 
D

Don Wiss

Where are you setting which drive to open/close?

Try this:

Option Explicit
Private Declare Function mciSendString _
Lib "winmm.dll" _
Alias "mciSendStringA" (ByVal lpstrCommand As
String, _
ByVal
lpstrReturnString As String, _
ByVal uReturnLength As
Long, _
ByVal hwndCallback As
Long) As Long

Sub OpenCDDrive()

mciSendString "Set CDAudio Door Open", 0&, 0, 0

End Sub


Sub CloseCDDrive()

mciSendString "Set CDAudio Door Closed", 0&, 0, 0

End Sub


RBS
 
R

RB Smissaert

This does the default audio drive.
You can specify the drive letter like this:

Option Explicit
Private Declare Function mciSendString _
Lib "winmm.dll" _
Alias "mciSendStringA" (ByVal lpstrCommand As
String, _
ByVal
lpstrReturnString As String, _
ByVal uReturnLength As
Long, _
ByVal hwndCallback As
Long) As Long

Sub OpenCDDrive(Optional strDriveLetter As String)

If Len(strDriveLetter) = 0 Then
mciSendString "Set CDAudio Door Open", 0&, 0, 0
Else
mciSendString "open " & strDriveLetter & _
": type CDAudio alias drive" & strDriveLetter, 0&, 0, 0
mciSendString "set drive" & strDriveLetter & " door open", 0&, 0, 0
End If

End Sub

Sub CloseCDDrive(Optional strDriveLetter As String)

If Len(strDriveLetter) = 0 Then
mciSendString "Set CDAudio Door Closed", 0&, 0, 0
Else
mciSendString "open " & strDriveLetter & _
": type CDAudio alias drive" & strDriveLetter, 0&, 0, 0
mciSendString "set drive" & strDriveLetter & " door closed", 0&, 0, 0
End If

End Sub


Sub test()

OpenCDDrive "D"

End Sub


RBS
 
D

Don Wiss

This does the default audio drive.
You can specify the drive letter like this:

Thanks. It works like a charm. I'm not sure what is considered the default.
One reads CDs/DVDs and is called "J". The other read/writes CDs/DVDs and is
called "D". I would consider the one that reads only the default.

As I've added hard drives the letters have become scrambled and I haven't
bothered to change them.

Don <www.donwiss.com> (e-mail link at home page bottom).
 
R

RB Smissaert

You probably won't need it, but there are lots of other
commands you can pass to this API.

RBS
 
Joined
Jul 4, 2021
Messages
4
Reaction score
1
This does the default audio drive.
You can specify the drive letter like this:

Option Explicit
Private Declare Function mciSendString _
Lib "winmm.dll" _
Alias "mciSendStringA" (ByVal lpstrCommand As
String, _
ByVal
lpstrReturnString As String, _
ByVal uReturnLength As
Long, _
ByVal hwndCallback As
Long) As Long

Sub OpenCDDrive(Optional strDriveLetter As String)

If Len(strDriveLetter) = 0 Then
mciSendString "Set CDAudio Door Open", 0&, 0, 0
Else
mciSendString "open " & strDriveLetter & _
": type CDAudio alias drive" & strDriveLetter, 0&, 0, 0
mciSendString "set drive" & strDriveLetter & " door open", 0&, 0, 0
End If

End Sub

Sub CloseCDDrive(Optional strDriveLetter As String)

If Len(strDriveLetter) = 0 Then
mciSendString "Set CDAudio Door Closed", 0&, 0, 0
Else
mciSendString "open " & strDriveLetter & _
": type CDAudio alias drive" & strDriveLetter, 0&, 0, 0
mciSendString "set drive" & strDriveLetter & " door closed", 0&, 0, 0
End If

End Sub


Sub test()

OpenCDDrive "D"

End Sub


RBS
This does the default audio drive.
You can specify the drive letter like this:

Option Explicit
Private Declare Function mciSendString _
Lib "winmm.dll" _
Alias "mciSendStringA" (ByVal lpstrCommand As
String, _
ByVal
lpstrReturnString As String, _
ByVal uReturnLength As
Long, _
ByVal hwndCallback As
Long) As Long

Sub OpenCDDrive(Optional strDriveLetter As String)

If Len(strDriveLetter) = 0 Then
mciSendString "Set CDAudio Door Open", 0&, 0, 0
Else
mciSendString "open " & strDriveLetter & _
": type CDAudio alias drive" & strDriveLetter, 0&, 0, 0
mciSendString "set drive" & strDriveLetter & " door open", 0&, 0, 0
End If

End Sub

Sub CloseCDDrive(Optional strDriveLetter As String)

If Len(strDriveLetter) = 0 Then
mciSendString "Set CDAudio Door Closed", 0&, 0, 0
Else
mciSendString "open " & strDriveLetter & _
": type CDAudio alias drive" & strDriveLetter, 0&, 0, 0
mciSendString "set drive" & strDriveLetter & " door closed", 0&, 0, 0
End If

End Sub


Sub test()

OpenCDDrive "D"

End Sub


RBS
 
Joined
Jul 4, 2021
Messages
4
Reaction score
1
Hi,

I am new to Excel VBA. I want some code to actually read the full contents of a CD (Compact Disc). I want to read the artist, title, track artist, track list, full time etc. If anyone can help with this then please let me know.

Thanks,

Kev
 

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