' RECURSIVE FILE PROCESSING TEMPLATE
' This is a template macro for recursively processing all documents in a directory.
' You can use it for just about any operation for multiple documents; e.g.,
' saving documents as JPEG files, updating dimensions in
' documents, printing documents, etc.
Option Explicit
Sub main()
Dim swApp As SldWorks.SldWorks
Set swApp = Application.SldWorks
Dim fso As New FileSystemObject
Dim sDirectory As String
Dim sFilter As String
sDirectory = "W:\am\samples\tutorial"
sFilter = "*.*"
Dim nDirs As Long
Dim nFiles As Long
Call IterateFiles(sDirectory, sFilter, nDirs, nFiles, fso, swApp)
End Sub
Private Sub IterateFiles(ByVal sFol As String, sFile As String, _
nDirs As Long, nFiles As Long, fso As FileSystemObject, swApp As SldWorks.SldWorks)
Dim tFld As Folder, tFil As File, filename As String
Dim fld As Folder
On Error GoTo Catch
Set fld = fso.GetFolder(sFol)
filename = Dir(fso.BuildPath(fld.path, sFile), vbNormal Or _
vbHidden Or vbSystem Or vbReadOnly)
While Len(filename) <> 0
Dim fType As SwConst.swDocumentTypes_e
Dim fsType As String
fsType = LCase(Right$(filename, 6))
fType = -1
If fsType = "sldprt" Then
fType = swDocPART
End If
If fsType = "sldasm" Then
fType = swDocASSEMBLY
End If
If fsType = "slddrw" Then
fType = swDocDRAWING
End If
If fType = -1 Then ' Edit this to process files other than .sldprt, .sldasm, or .slddrw
Debug.Print "Skipping: " & fld.path & "\" & filename
GoTo endLoop
End If
Call ProcessSingleFile(fld.path, filename, fType, swApp, fso)
nFiles = nFiles + 1
endLoop:
filename = Dir() ' Get next file
DoEvents
Wend
nDirs = nDirs + 1
If fld.SubFolders.Count > 0 Then
For Each tFld In fld.SubFolders
DoEvents
Call IterateFiles(tFld.path, sFile, nDirs, nFiles, fso, swApp)
Next
End If
Exit Sub
Catch: filename = ""
Resume Next
End Sub
' Customize this function.
Private Function ProcessSingleFile(path As String, filename As String, docType As swDocumentTypes_e, swApp As SldWorks.SldWorks, fso As FileSystemObject) As Boolean
' Debug.Print "----"
' Debug.Print "Your code to process a single document (with SldWorks::OpenDoc6, QuitDoc, etc.) goes here.--"
Dim logString As String
logString = "Path: " & path & ", Filename: " & filename & ", Type: " & docType & " Exists? "
' Debug.Print "----"
If fso.FileExists(path & "\" & filename) = True Then
Dim swModel As ModelDoc2
Dim vConfigNames As Variant
vConfigNames = swApp.GetConfigurationNames(path & "\" & filename)
Dim docSpec As DocumentSpecification
Set docSpec = swApp.GetOpenDocSpec(path & "\" & filename)
' docSpec.ConfigurationName = "<config name>"
Set swModel = swApp.OpenDoc7(docSpec)
Debug.Print "Warnings: " & docSpec.Warning
Debug.Print "Errors: " & docSpec.Error
' Save as another document type, query model values, etc...
swApp.QuitDoc (path & "\" & filename)
logString = logString & "True"
ProcessSingleFile = True
Else
logString = logString & "False"
ProcessSingleFile = False
End If
Debug.Print logString
End Function
The IterateFiles function takes a:
- root pathname (e.g. “C:\parts\”)
- filename filter (e.g. *.sldprt)
- directory and file counters (pass an uninitialized long to each)
- FileSystemObject (reference Microsoft scripting runtime in your macro)
- SldWorks object
and calls the ProcessSingleFile function on each file in the root path and all its subdirectories. The ProcessSingleFile function is set up to give the developer a path, filename, file type, and SldWorks object—what you need to open a document, process it, and save and close it.
Using this template can eliminate a lot of the overhead of developing file processing solutions. |