AnsweredAssumed Answered

Macro does nothing on first run.

Question asked by Mark Soldan on Jun 18, 2020
Latest reply on Jun 19, 2020 by Mark Soldan

I am finding that a lot of my macros do nothing the first time I run them.  When I hit run a second time, it does what I expect.  It will continue to work each time I hit run as long as I do not change what is selected in the SolidWorks window.  Any time I change what is selected, I have to hit run a second time to get it to "wake up".  I think is a problem with my use of the selection manager.  Here is an example of part of a macro that exhibits this problem:


1) run on a drawing

2) select a cell in a table

3) macro will widen column #1 (but not the first time it is run)



Option Explicit

Sub main()

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swSheet As SldWorks.Sheet
Dim swTableAnn As SldWorks.TableAnnotation
Dim swAnnotation As SldWorks.Annotation
Dim swSelectionMgr As SelectionMgr
Dim DoubleStatus As Double
Dim idx As Long


Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swDraw = swModel
Set swSheet = swDraw.GetCurrentSheet
Set swSelectionMgr = swModel.SelectionManager
For idx = 1 To swSelectionMgr.GetSelectedObjectCount2(-1)

Set swTableAnn = swSelectionMgr.GetSelectedObject6(idx, -1)
Set swAnnotation = swTableAnn.GetAnnotation

Next idx
DoubleStatus = swTableAnn.SetColumnWidth(1, swTableAnn.GetColumnWidth(1) + 0.00254, 0)

End Sub


Thank you for your help