VBA Excel to duplicate a part and its plan + renaming

Hello

I want to open a room and its plan from an Excel file, save this room under a new name 'save as' and for the plan to be up to date in terms of reference before saving it in turn.

I can open my files simultaneously, and save them under a new name. But the shot always points to the other room.

However, in manual it works. It looks like the record is not saving Under, but rather saving a copy.

I open with this method:

Set swModel = swApp.OpenDoc6("link to my plan", 3, 0, "", myError, myWarning)  

And I record with this:

 swModel.SaveAs2 "new link", 0, True, False

 

PS: And by the way, if someone has a trick to change the Part Designation property at the same time, it would help me a lot.

Here's an example I use to create a copy of the part/assembly and the  linked plane from an open part or assembly, to add a hint to it.

To do this, I retrieve the index property of the open part/assembly and modify another one, the index property of the new part/assembly

With a little modification you should be able to modify all this to do what you need.

And you should understand quite well it's my 1st macro quite important so I put a lot of comments!

On the other hand, it is launched from solidworks. It's up to you to adapt it!

 


addingindex.swp
1 Like

Hello

It's all in the sbadenis macro (so +1 for it), especially the ReplaceReferencedDocument function. For example:

Option Explicit

Const sReferencingDoc As String = "C:\Users\XXX\Desktop\Temp\Pièce5.SLDDRW"
Const sOldDoc As String = "C:\Users\XXX\Desktop\Temp\Pièce5.sldprt"
Const sNewDoc As String = "C:\Users\XXX\Desktop\Temp\Pièce1.sldprt"

Sub main()

    Dim swApp As SldWorks.SldWorks
    Dim bRet As Boolean

    Set swApp = CreateObject("SldWorks.Application")

    bRet = swApp.ReplaceReferencedDocument(sReferencingDoc, sOldDoc, sNewDoc)
    
End Sub

To be used without opening any of the documents in Solidworks!!

In your line "swModel.SaveAs2 "new link", 0, True, False", the "True" means "SaveAsCopy" = yes ... but you don't need this line anymore if you use the ReplaceReferenceDocument function...

Kind regards

1 Like

Thank you for your answers, in fact replacing the True with False was enough to meet my need.

 

FYI I code from Excel and I have the impression that some things don't work the same. Like the lines with bRet, I've already seen some with the change of ownership, but it doesn't work on Excel, while on Solid yes.

Hello

I still have one flaw to correct. Let me explain:

I have an assembly "A" that contains an assembly "B"

"A" and "B" contain a "C" part.

If I replace part C with a part D in assembly A, the part in assembly B will also be replaced (that's the goal).

I finish by saving my assembly A. But I note that at the same time, assembly B was registered.

But I don't want to do that. However I put 0 as a backup option, But I notice that we should theoretically indicate to save the referents with swSaveAsOptions_SaveReferenced, but how to force it not to save them?

Hello

I don't really see the point of replacing your part C with a part D in your assembly B if you don't want to save this assembly B, you might as well replace part C with your part D only in your assembly A and keep your assembly B with your part C ...

Kind regards

In fact, this is not exactly what is happening, but I wanted to explain the problem more simply.

Basically, I want to be able to save only my assembly, and not the kids.

Hello

The SaveAs2 function meets your needs but you shouldn't put 0 in the save options, it's not a valid option, you have to add up the choices made, for example:

1 + 2 for swSaveAsOptions_Silent and swSaveAsOptions_Copy

1 + 512 for swSaveAsOptions_Silent and swSaveAsOptions_CopyAndOpen

...

Kind regards