MACRO LIBRARY

Lesson 1.10 start code

Free public access. No account or subscription required.

Files

Lesson 1.10 start code

Code

Module: mMain

Dim strFolder As String
Dim strFile As String
Dim oFSO As FileSystemObject
    
Sub main()
    Set oFSO = New FileSystemObject
    
'1. Browse for folder
    strFolder = BrowseFolder("") & "\"
    Debug.Print strFolder
    
'2. Cycle through folder and get all SolidWorks parts
    strFile = Dir(strFolder & "*.sldprt", vbNormal)
    Do While strFile <> ""
        Debug.Print strFile
        
        '3. If part is called Part2.SLDPRT then delete it
        If InStr(1, strFile, "Part2.SLDPRT") <> 0 Then oFSO.DeleteFile strFolder & strFile
        
        '4. If part is called Part3.SLDPRT then move to archive folder
        If InStr(1, strFile, "Part3.SLDPRT") <> 0 Then
            oFSO.MoveFile strFolder & strFile, Replace(strFolder, "Current", "Archive") & strFile
        End If
        strFile = Dir
    Loop
End Sub

Module: mBrowseFolder

#If VBA7 Then
    
Private Type BROWSEINFO
hOwner As LongPtr
pidlRoot As LongPtr
pszDisplayName As String
lpszTitle As String
ulFlags As LongPtr
lpfn As LongPtr
lParam As LongPtr
iImage As LongPtr
End Type
    
Private Declare PtrSafe Function SHGetPathFromIDList Lib "shell32.dll" Alias _
            "SHGetPathFromIDListA" (ByVal pidl As LongPtr, _
            ByVal pszPath As String) As LongPtr
            
Private Declare PtrSafe Function SHBrowseForFolder Lib "shell32.dll" Alias _
            "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
            As LongPtr
            
Dim X As LongPtr, bi As BROWSEINFO, dwIList As LongPtr
    
#Else
    
Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
    
Private Declare  Function SHGetPathFromIDList Lib "shell32.dll" Alias _
            "SHGetPathFromIDListA" (ByVal pidl As Long, _
            ByVal pszPath As String) As Long
            
Private Declare  Function SHBrowseForFolder Lib "shell32.dll" Alias _
            "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
            As Long
            
Dim X As Long, bi As BROWSEINFO, dwIList As Long
    
#End If
    
Private Const BIF_RETURNONLYFSDIRS = &H1
    
Public Function BrowseFolder(szDialogTitle As String) As String
    
Dim szPath As String, wPos As Integer
    
    With bi
        .hOwner = hWndAccessApp
        .lpszTitle = szDialogTitle
        .ulFlags = BIF_RETURNONLYFSDIRS
    End With
    
    dwIList = SHBrowseForFolder(bi)
    szPath = Space$(512)
    X = SHGetPathFromIDList(ByVal dwIList, ByVal szPath)
    
    If X Then
        wPos = InStr(szPath, Chr(0))
        BrowseFolder = Left$(szPath, wPos - 1)
    Else
        BrowseFolder = vbNullString
    End If
End Function