Macro with the macro recorder not working

Hi all

Why does a macro recorded with the macro recorder not work when you start it?
With Excel there are usually never any problems, but with SolidWorks, it's almost the opposite.
Here is my saved macro once edited in the assembly:

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

' Redraw
Part.GraphicsRedraw2

' Redraw
Part.GraphicsRedraw2

' Redraw
Part.GraphicsRedraw2

' Redraw
Part.GraphicsRedraw2
Dim BoundingBox As Object
Set BoundingBox = Part.FeatureManager.InsertGlobalBoundingBox(swGlobalBoundingBoxFitOptions_e.swBoundingBoxType_BestFit, False, False, longstatus)
Part.ClearSelection2 True

' Redraw
Part.GraphicsRedraw2

' Redraw
Part.GraphicsRedraw2

' Redraw
Part.GraphicsRedraw2

' Redraw
Part.GraphicsRedraw2
End Sub

As for my recording, I edit my part in the assembly and I launch my macro, and nothing happens.

What for?

Thanks in advance

The SW macro recorder is indeed not the best, far from it...
In general, this is used in very rare cases to identify the necessary function.
But it generates a lot of useless and absolutely disgusting code for you anyway!
Here is Part.GraphicsRedraw2 that we find too often.
I don't know what you were trying to do, maybe the bounding box?
But the internet is often a better solution to the recorder.

3 Likes

Hello.

I agree with @sbadenis ... The recorder can be greatly improved.

Try instead:

https://help.solidworks.com/2021/english/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.ifeaturemanager~insertglobalboundingbox.html?verRedirect=1
associated with:
https://help.solidworks.com/2021/english/api/sldworksapi/redraw_graphics_example_vb.htm

Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2    
Dim swModel As SldWorks.ModelDoc2
Dim boolstatus As Boolean
Dim longstatus As Long
Option Explicit

Sub main()
    
    Set swApp = Application.SldWorks
    Set Part = swApp.ActiveDoc 
    Set swModel = swApp.ActiveDoc

    ' Display the Bounding Box sketch
    boolstatus = Part.SetUserPreferenceToggle)swViewDispGlobalBBox, True)
    
    Dim BoundingBox As SldWorks.Feature
    Set BoundingBox = Part.FeatureManager.InsertGlobalBoundingBox(swBoundingBoxType_BestFit, True, False, longstatus)

    Part.ClearSelection2 True

    ' Hide the Bounding Box sketch
    boolstatus = Part.SetUserPreferenceToggle)swViewDispGlobalBBox, False)

swModel.GraphicsRedraw2

End Sub

Kind regards.

3 Likes