There is a
known way to eject CD on XP through Windows Media Player:
Sub EjectCD()
On Error Resume Next
Dim objWMP,objCDD
Set objWMP = CreateObject("WMPlayer.OCX")
Set objCDD = objWMP.cdromCollection
If objCDD.count > 0 Then
For i = 0 To objCDD.count - 1
objCDD.Item(i).Eject
Next
End If
Set objCDD = nothing
Set objWMP = nothing
End Sub
As well there is one more way that works more correctly (through FSO and WSH):
Const ssfDRVS = &H11
Sub EjectCD()
Dim objFSO,objWSH,objCDD,cd
cd = ""
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWSH = CreateObject("Shell.Application")
Set objCDD = objFSO.Drives
For Each obj In objCDD
If obj.DriveType = 4 Then
cd = obj& "\" : Exit For
End If
Next
Set objCDD = nothing
If cd <> "" Then
Set objCDD = objWSH.Namespace(ssfDRVS).ParseName(cd)
objCDD.InvokeVerb("E&ject")
Set objCDD = nothing
End If
Set objFSO = nothing
Set objWSH = nothing
Set cd = nothing
End Sub
But this code have two problems:
1. It can work only on a computers with English version of Windows.
2. The InvokeVerb method doesn't work on Vista in general!
I found this today when I tried to add the control options for CD/DVD into my new Media Player... However the problem is already solved! Please look below:
Const ssfDRVS = &H11
Sub EjectCD()
Dim objFSO,objWSH,objCDD,cd,n
cd = ""
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWSH = CreateObject("Shell.Application")
Set objCDD = objFSO.Drives
For Each obj In objCDD
If obj.DriveType = 4 Then
cd = obj& "\" : Exit For
End If
Next
Set objCDD = nothing
If cd <> "" Then
Set objCDD = objWSH.Namespace(ssfDRVS).ParseName(cd).Verbs
n = (objCDD.count - 5) '<== Because the "Eject" item is always the fifth from the end!
objCDD.Item(n).DoIt()
Set objCDD = nothing
End If
Set objFSO = nothing
Set objWSH = nothing
Set cd = nothing
Set n = nothing
End Sub
The last code works fine not only on XP but on Vista to.
That's all I wanted to tell at the moment...