Hyperlink to the CD drive

sabraham

New Member
Joined
Nov 19, 2009
Messages
16
I use the Hyperlink to open PDF files that are on the CD drive from my spreadsheet. I am on Windows XP and the CD drive is the D drive. In Vista the CD drive is the E drive. When I take the spreadsheet to a Vista machine the link fails as there is no D drive.

Is there any to code the path of the Hyperlink so it will go to th default CD drive for that computer.

Thanks
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You will also have that problem with machines with multiple hard drives.

Assuming you only have a single CD OR DVD-ROM drive, see here:

Slightly modified from http://support.microsoft.com/kb/291575:

All this should go into a standard module:

Code:
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" _
    (ByVal nDrive As String) As Long

Private Declare Function GetLogicalDriveStrings Lib "kernel32" _
    Alias "GetLogicalDriveStringsA" _
    (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Const DRIVE_CDROM& = 5

Public Function GetDriveStrings() As String
    ' Wrapper for calling the GetLogicalDriveStrings api
    
    Dim result As Long          ' Result of our API calls
    Dim strDrives As String     ' String to pass to API call
    Dim lenStrDrives As Long    ' Length of the above string
    
    ' Call GetLogicalDriveStrings with a buffer size of zero to
    ' find out how large our stringbuffer needs to be
    result = GetLogicalDriveStrings(0, strDrives)
    
    strDrives = String(result, 0)
    lenStrDrives = result
    
    ' Call again with our new buffer
    result = GetLogicalDriveStrings(lenStrDrives, strDrives)
    
    If result = 0 Then
        ' There was some error calling the API
        ' Pass back an empty string
        ' NOTE - TODO: Implement proper error handling here
        GetDriveStrings = ""
    Else
        GetDriveStrings = strDrives
    End If
End Function

Function CDLetter()
    Dim strDrives As String
    
    ' Find out what drives we have on this machine
    strDrives = GetDriveStrings()
    
    If strDrives = "" Then
        ' No drives were found
        CDLetter = "No CD Drives were found!"
    Else
        ' Walk through the string and check the type of each drive
        ' displaying any cd-rom drives we find
        Dim pos As Long
        Dim drive As String
        Dim drivetype As Long
        
        pos = 1
        
        Do While Not Mid$(strDrives, pos, 1) = Chr(0)
            drive = Mid$(strDrives, pos, 3)
            pos = pos + 4
            drivetype = GetDriveType(drive)
            If drivetype = DRIVE_CDROM Then
                CDLetter = UCase(drive)
            End If
        Loop
    End If
End Function
If a CD or DVD drive is present, the CDLetter function will return the drive letter in the format D:\
 
Last edited:
Upvote 0
Thanks for the information but I am unsure how to use it. The format in the Hyperlink box is D:\PDF NAME.pdf. How do I use this code to link to drives other than the D drive?
 
Upvote 0
If by the hyperlink box, you mean CTRL-K, I'm not sure you are going to be able to do it that way.

In the cell, where you want the hyperlink, type the following formula, change filename as necessary:

=HYPERLINK(CDLetter()&"PDF LETTER.PDF")
 
Last edited:
Upvote 0
Thanks again! Do I still need to add the code that you gave me and if so, how and where do I add it?
 
Upvote 0
Yes, you still need the code.

Copy the code I posted.
Right click on any sheet tab in your workbook and click on View Code
This will take you into the Visual Basic Editor (VBE)
From the menu on top, click Insert, then Module
Paste into white area
Hit Alt-q
When you save the workbook, this code will be saved with it.
 
Upvote 0
Sorry to be a pest but I am still having problems. I inserted the code that you sent and am using the following format:

=HYPERLINK(CDLetter()&"47-417.PDF")

I am trying to link to and open a file named 47-471.pdf that is on the CD drive.

When I put the cursor over the cell, the hand comes up. When I click it nothing happens.

Any suggestions?

Thanks
 
Upvote 0
It works perfectly fine in my tests.
Does it show the correct drive in the hyperlink?
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top