10 Replies Latest reply on Jul 5, 2017 11:19 AM by Chad Hawkenson

    Customized Macro Button Run Time Error

    Chad Hawkenson

      I am wondering if anyone can help me. I have a macro that works great when running with the "play" macro button. I created a customized macro button for ease for other users and now have a run time error. (I highlighted in red below)

       

      Sub CalcFaceProperties()

       

       

          Dim vEdges As Variant

          Dim vCurveParams As Variant

          Dim dTotalPerimeter As Double

          Dim lOuterLoop As Long

          Dim dArea As Double

          Dim xLoop As Long

          Dim lLoopCount As Long

          Dim xEdge As Long

          'Dim dMassMeter As Double

          'Dim dAnodisingPerimeter As Double

          'Dim dExtrusionFactor As Double

         

          'get active document

         Set swDoc = swApp.ActiveDoc

         

          'check for active document

          If swDoc Is Nothing Then

         

              MsgBox "No active part document.     ", vbCritical + vbOKOnly, "Face Properties"

              Exit Sub

             

          ElseIf swDoc.GetType <> swDocPART Then

         

              MsgBox "Active document not a part.     ", vbCritical + vbOKOnly, "Face Properties"

              Exit Sub

       

      Would be great if I could get this to work properly. Any help is greatly appreciated.

        • Re: Customized Macro Button Run Time Error
          Deepak Gupta

          If you're running the macro in excel file then add this line

          Set swApp = CreateObject("SldWorks.Application")

          above

          Set swDoc = swApp.ActiveDoc

           

          If in SOLIDWORKS then use line

          Set swApp = Application.SldWorks

          • Re: Customized Macro Button Run Time Error
            Cad Admin

            Chad, try this...

             

            Sub CalcFaceProperties()

             

                Dim swApp                     As SldWorks.SldWorks

                Dim swDoc                       As SldWorks.ModelDoc2

             

                Dim vEdges As Variant

                Dim vCurveParams As Variant

                Dim dTotalPerimeter As Double

                Dim lOuterLoop As Long

                Dim dArea As Double

                Dim xLoop As Long

                Dim lLoopCount As Long

                Dim xEdge As Long

              

                'Dim dMassMeter As Double

                'Dim dAnodisingPerimeter As Double

                'Dim dExtrusionFactor As Double

                'get active document

                Set swApp = Application.SldWorks

                Set swDoc = swApp.ActiveDoc

              

                'check for active document

             

                If swDoc Is Nothing Then

                     MsgBox "No active part document.     ", vbCritical + vbOKOnly, "Face Properties"

                    Exit Sub

                 

                ElseIf swDoc.GetType <> swDocPART Then

                     MsgBox "Active document not a part.     ", vbCritical + vbOKOnly, "Face Properties"

                    Exit Sub

                    End If

            End Sub

              • Re: Customized Macro Button Run Time Error
                Chad Hawkenson

                This got rid of the bug which is great and appreciated. However now when I run the macro with my customized button it returns the correct value but doesn't update the appropriate Custom Property like it does if I run the macro with the "Run Macro Button". I have attached the macro.

                  • Re: Customized Macro Button Run Time Error
                    Deepak Gupta

                    Make sure you set the method to main while setting the macro button.

                     

                      • Re: Customized Macro Button Run Time Error
                        Chad Hawkenson

                        Good Morning Deepak,

                        Is there a way to adjust the macro so it returns the calculations in Inches (") instead of m.m.? If that is possible it would solve my other issues. Thank you for your help.

                          • Re: Customized Macro Button Run Time Error
                            Deepak Gupta

                            Yes can be done. Change these two lines and replace mm factor with inch factor.

                             

                            'convert face area into mm^2

                                dArea = dArea * 1000000#

                             

                            'convert total perimeter to milimeters

                                dTotalPerimeter = dTotalPerimeter * 1000#

                              • Re: Customized Macro Button Run Time Error
                                Chad Hawkenson

                                    'convert face area into mm^2

                                    dArea = (dArea * 1000000#) / 25.4

                                 

                                    'convert total perimeter to milimeters

                                    dTotalPerimeter = (dTotalPerimeter * 1000#) / 25.4 

                                 

                                Is this how you would do it? I am not sure if this is the correct way. When I go to save the changes it reverts back to:

                                'convert face area into mm^2

                                    dArea = dArea * 1000000#

                                 

                                'convert total perimeter to milimeters

                                    dTotalPerimeter = dTotalPerimeter * 1000#

                                 

                                it doesn't retain my changes.

                                 

                                I do appreciate your help.

                                  • Re: Customized Macro Button Run Time Error
                                    Deepak Gupta

                                    You get default linear values in Meter and Angles in Radian.

                                     

                                    So to convert to any other unit, you need to use the appropriate factor. For example

                                    'convert face area into in^2

                                        dArea = dArea * 1550.0031000062

                                    'convert total perimeter to inches

                                    dTotalPerimeter = dTotalPerimeter * 39.37007874

                                    Use rounding as needed.

                                     

                                    And in this line, change mm to in

                                     

                                    MsgBox "The following values have been saved as custom properties in this model:     " & vbLf & _

                                                "(number in parenthesises equals value that was saved)     " & vbLf & vbLf & _

                                                "Section Area = " & dArea & " (" & PropertyValue(2) & ")" & " mm^2     " & vbLf & _

                                                "Total Perimeter = " & dTotalPerimeter & " (" & PropertyValue(3) & ")" & " mm       ", vbInformation + vbOKOnly, "Face Properties"

                        • Re: Customized Macro Button Run Time Error
                          Chad Hawkenson

                          Thank you both. Its up and working perfectly now!

                          • Re: Customized Macro Button Run Time Error
                            Chad Hawkenson

                            It is now working absolutely perfectly. Thank you so much!