4 Replies Latest reply on Nov 15, 2013 4:01 PM by Matt Martens

    Problems with GetObject

    Matt Martens

      I am executing the following code from within excel and am having trouble with the GetObject line.

       

      On Error Resume Next

      Set swApp = GetObject(, "SldWorks.Application")

      If swApp Is Nothing Then

          Set swApp = CreateObject("SldWorks.Application")

          swApp.LoadAddIn "C:\Program Files\SolidWorks Corp\SolidWorks\Toolbox\SwBrowser.dll"

          swApp.LoadAddIn "C:\Program Files\SolidWorks Corp\SolidWorks\Toolbox\SwToolbox.dll"

          swApp.LoadAddIn "C:\Program Files\SolidWorks Corp\SolidWorks Explorer\PDMWorks Clientx64 2013\pdmwaddin.dll"

          swApp.RunMacro "S:\SolidWorks File Locations\Macros\StartUpMacro.swp", "StartUpMacro1", "main"

      End If

       

       

      swApp.FrameState = 1

      swApp.OpenDoc dir & "\" & swFilename, FileType

       

      I am attempting to get the currently running session of SW and if it doesn't exist open a new session, load add-ins, and run a macro I always run at startup. The problem is that the GetObject doesn't actually get the currently running session. I tried using the argument GetObject("", "SldWorks.Application") as the first argument and it worked for when SW was running, but when SW is not running that argument will start SW and then my add-ins don't get loaded and my macro doesn't run.

       

      I can deal with loading the add-ins even if they are already loaded but I don't want to run the macro if SW is already running.

       

      Any ideas why the GetObject is not working properly?

        • Re: Problems with GetObject
          Matt Martens

          Can anybody help me?

            • Re: Problems with GetObject
              Josh Brady

              Just a shot in the dark... You don't have swApp declared as "Dim swApp as New..." do you?

                • Re: Problems with GetObject
                  Matt Martens

                  No. I came across a few threads where users had the same problem and it was resolved by running a function that uses "SldWorks.Application." & revision number and loops through until it finds the version that is currently running, but i cannot get consistent results with this method. Either the function fails to pass the object back or, when the application was started by my code, it fails to get object altogether.

                   

                  I was hoping some of the programmers more experienced than I could point me to some things to try.

                    • Re: Problems with GetObject
                      Matt Martens

                      I wanted to post my resolution so others with the same issue can find a solution or in case I forget. I was able to get more consistent results by using the following:

                       

                      On Error Resume Next

                      Set swApp = GetObject(, "SldWorks.Application")

                      If swApp Is Nothing Then

                          Shell "C:\Program Files\SolidWorks Corp\SolidWorks\SLDWORKS.exe", vbMaximizedFocus

                          Do

                              tStop = Timer + 60

                              Set swApp = GetObject(, "SldWorks.Application")

                          Loop Until Not swApp Is Nothing Or Timer > tStop

                          swApp.RunMacro "S:\SolidWorks File Locations\Macros\StartUpMacro.swp", "StartUpMacro1", "main"

                      End If

                      swApp.FrameState = 1

                      swApp.OpenDoc directory & "\" & swFilename, FileType

                       

                       

                      By using the shell method to start SW when an instance was running, I don't have to load the add-ins and the GetObject works better on subsequent runs. Note, the timer is there to insure you don't get stuck in an endless loop if the the Shell command doesn't get SW opened. I set the timer to timeout at 60 seconds because that should be more than enough time on most systems.