Example 6 - batch open.swb
' ***************************************************************
'
' batch open.swb - macro recorded on 01/14/00 by
' Joe Jones joe@nhcad.com
' New Hampshire CAD www.nhcad.com
'
' This program will do a batch OPEN / CHANGE DENSITY / SAVE / CLOSE
' to all part files ".SLDPRT" found in the working directory.
' With some simple modifications it could be used to batch plot
' or set a host of other user defined values.
'
' to run this program
' 1) start solidworks - do not open any part files
' 2) edit this macro to use the correct working direcotry
' (see "workDir" below)
' 3) run the macro
' ***************************************************************
Dim swApp As Object
Dim ModelDoc As Object
Dim ReturnVal As Long
Dim Response As String
Dim DocName As String
Dim Success As Boolean
Dim DocType As String
' *********** YOU MAY HAVE TO CHANGE THIS ***********
' change the following constant to target a directory
Const workDir = "c:\jj\sldworks\"
' *********** YOU MAY HAVE TO CHANGE THIS ***********
' change the following constant to target a type of file
Const swDocType = ".SLDPRT" ' I am only want to open part files
' the following constants are used in the OpenDoc2() function
Const swDocNONE = 0
Const swDocPART = 1
Const swDocASSEMBLY = 2
Const swDocDRAWING = 3
Const readOnly = 0 ' 0-false 1-true
Const viewOnly = 0 ' 0-false 1-true
Const silent = 1 ' 0-false 1-true
' the following constants are used in the
' SetUserPreferenceDoubleValue() function
Const swDetailingNoteFontHeight = 0
const swDetailingDimFontHeight = 1
Const swSTLDeviation = 2
Const swSTLAngleTolerance = 3
Const swSpinBoxMetricLengthIncrement = 4
Const swSpinBoxEnglishLengthIncrement = 5
Const swSpinBoxAngleIncrement = 6
Const swMaterialPropertyDensity = 7
' start of main program
Sub main()
Set swApp = CreateObject ("SldWorks.Application")
ChDir(WorkDir)
Response = Dir(workDir)
Do Until Response = ""
' see if filename ends with .SLDPRT
If Right(Response, 7) = swDocType Then
' open the SolidWorks part file
Set modelDoc = swApp.OpenDoc2 (Response, _
swDocPart, readOnly, viewOnly, _
silent, returnVal)
' add your own code here to do
' whatever you want to the part file
Success = ModelDoc.SetUserPreferenceDoubleValue _
(swMaterialPropertyDensity, 2699) ' kg/(cu meter)
' close the part
DoEvents
DocName = ModelDoc.GetTitle
returnVal = ModelDoc.Save2 (silent)
swApp.CloseDoc DocName
Set modelDoc = Nothing
End If
' get the next filename
Response = Dir
Loop
Set swApp = Nothing
End Sub