Macro packandgo (take-home composition)

Hello everyone,

I'm trying to make this macro from solidworks help work but it doesn't work under solidworks

Can anyone help me?

http://help.solidworks.com/2015/English/api/sldworksapi/Pack_and_Go_an_Assembly_Example_vb.htm?verRedirect=1

 

 

Hello

For me it works very well, provided that I change the path of the file to open ("C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\samples\tutorial\advdrawings\handle.sldasm") which does not exist on my PC and replace it with a valid path.

Kind regards

yes I did change the path 

but it gives me an error "runtime error '91': Object variable or With block varable not defined" when I do debugging this but on "Set swModelDocExt = swModelDoc.Extension" at the beginning of the program

On the other hand, I copied the code into a . SWP and I run the macro from solidworks, I don't see why it doesn't work.

Won't I have a missing library reference?

 

Hello

No. If you had a missing reference, you would have another type of error.

I think you have a path, right, or read-only error. And your swModelDoc object should remain empty. Try taking a step-by-step with [F5] to check the value of your item.

1 Like

That's exactly what I did and I have the same references as you (to SW's version ready).

Are you sure that the paths and assembly names you put are valid?

To see if it comes from there, replace the lines:

openFile = "C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\samples\tutorial\advdrawings\handle.sldasm"
Set swModelDoc = swApp.OpenDoc6(openFile, swDocASSEMBLY, swOpenDocOptions_Silent, "", errors, warnings)

by:

Set swModelDoc = swApp.ActiveDoc

and then opens an assembly before launching the macro.

Small question too, is it an assembly that you put as a file in the path?

One more question, did you put the extension of your assembly in the file name even if it is not displayed in Windows Explorer (.sldasm)?

it works I must have made a mistake in the copy of the path.

I would like to add a function to the "PackAndGo", rename all the files that contain "XXXXX" and rename them with a number 44444 (precision the XXXXX is not in suffix or prefix)

 

yes it's an assembly in the path file with the name of the extension

    Try something like this:

' Verify document paths and filenames after adding prefix and suffix
ReDim pgGetFileNames(namesCount - 1)
ReDim pgDocumentStatus(namesCount - 1)
status = swPackAndGo.GetDocumentSaveToNames(pgGetFileNames, pgDocumentStatus)
For i = 0 To (namesCount - 1)
     If Instr(pgGetFileNames(i), "XXXXXX") <> 0 Then
          pgGetFileNames(i) = Replace(pgGetFileNames(i), "XXXXX", "444444")
     End If
Next i

 

Doesn't  it work?

I have to place it or in the code? I put it at the end like these

'[.....]

' Add a prefix and suffix to the new Pack and Go filenames-------------------- (Aplatissez la structure de dossiers Pack and Go; enregistrer tous les fichiers dans le répertoire racine)
swPackAndGo.AddPrefix = "SW_"
swPackAndGo.AddSuffix = "_PackAndGo"


' Verify document paths and filenames after adding prefix and suffix----------------- (Aplatissez la structure de dossiers Pack and Go; enregistrer tous les fichiers dans le répertoire racine)
ReDim pgGetFileNames(namesCount - 1)
ReDim pgDocumentStatus(namesCount - 1)
status = swPackAndGo.GetDocumentSaveToNames(pgGetFileNames, pgDocumentStatus)
For i = 0 To (namesCount - 1)
If InStr(pgGetFileNames(i), "xxxxx") <> 0 Then
pgGetFileNames(i) = Replace(pgGetFileNames(i), "xxxxx", "yyyyy")
End If
Next i



 ' Verify document paths and filenames after adding prefix and suffix------------------ (Vérifier les chemins d'accès aux documents et les noms de fichiers après avoir ajouté un préfixe et un suffixe)
ReDim pgGetFileNames(namesCount - 1)
ReDim pgDocumentStatus(namesCount - 1)
status = swPackAndGo.GetDocumentSaveToNames(pgGetFileNames, pgDocumentStatus)
Debug.Print ""
Debug.Print "  My Pack and Go path and filenames after adding prefix and suffix: "
For i = 0 To (namesCount - 1)
     Debug.Print "    My path and filename is: " & pgGetFileNames(i)
Next i
' Pack and Go
statuses = swModelDocExt.SavePackAndGo(swPackAndGo)

End Sub

 

It is not enough to paste one piece of code after another. You have to try to understand a little...

swPackAndGo.AddPrefix = "SW_"
swPackAndGo.AddSuffix = "_PackAndGo"

Adds a prefix and suffix. Since you haven't changed the values, I doubt you need it. You can delete it.

 

