Macro packandgo (take-home composition)

To integrate the drawings you must add:

swPackAndGo.IncludeDrawings = True

 

See here.

1 Like

At remrem, yes this line is indeed put in the proposed macro:

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

but indeed, when renaming the files included in the PackAndGo, the name of the 2D file is not taken into account, I did not find a function to rename these plans automatically, as can be found in the "Select / Replace... " of the Pack and Go:

If you have this function via the APIs, I'm also interested in...

Kind regards

Thank you for your answers sorry for the late rest,

It works at the level of excluding rooms without "xxxxx" on the other hand it copies the plans of the rooms without "xxxxx", is it possible to remove them?

Hello

I think it's not possible, or at least I don't know the function to do it, but you can plan in your macro to retrieve the name of all these files (without forgetting the prefixes and suffixes) at the time of the pgFileNames(i) = ""then delete all the plans concerned, after the PackAndGo function, by the standard VBA file management functions.

Kind regards

Yes in the end that's what I just did.

On the other hand, when I launch the macro from solidworks, it works, but I run it from the Excel file, it doesn't work.

Do you have an idea?

Give us your final code in Excel.

Did you execute in step by step mode?

I took the SW code that I put in Excel 

 

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
    Set swModelDoc = swApp.ActiveDoc
    '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", "85000")
        Else
        pgFileNames(i) = ""
        End If
    Next i
    status = swPackAndGo.SetDocumentSaveToNames(pgFileNames)
    

    ' Set folder where to save the files
    
    myPath = "G:\Intégration\BA\QRM\enregistrement\85000\"
    status = swPackAndGo.SetSaveToName(True, myPath)
    
    ' Flatten the Pack and Go folder structure; save all files to the root directory
    swPackAndGo.FlattenToSingleFolder = True

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

End Sub

 

So you must be missing things in Tools / References... of the Excel macro editor!!

And you have to create a Solidworks object and attach yourself to it, which is done automatically via Solidworks macros but not from Excel...

How do you do that?

Ben!!!! Uh!!!! How can I put it!!! Maybe as I already told you during your question which is here, in my first answer of 24/01/2019, where there are:

Sub Macro()
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2

Set swApp = CreateObject("SldWorks.Application")
swApp.Visible = True
        
Set swModel = swApp.ActiveDoc

....

End Sub

 

Not to mention the references to put in "tools / reference .... ", in particular the one to "SLDWORKS xxxx type library".

Kind regards

Hello 

In terms of references, I have the same ones.

On the other hand, I made a call from Solidworks through the

Set swApp = Application.SldWorks

while the function that works is:

Set swApp = CreateObject("SldWorks.Application")

What is the difference between Application.SldWorks and CreateObject("SldWorks.Application")?

because I have Excel macros that work with Application.SldWorks

Thank you.

Hello

Look around here, you will find the explanations requested.

Kind regards

1 Like

Great material,

If you want to replace the path too, I could do it this way:

  For i = 0 To (namesCount - 1)
        If InStr(pgFileNames(i), "ActualName") <> 0 Then
            pgFileNames(i) = Replace(pgFileNames(i), "ActualName", "NameToReplace")
         End If
        ' If InStr(pgFileNames(i), myPath)
          pgFileNames(i) = Replace(pgFileNames(i), "ParentFolder", "ParentFolder\ChildFolder")
         Debug.Print pgFileNames(i)
         'End If
    Next i

independent of the path declared