4 Replies Latest reply on May 25, 2016 8:04 AM by John Alexander

    Selecting/browsing folder for pack and go macro

    A. D.

      Dear all,

      I've recently found a macro online, which pack and go's some solidworks files. This works quite well, except that it always pack and go's to a hardcoded folder. Instead of this hardcoded folder, i would like to let the user choose where to place/save the pack and go through a browser window like you get when you 'save as' something.

      The thing is, i've got no clue on how to produce such a macro, and would be very happy if you guys could help me with this.


      Ideally, as an extra, it would be great if it would start browsing in a specific folder  (this folder may be hardcoded, that's no problem).


      I hope you guys can help me with this,


      Thanks in advance!

        • Re: Selecting/browsing folder for pack and go macro
          Deepak Gupta

          Need to look at the macro to suggest/make changes. Please upload the macro here.

            • Re: Selecting/browsing folder for pack and go macro
              A. D.

              I'm still working on/adapting the macro to my needs, but this is what i have thusfar: (Can be a bit messy, it's Work In Progress )



              Const strOutputPath As String = "C:\_Pack\fdahinden_KB_15_"
              Const strCustProp As String = "Index"
              Sub main()
                  Dim swapp As SldWorks.SldWorks
                  Dim swmodel As SldWorks.ModelDoc2
                  Dim swCompModel As SldWorks.ModelDoc2
                  Dim swCustPropMgr As SldWorks.CustomPropertyManager
                  Dim swPackAndGo As SldWorks.PackAndGo
                  Dim strValOut As String
                  Dim strFileExt As String
                  Dim intDocCount As Integer
                  Dim vPathNames As Variant
                  Dim strSetPathNames() As String
                  Dim i As Integer
                  Dim vStatus As Variant
                  Dim strRetVal As String
                  Dim Number      As String
                  Set swapp = Application.SldWorks
                  Set swmodel = swapp.ActiveDoc
                  'Error handling
                  If swmodel Is Nothing Then
                      swapp.SendMsgToUser "Please open an assembly or drawing."
                      Exit Sub
                  ElseIf swmodel.GetType <> swDocASSEMBLY And swmodel.GetType <> swDocDRAWING Then
                      swapp.SendMsgToUser "Please open an assembly or drawing."
                      Exit Sub
                  End If
                 Number = InputBox("KB Nummer")
                  If Number = "" Then
                  MsgBox "Please enter number"
                  'Get IPackAndGo object
                  Set swPackAndGo = swmodel.Extension.GetPackAndGo
                  'Resize the set paths array
                  intDocCount = swPackAndGo.GetDocumentNamesCount
                  ReDim strSetPathNames(intDocCount - 1) As String
                  'Get the current document paths and append the Revision custom property
                  swPackAndGo.GetDocumentNames vPathNames
                  For i = 0 To UBound(vPathNames)
                      strSetPathNames(i) = vPathNames(i)
                      'The document should be open in memory, so get it
                      Set swCompModel = swapp.GetOpenDocumentByName(strSetPathNames(i))
                      If Not swCompModel Is Nothing Then
                          'Get the custom property value
                          Set swCustPropMgr = swCompModel.Extension.CustomPropertyManager(Empty)
                          swCustPropMgr.Get4 strCustProp, True, Empty, strValOut
                      End If
                          'strSetPathNames(i) = "KB_15_" & Number & Right(strSetPathNames(i), Len(strSetPathNames(i)) - InStrRev(strSetPathNames(i), "\"))
                          strSetPathNames(i) = "test"
                  Next i
                  'Set the save paths
                  swPackAndGo.SetDocumentSaveToNames strSetPathNames
                  'IPackAndGo::SetSaveToName is needed for two reasons. First, it is needed for _
                      IPackAndGo::IncludeDrawings to work. Second, it is needed if the pack and go _
                      is to result in a .zip file. Also note that in this example, at least, I am not _
                      able to see any difference between Override being True or False
                  swPackAndGo.SetSaveToName True, strOutputPath
                  'Include drawings
                  swPackAndGo.IncludeDrawings = True
                  'Save the pack and go
                  vStatus = swmodel.Extension.SavePackAndGo(swPackAndGo)
                  End If
              End Sub
            • Re: Selecting/browsing folder for pack and go macro
              Matt Martens

              See the code below:


              Dim MyShell As New Shell32.Shell

              Dim myFolder As Shell32.Folder3


              'Set the root folder

              rootFolder = "your root folder here"


              'Call Windows folder browser opened to your root folder for user to specify which folder to use

              Set myFolder = MyShell.BrowseForFolder(0, "Set browse window caption here.", 16, rootFolder)


              You will need to turn on the reference to the "Microsoft Shell Controls and Automation" library for this to work.

              • Re: Selecting/browsing folder for pack and go macro
                John Alexander

                You can create a form in VBA that will allow the user to specify a path.



                Minimally, all you need to achieve your purpose is a text box



                Dim strOutputPath As String

                Dim user_form As New UserForm1


                user_form.show ' shows the userform

                strOutputPath  = user_form.TextBox1.value


                In the UserForm module there are two views: GUI and code. In the code, you can register callback functions for handling events like button presses, keyboard strokes, etc.



                The following should respond to any a keypress event, check if "Enter" was pressed, hide the userform and return execution to the sub/function that called it.


                Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

                    If KeyCode = vbKeyReturn Then


                    End If

                End Sub