7 Replies Latest reply on Jan 6, 2013 9:48 PM by Dan Miel

    How to open a macro form to the front?

    Tom Strohscher

      I've created a macro with a form.  When I run the marco the corm open behind SW and the user can't see it. 

      How can I open the form to the front?

       

      m_Capture.JPG

        • Re: How to open a macro form to the front?
          Steven Smith

          Add the X64 module as a second module.

           

          Then copy and paste the Userform module1 text into your initial module.

           

          This will bring your userform to the front.

          • Re: How to open a macro form to the front?
            Artem Taturevych

            This is the known issue of x64 VBA macros. There is a workaround to forcibly make the SolidWorks window parent to the macro. Please refer the solution S-013887 in Knowledge Base. You may find the sample macro there.

             

            You can also find a sample on Keith's web-site: http://www.cadsharp.com/macros/keep-vba-user-form-on-top-on-64-bit-systems-vba/

            __________________________

            Regards,

            Happy New Year!

            Artem Taturevych

            Application Engineer at Intercad

            http://intercad.com.au/

            Tel: +61 2 9454 4444

            • Re: How to open a macro form to the front?
              Keith Rice

              Thanks Artem.

               

              Tom--- the macro on my site that Artem pointed to is a vastly simplified version of the macro provided in solution S-013887, so you may find it easier to work with. Also, it centers the user form in the middle the screen by first determining the height and width. Should you want to modify the placement, you will need to modify these lines:

               

              lFormXval = sWinPlacementForm.rcNormalPosition.Right / 2
              lFormYval = sWinPlacementForm.rcNormalPosition.Bottom / 2

              'X and Y locations for form
              lXval = lSolidWorksXval - lFormXval
              lYval = lSolidWorksYval - lFormYval

               

              Keith

              Video Tutorials for the SolidWorks API

              • Re: How to open a macro form to the front?
                Josh Brady

                Or update to 2013.  I believe (according to what I've read) that the problem of forms popping up behind SW is due to VBA being 32 bit while SW is 64, so VBA has to run in a separate process that can't take focus.

                 

                Per the What's New, 2013 has a 64 bit version of VBA, so this problem should be solved.  Caveat: I haven't tried this myself yet...

                • Re: How to open a macro form to the front?
                  Tom Strohscher

                  Thanks I got it all working.

                   

                  I understand we have gotten back on maint and will be upgrading from 2011 to 2013.

                   

                  Will I have to convert my macros to 64 bit?

                    • Re: How to open a macro form to the front?
                      Dan Miel

                      Tom,

                      If you have an api call that has a Long number you will need to convert your Longs to be 64 bit compatible.  If the only api calls you have are in the module to put the form on top then you can remove the module and you should be fine.

                      Dan Miel

                      SW 2011

                      • Re: How to open a macro form to the front?
                        Dan Miel

                        I thought I would add more information in case a person uses api calls other that putting a form on top.

                        In one of my programs I use an api call to get the name of the computer that is running the program. Below is the code I used. It is written so that the program can be used in VBA or VBA7.

                        You can Google PtrSafe or Longptr for more complete information.

                        I hope this helps.

                         

                        ‘Written Dan Miel 10-16-2012

                        ‘****If VBA7 use the first call or if VBA use the second.

                        #If VBA7 Then  '11-16-2012

                        Declare PtrSafe Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As LongPtr) As LongPtr

                        #Else

                        Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

                        #End If

                         

                         

                        ‘***Use this function to return the computer name.

                        Function UserName() As String

                             ' Dimension variables

                        #If VBA7 Then  '11-16-2012

                            Dim ret As LongPtr ', UserName As String

                        #Else

                            Dim ret As Long ', UserName As String

                        #End If

                             Dim lpBuff As String * 25  

                             ' Get the user name minus any trailing spaces found in the name.

                             ret = GetUserName(lpBuff, 25)

                             UserName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)

                             ' Display the User Name

                             'MsgBox UserName

                             End Function