I pieced this together some time ago from web resources.
I was trying to get it to scan the document and set all reference dims to a different tolerance than the model dimensions.
I only got as far as scanning selected dimensions.
You should be able to take it and modify it to suit your needs:
Dim myInstance As IModelDoc2
Dim myLength As Integer
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swDisplayDimension As SldWorks.DisplayDimension
Dim swDimension As SldWorks.Dimension
Dim swDimensionTolerance As SldWorks.DimensionTolerance
' scans selected dimensions
' if the dimension is a model dimension, it sets the tolerance LIMIT to
' 4 decimal places
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Dim i As Integer
For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
If swSelMgr.GetSelectedObjectType3(i, -1) = swSelDIMENSIONS Then
Set swDisplayDimension = swSelMgr.GetSelectedObject6(i, 0)
'make sure we have a dimension
Set swDimension = swDisplayDimension.GetDimension
Set swDimensionTolerance = swDimension.Tolerance
myDriven = swDimension.DrivenState
If myDriven = 1 Then 'Reference dimension, set to two decimal place
'myTol = 2
'Skip driven dimensions
'swDimensionTolerance.Type = swTolNONE ' set no tolerance
ElseIf myDriven = 2 Then 'Model dimension, set to 4 decimal places
myTol = 4
swDimensionTolerance.Type = swTolLIMIT ' set toleranced limit
myval1 = swDisplayDimension.SetUnits(False, swINCHES, swDECIMAL, 1, False)
myval = swDisplayDimension.SetPrecision2(myTol, myTol, myTol, myTol)
Thank you much for this, but i am looking to change the document units, not just the dimensions. Please correct me if i am wrong, but this gathers the dimensions and changes the units from the dimensions properties. I am looking to change the units in the drawing options. This allows me to update and add dimensions needed for production without re running the macro every new dimension. I am going to use this macro and modify it a bit to run through my drawings and change all dims to be back to drawing properties. well going to try anyway. Thank you!
I see... My mistake.
What you are trying to do should be simple. I just recorded a macro and changed the settings and it seems to have updated the default units. It should do what you want.
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Set swApp = _
Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsLinearDecimalPlaces, 0, 3)
boolstatus = Part.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsLinearDecimalDisplay, 0, swFractionDisplay_e.swDECIMAL)
boolstatus = Part.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsLinearFractionDenominator, 0, 0)
boolstatus = Part.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsDualLinearDecimalDisplay, 0, swFractionDisplay_e.swDECIMAL)
boolstatus = Part.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsDualLinearFractionDenominator, 0, 0)
Ya i tried this, but am not having any luck getting it to work properly. I actually have the exact same code in my VBA editor now. Maybe I should Record it again. Learning Solidworks API is a lot tougher than I anticipated.
Guess I recorded something funny when I did it the first time, cause it worked like a charm this time.