18 Replies Latest reply on Jun 6, 2013 6:54 PM by Josh Brady

    Macro doesn't open in 2013sp0

    Nico Van Gils

      Hi,

       

      Having a problem opening macro's in SW2013 sp0:

       

      - Downloaded a 3D file (.swb) of a bearing from the SKF website (file in attach)

      - Tried to run it in SW2013 sp0. (this is the first macro I ran)

      - Nothing happens. SW doesn't even hang.

      - When editing the macro and debugging it gives a few errors. But my knowledge of VB isn't that great to solve it.

       

      Already tried what's in the tread concerning SW2013 beta 1

      https://forum.solidworks.com/message/301044#301044

       

      Anyone having the same problem?

       

      Nico

        • Re: Macro doesn't open in 2013sp0
          Frank Lindeman

          Hello all,

           

          Further to Artem's suggestions, please find below the changes that need to be made in order to use the Win32 Folder Browser API functions used in the macro::

           

            Change #1

           

            Public Type BROWSEINFO

            #If VBA7 Then

                hWndOwner       As LongPtr
                PIDLRoot        As LongPtr
                pszDisplayName  As String
                lpszTitle       As String
                ulFlags         As Long
                lpfnCallback    As LongPtr
                lParam          As LongPtr
                iImage          As Long

            #Else

                hWndOwner       As Long
                PIDLRoot        As Long
                pszDisplayName  As String
                lpszTitle       As String
                ulFlags         As Long
                lpfnCallback    As Long
                lParam          As Long
                iImage          As Long

            #End If

            End Type

           


            Change #2


            #If VBA7 Then

              Public Declare PtrSafe Function SHBrowseForFolder Lib "Shell32" (lpbi As BROWSEINFO) As Long

              Public Declare PtrSafe Function SHGetPathFromIDList Lib "Shell32" (ByVal pidList As LongPtr, ByVal lpBuffer As String) As Long

              Private Declare PtrSafe Sub CoTaskMemFree Lib "ole32" (ByVal hMem As LongPtr)

            #Else

              Public Declare Function SHBrowseForFolder Lib "Shell32" (lpbi As BROWSEINFO) As Long

              Public Declare Function SHGetPathFromIDList Lib "Shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long

              Private Declare Sub CoTaskMemFree Lib "ole32" (ByVal hMem As Long)

            #End If

           

            Change #3

           

            Public Function BrowseForFolder() As String
              Dim tBI         As BROWSEINFO
            #If VBA7 Then
              Dim lngPIDL     As LongPtr
            #Else
              Dim lngPIDL     As Long
            #End If

           

           

          With these changes the code can be used in SW2012 and earlier and also in SW2013 and later.

           

          There is another entry on the SolidWorks Forum on the Win32 APIs used in this macro:

           

                          How to use Win32 APIs from a SolidWorks VBA macro which must support SW2012 & SW2013.

                          https://forum.solidworks.com/docs/DOC-2241

           

           

          More general information can be found at:

           

                          SolidWorks 2013 API changes

                          https://forum.solidworks.com/docs/DOC-2141

           

           

          And now is the time to think about visiting SolidWorks World 2013:

           

          Title:Visual Basic® for Applications 7: Getting SolidWorks Macros Ready for 64-Bit
          Presentation Category:Design Automation
          Sub-Category:API
          Session Type:Breakout Session
          Presentation Level:Intermediate (1-3 years' experience)
          Abstract:Hear an introduction to the language changes of Visual Basic for Applications 7 in SolidWorks 2013, supporting 64-bit. This presentation will outline scenarios to adapt existing macros using external Windows® and ActiveX® controls dlls for use with SolidWorks 2013, while keeping them backward-compatible.
          Attendee Benefits:Attendees will learn to take advantage of the new VBA 7 features and what needs to be done to protect investments in existing VBA macros.
          Speaker:

          Frank Lindeman 

          SolidWorks Partner Technical Manager (API)

          Dassault Systèmes SolidWorks

           

          Frank Lindeman has been a technical manager with the SolidWorks API group since 2004, working with customers, solution partners, and OEM partners to build powerful SolidWorks-based applications.

          Date:January 22, 2013
          Day and Time:Tuesday, 10:30 AM - 12:00 PM

           

           

          Regards,

           

            Frank

           

           

          SolidWorks API, Partner Technical Manager

          Cambridge, UK

            • Re: Macro doesn't open in 2013sp0
              Frank Lindeman

              FYI: I have reached out to the SolidWorks partner involved in the generation of the SWB macros for SKF product models, in the hope this problem can be resolved at source.

               

              Regards,

               

                Frank

               

               

              SolidWorks API, Partner Technical Manager

              Cambridge, UK

                • Re: Macro doesn't open in 2013sp0
                  Nico Van Gils

                  Frank,

                   

                  Thank you for contacting the guys from SKF. That would resolve a lot of problems.

                   

                  Also thank you for the code to be changed in the macro's. But with the low amount of knowledge concerning macro's and programming them this isn't easy to do.

                   

                  It is a bit like driving a car. You can perform excellent on the road but know absolutely nothing about how the engine works.

                  nico

                    • Re: Macro doesn't open in 2013sp0
                      Frank Lindeman

                      Hello Nico,

                       

                      > Thank you for contacting the guys from SKF. That would resolve a lot of problems.

                       

                      To be clear, I only contacted the partner who did the work for SKF, to make them aware of the issue.

                       

                       

                      >  Also thank you for the code to be changed in the macro's. But with the low amount of knowledge concerning macro's and programming them this isn't easy to do.

                       

                      Although I had attached a fixed macro to my original reply, I realized later I made the changes in the Visual Basic editor, leaving some editor specific lines in the saved out code.

                      I now made the changes in a pure text editor, so the fixed macro is as close as possible to the one you downloaded.

                      See attached.

                       

                       

                      >  It is a bit like driving a car. You can perform excellent on the road but know absolutely nothing about how the engine works.

                       

                      OK, car's fixed now. Happy driving.

                       

                      Regards,

                       

                        Frank

                       

                       

                      SolidWorks Partner Technical Manager (API)

                      Cambridge, UK

                • Re: Macro doesn't open in 2013sp0
                  JJ Meijer

                  I created a swb loader that will replace the browse for folder function and use the FolderBrowserDialog from .net framework.

                   

                  Use open with... or drop (mutliple) .swb files on top of the .exe, batch load is possible this way. Now you can load vba6 .swb files "directly" in sw2013.

                   

                  framework 4.0 required

                   

                  Here is the code:

                   

                   

                  using System;

                  using SolidWorks.Interop.sldworks;

                  using System.IO;

                  using System.Text;

                  using System.Windows.Forms;

                   

                  namespace SwbLoader

                  {

                      class Program

                      {

                          [STAThreadAttribute]

                          static void Main(string[] args)

                          {

                              //Programm used correctly?

                              if (args.Length == 0)

                              {

                                  Console.WriteLine("No file selected!");

                                  Console.ReadKey();

                                  return;

                              }

                   

                              //configure dialog

                              var dial = new System.Windows.Forms.FolderBrowserDialog();

                              dial.ShowNewFolderButton = true;

                              dial.SelectedPath = "";

                   

                              //exit on cancel

                              if (dial.ShowDialog() != System.Windows.Forms.DialogResult.OK)

                              {

                                  return;

                              }

                   

                              //configure file paths

                              string temppath = Path.GetTempPath();

                              string targetpath = dial.SelectedPath;

                             

                              //loop through files

                              foreach (string str in args)

                              {

                                  Console.WriteLine("Processing: " + str);

                                  string filecontent;

                   

                                  using (var tr = File.OpenText(str))

                                  {

                                      filecontent = tr.ReadToEnd();

                                  }

                   

                                  try

                                  {

                                      if (filecontent.Contains("Private Type BROWSEINFO"))

                                      {

                                          int brwsinfoStart = filecontent.IndexOf("Private Type BROWSEINFO");

                                          int brwslength = filecontent.IndexOf("Private Declare", brwsinfoStart) - brwsinfoStart;

                                          filecontent = filecontent.Remove(brwsinfoStart, brwslength);

                                      }

                                      else

                                      {

                                          throw new Exception("\"Private Type BROWSEINFO\" not found!");

                                      }

                   

                                      if (filecontent.Contains("Public Function BrowseForFolder()"))

                                      {

                                          int brwsfuncStart = filecontent.IndexOf("Public Function BrowseForFolder() As String");

                                          int brwsfuncLength = filecontent.IndexOf("Sub CreatePart0", brwsfuncStart) - brwsfuncStart;

                                          filecontent = filecontent.Remove(brwsfuncStart, brwsfuncLength);

                                      }

                                      else

                                      {

                                          throw new Exception("\"Public Function BrowseForFolder()\" not found!");

                                      }

                   

                                      if (filecontent.Contains("Private Declare Function SHBrowseForFolder"))

                                      {

                                          int DecStart = filecontent.IndexOf("Private Declare Function SHBrowseForFolder");

                                          int Declen = filecontent.IndexOf("Private Const", DecStart) - DecStart;

                                          filecontent = filecontent.Remove(DecStart, Declen);

                                      }

                                      else

                                      {

                                          throw new Exception("\"Private Declare Function SHBrowseForFolder\" not found!");

                                      }

                   

                                      if (filecontent.Contains("storePath=BrowseForFolder"))

                                      {

                                          filecontent = filecontent.Replace("storePath=BrowseForFolder", "storePath=\"" + targetpath + "\"");

                                      }

                                      else

                                      {

                                          throw new Exception("\"storePath=BrowseForFolder\" not found!");

                                      }

                                  }

                                  catch(Exception ex)

                                  {

                                      Console.WriteLine(ex.Message);

                                      Console.WriteLine();

                                      Console.WriteLine("Could not detect standard text structure!");

                                      Console.ReadKey();

                                  }

                   

                   

                                  using (var tw = new StreamWriter(Path.Combine(temppath, "temp.swb")))

                                  {

                                      tw.Write(filecontent);

                                  }

                                  RunMacro(Path.Combine(temppath, "temp.swb"));

                              }

                          }

                   

                          static void RunMacro(string path)

                          {

                   

                              var swapp = new SldWorks();

                   

                   

                              swapp.Visible = true;

                              swapp.RunMacro(path, "", "main");

                   

                          }

                   

                      }

                  }

                  • Re: Macro doesn't open in 2013sp0
                    Frank Lindeman

                    Problem and solution are now documented in Knowledge Base Solution S-061791.

                     

                    The SolidWorks partner involved in the generation of the SWB macros has rectified the problem.

                     

                    Regards,

                     

                      Frank

                     

                     

                    SolidWorks Partner Technical Manager (API)

                    Cambridge, UK