AnsweredAssumed Answered

MACRO Help Wanted - learning the language

Question asked by Tyler Smith on Aug 23, 2016
Latest reply on Aug 24, 2016 by Tyler Smith

Hey Everyone,


Hoping someone can help me out with the following issue I am having.  Basically I created a Macro through SolidWorks to save open drawings as a .dwg file, close that doc, and move to the next one.  My question is:


  • What do I need to write to create a loop that ends when there are no more "active docs"? Currently I take the code below and copy&paste it 20 times with an End Sub at the end.  Obviously the program lets me know I screwed up when I run it and it runs out of docs prior to 20, or I have to run it again when it gets to 20 and closes.


Dim swApp As SldWorks.SldWorks

Dim Part As SldWorks.ModelDoc2

Dim sPathName As String

Dim boolstatus As Boolean

Dim longstatus As Long, longwarnings As Long

Sub main()


Set swApp = Application.SldWorks


Set Part = swApp.ActiveDoc

boolstatus = Part.Extension.SelectByID2("Sheet1", "SHEET", 0.371587160183066, 0.054268814645309, 0, False, 0, Nothing, 0)

boolstatus = Part.ActivateView("Drawing View1")

boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)

Part.ClearSelection2 True

boolstatus = Part.EditRebuild3()

Part.ViewZoomTo2 0, 0, 0, 0.1, 0.1, 0.1

Part.ViewZoomTo2 0, 0, 0, 0.1, 0.1, 0.1

Part.ViewZoomTo2 0, 0, 0, 0.1, 0.1, 0.1

Part.ViewZoomTo2 0, 0, 0, 0.1, 0.1, 0.1


sPathName = Mid(Part.GetPathName, InStrRev(Part.GetPathName, "\") + 1)

sPathName = Left(sPathName, InStrRev(sPathName, ".") - 1)

longstatus = Part.SaveAs3(sPathName & ".DWG", 0, 0)

swApp.CloseDoc Part.GetPathName


MsgBox "All Done"


End Sub



I am very new to all of this and have really started to dig into programming and understanding how this all works.  But because this is still so new, please feel free to "dumb it down" I will not take offense.  The more you can explain why you did something, the more it will help.


And if I posted in the wrong section, please move this question.