I have some code that changes the text and precision of selected dimensions as follows:
'Set a reference to the select set of the active document.
Dim oDoc As DrawingDocument
Set oDoc = ThisApplication.ActiveDocument
Dim oDrawingDims() As DrawingDimension
Dim oItemsCount As String
oItemsCount = oDoc.SelectSet.Count
Dim oDimText As String, oDimPrecision As Long
oDimText = gDimText
oDimPrecision = gDimPrecision
If oItemsCount = 0 Then
Dim a As Long
a = 0
Dim i As Long
For i = 1 To oDoc.SelectSet.Count
If Not oDoc.SelectSet.Item(i) Is Nothing Then
If TypeOf oDoc.SelectSet.Item(i) Is DrawingDimension Then
a = a + 1
ReDim Preserve oDrawingDims(0 To a)
Set oDrawingDims(a) = oDoc.SelectSet.Item(i)
Dim b As Long
For b = 1 To a
oDrawingDims(b).Text.FormattedText = oDimText
oDrawingDims(b).Precision = oDimPrecision
The problem I have is that after it is modified the Dimension correctly, it de-selects all the items so there is nothing selected.
How can I add some more code in VBA so it will keep the selected items selected?
Please note that I did not create the code above, I copied if from the web and changed it to suit. I can't remember who created the original code, but i has saved me a lot of time and effort.
If anyone can guide me on how to re-select the selected items that will save us even more time.
Solved! Go to Solution.
Any transacted operation will clear the select set, for example modifying a dimension properties. You need to store the content of the select set before doing any modification. You can for example create and populate an ObjectCollection or a simple array of variants.
I hope it helps,
I did think it would be something like that.
I have tried to get create a Object Collection but I cannot work out how to re-select them and add them to it.
Could you please advise on the code needed to create the object collection, add the dimensions to it and then re-select the collection?
You create the ObjectCollection with TransientObjects.CreateObjectCollection. You should be able to find plenty of examples in the API Help Files.
ObjectCollection.Add to add objects to it.
You can re-select the objects with SelectSet.Select.
I have tried to work out the VBA code by using the Inventor API Help and Examples and I cannot get it to work.
Would you be able to supply me the code to get the selected items into a object collection and then how to recall them?
There will normally be more than 1 item selected at a time.
Many thanks in advance for your assistance.
How about that:
Sub DimTest() Dim doc As DrawingDocument Set doc = ThisApplication.ActiveDocument Dim collec As ObjectCollection Set collec = ThisApplication.TransientObjects.CreateObjectColle
ction Dim obj As Variant For Each obj In doc.SelectSet collec.Add obj Next For Each obj In collec If TypeOf obj Is DrawingDimension Then Dim drawDim As DrawingDimension Set drawDim = obj drawDim.HideValue = True drawDim.OverrideModelValue = True drawDim.Text.FormattedText = "ADN rules!" End If Next For Each obj In collec doc.SelectSet.Select obj Next End Sub
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
That is perfect. I will modify the code to incorporate your method.
Many thanks you have saved us a lot of time and effort.