Search This Blog

Sunday, February 20, 2005

What Drive is the Boot Drive? VBA Function

Chances are, during your programming work, you will need to know what the boot drive is on the computer your routine is currently running on. An old trick from the DOS days is to check the value of the COMSPEC environment variable. You can use the function below to return the path to the system files folder location containing the Shell or Command Interpreter. This same folder is the one typically used by Windows to install ActiveX libraries and controls. You can use this function to programmatically register a control or DLL.

And the best news of all is that this is really easy to do within VBA. Getting the value of any environment variable can be done using the built in .Environ$ function. Try the following:

Function ReturnOSPath() As String
'This function assumes that the "Comspec"
'variable File path is located on the boot drive
Dim strKRNL As String
strKRNL = VBA.Environ$("ComSpec")
On Error GoTo Err_Handler
ReturnOSPath = Mid$(strKRNL, 1, (VBA.Len(strKRNL) - 7))
Exit Function
Err_Handler:
ReturnOSPath = vbNull
End Function

Public Sub testospath()
MsgBox ReturnOSPath
End Sub


To use these, fire up the VBA editor within AutoCAD or any other VBA enabled application. Cut and paste the above code into the editor. Now call the "testospath" procedure to see how it works.