MACRO LIBRARY

Lesson 1.6 homework part B solution

Free public access. No account or subscription required.

Files

Lesson 1.6 homework part B solution

Code

'Homework 1.6b
'Create a macro that extracts five pieces of information from a file location and displays _
    the results in a message box: _
    -SolidWorks document type _
    -File directory _
    -Part number _
    -Location _
    -Whether the file is chassis-related (if the project contains the word 'chassis'
    
'The file location always has the following form:
    
'<drive>:<department><project><part number>-rev<revision number>.<doc type>
    
'Do assume that the only one period and dash will exist in the file path.
'Do NOT assume that the file extension will always be 6 characters long.
'Do NOT assume that the file extension is always lowercase.
'Do NOT assume that the part number is always the same length.
    
'Test your macro on the following string: _
    "H:\Engineering\Chassis\01482-rev2.sldprt"
    
Const strFilePath = "H:\Engineering\Chassis\01482-rev2.sldprt"
    
Dim strType As String
Dim strFileDir As String
Dim strPartNo As String
Dim strDrive As String
Dim blnChassis As Boolean
    
Dim intPeriodLoc As Integer
Dim intDashLoc As Integer
Dim intLastSlashLoc As Integer
Dim intLength As Integer
    
Sub main()
    'get the document type
    intLength = Len(strFilePath)
    intPeriodLoc = InStr(1, strFilePath, ".")
    strType = UCase(Right(strFilePath, intLength - intPeriodLoc))
    Select Case strType
        Case "SLDPRT"
            strType = "part"
        Case "SLDASM"
            strType = "assembly"
        Case "SLDDRW"
            strType = "drawing"
        Case Else
            MsgBox "Unrecognized document type. Program will end."
            End
    End Select
    
    'get the drive letter
    strDrive = Left(strFilePath, 1)
    
    'get the file directory
    intLength = Len(strFilePath)
    intLastSlashLoc = InStrRev(strFilePath, "", -1)
    strFileDir = Left(strFilePath, intLastSlashLoc)
    
    'get the part number
    intDashLoc = InStr(strFilePath, "-")
    strPartNo = Mid(strFilePath, intLastSlashLoc + 1, intDashLoc - intLastSlashLoc - 1)
    
    'determine whether this is a chassis-related part
    blnChassis = False
    If InStr(1, strFilePath, "Chassis") <> 0 Then blnChassis = True
    
    'display results
    MsgBox "Document type: " & strType & vbCrLf & _
           "File directory: " & strFileDir & vbCrLf & _
           "Part number: " & strPartNo & vbCrLf & _
           "Location: " & strDrive & " drive" & vbCrLf & _
           "Chassis-related: " & blnChassis
End Sub