ReDim pgGetFileNames(namesCount - 1)
ReDim pgDocumentStatus(namesCount - 1)
status = swPackAndGo.GetDocumentSaveToNames(pgGetFileNames, pgDocumentStatus)
Debug.Print ""
Debug.Print "  My Pack and Go path and filenames after adding prefix and suffix: "
For i = 0 To (namesCount - 1)
     Debug.Print "    My path and filename is: " & pgGetFileNames(i)
Next i

This only displays the final names of the documents in the debugger. I don't think you need it.

 

Done a step-by-step test to see if the file names are changing correctly.

1 Like

Here is a piece of functional macro and clean of the displays in the debugger:

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModelDoc As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swPackAndGo As SldWorks.PackAndGo
Dim openFile As String
Dim pgFileNames As Variant
Dim pgFileStatus As Variant
Dim pgGetFileNames As Variant
Dim pgDocumentStatus As Variant
Dim status As Boolean
Dim warnings As Long
Dim errors As Long
Dim i As Long
Dim namesCount As Long
Dim myPath As String
Dim statuses As Variant

Sub main()

    Set swApp = Application.SldWorks

    ' Open assembly
    openFile = "C:\Users\DRO\Desktop\Nouveau dossier\Assemblage1.SLDASM"
    Set swModelDoc = swApp.OpenDoc6(openFile, swDocASSEMBLY, swOpenDocOptions_Silent, "", errors, warnings)
    'Set swModelDoc = swApp.ActiveDoc
    Set swModelDocExt = swModelDoc.Extension

    ' Get Pack and Go object
    Set swPackAndGo = swModelDocExt.GetPackAndGo

    ' Get number of documents in assembly
    namesCount = swPackAndGo.GetDocumentNamesCount

    ' Include any drawings, SOLIDWORKS Simulation results, and SOLIDWORKS Toolbox components
    swPackAndGo.IncludeDrawings = True
    swPackAndGo.IncludeSimulationResults = True
    swPackAndGo.IncludeToolboxComponents = True

    ' Get current paths and filenames of the assembly's documents
    'status = swPackAndGo.GetDocumentNames(pgFileNames)

    ' Get current save-to paths and filenames of the assembly's documents
    status = swPackAndGo.GetDocumentSaveToNames(pgFileNames, pgFileStatus)

    ' Set document paths and names for Pack and Go
    For i = 0 To (namesCount - 1)
        If InStr(pgFileNames(i), "XXXXX") <> 0 Then
            pgFileNames(i) = Replace(pgFileNames(i), "XXXXX", "44444")
        End If
    Next i
    status = swPackAndGo.SetDocumentSaveToNames(pgFileNames)
    
    ' Set folder where to save the files
    myPath = "C:\Users\DRO\Desktop\Temp PDF\"
    status = swPackAndGo.SetSaveToName(True, myPath)
    
    ' Flatten the Pack and Go folder structure; save all files to the root directory
    swPackAndGo.FlattenToSingleFolder = True

    ' Add a prefix and suffix to the new Pack and Go filenames
    swPackAndGo.AddPrefix = "SW_"
    swPackAndGo.AddSuffix = "_PackAndGo"

    ' Pack and Go
    statuses = swModelDocExt.SavePackAndGo(swPackAndGo)

End Sub

Be careful not to forget the line:

status = swPackAndGo.SetDocumentSaveToNames(pgFileNames)

after the loop to rename the files including "XXXXX" otherwise the PackAndGo does not take the modification into account.

Kind regards

1 Like

Hello, after trying your clean macro it doesn't change the name of the file

I'm  attaching an ASM for testing to see if it works for you

 


xxxxx.zip

Hello

after testing my macro on your assembly, it works by putting the series of Xs in lowercase (x).

Kind regards

2 Likes

I had tried several times but I hadn't thought about it. Thank you

On the other hand, it doesn't change the name of the plans that are with my assembly and my parts? Do you have an idea?

 

Hello

No, this does not change the name of the plan because the plan file is not, strictly speaking , part of the PackAndGo file list but is only embedded via a checkmark of the function. You can however in your macro plan to rename all the plans concerned, after the PackAndGo function, by the standard VBA file management functions, you will find information to do it HERE.

Kind regards

1 Like

Thank you very much, I could manage with this external management at Solidworks.

I have one last request, is it possible to exclude in the PackAndGo macro the files that do not have in the file name 'XXXXX'

Hello

Try by putting :

For i = 0 To (namesCount - 1)
    If InStr(pgFileNames(i), "xxxxx") <> 0 Then
        pgFileNames(i) = Replace(pgFileNames(i), "xxxxx", "44444")
    Else
        pgFileNames(i) = ""
    End If
Next i
status = swPackAndGo.SetDocumentSaveToNames(pgFileNames)

 

Kind regards