8 Replies Latest reply on Sep 10, 2013 2:44 PM by Chris Johnson

    While Loop problems?

    Chris Johnson

      Hello everyone,


      I have a while loop that im using to prompt the user to select a face so that a sketch may be started on it.


      everything works fine (Im able to spin the model around and make selections) when testing it in the vb editor but when I run it from within soldiworks using the "Run Macro" button I cannot spin the model around nor make selections. essentially SW locks up and the only way out is to end the process.


      is there something wrong with the loop im using or the way im connecting to solidworks?



      Option Explicit



      Dim swApp As Object


      Dim Part                                As Object

      Dim boolstatus                          As Boolean

      Dim longstatus                          As Long, longwarnings As Long

      Dim swSelMgr                            As SldWorks.SelectionMgr

      Const SelectedObjectCount = 1


      Sub main()


      Set swApp = Application.SldWorks

      Set Part = swApp.ActiveDoc



      'Select the face to verify macro is workiing

              boolstatus = Part.Extension.SelectByID2("", "FACE", 0.304800000000057, 0.151591133586805, -3.67656190501293E-02, False, 0, Nothing, 0)



      'Selection Setup

              Set swSelMgr = Part.SelectionManager

              Part.ClearSelection2 True

              MsgBox "Select a Face to to start a sketch on, then click OK."


          'Wait while user selects a face and clicks OK

              While swSelMgr.GetSelectedObjectCount < SelectedObjectCount




      End Sub


      thank you in advance for the help



        • Re: While Loop problems?
          Josh Brady

          You at least need a DoEvents statement inside the loop.


          What version of SolidWorks are you running?  If 2012 or earlier plus 64 bit, there may be an additional problem due to VBA being 32 bit and running in a separate process.  That caused all sorts of problems.

            • Re: While Loop problems?
              Chris Johnson

              Hi Josh,


              SW 2012 X64 SP5.0


              the DoEvents didnt fix it.  is there a fix for the additional problem you mentioned?


              thank you


                • Re: While Loop problems?
                  Simon Turner

                  I think you will either need to implement a PropertyManagerPage (fiddly), or ask the user to pre-select a face before running the macro (easier).

                    • Re: While Loop problems?
                      Chris Johnson

                      Thanks Simon,


                      I guess I dont understand why it works when I run the macro from the editor but it doesnt work inside of SW.


                      for lack of a better solution I guess I could have all the end users run the macro from the editor but that just seems silly....

                      Surely I am missing something?





                    • Re: While Loop problems?
                      Josh Brady

                      The simple "while loop" wait is pretty much out of the question with 64 bit SW and 32 bit VBA.  You will need to make a more complicated macro that registers to receive the SelectionChangeNotify (or somethign like that) event.  You may be able to rewrite in a VSTA .dll macro.... I'm not sure.  Simplest fix is to upgrade to 2013.  Simplest, that is, from a purely macro point of view.  I know there are lots of other factors that go into upgrading to the next release.  :-)

                        • Re: While Loop problems?
                          Chris Johnson

                          Thanks Josh,


                          Wow, I had no idea this limitation existed I just spent a month learning vba and writing 800+ lines of code only to find out it wont work in 64 bit SW.


                          I suppose I can try to learn VSTA real quick and push everything over to that...

                          shouldn't be too hard to figure out if it will work.


                          thank you very much  for your help. even though it was horrible news I really appreciate you taking the time to reply