I found the example of how to Pack and Go an assembly with a macro (VBA). I will want to add a prefix.
This works great, except, how do I not include my library parts? I don’t want to rename them or change the folder location. If I were doing a manual pack and go, I would uncheck my library parts and it would keep the reference back to the original file location.
I do have the advantage that this macro will only be run on a specific assembly, so I can know which files are library parts and hard code that into the macro if that helps.
Hey Carrie, if your library is in a specific location, perhaps you could do a filter to omit the items that are in that specific location? Just a thought. I know you’re speaking macro wise, but maybe that could inspire your solution. I can’t really help you programming wise, sorry.
That’s a good thought. If you get your full list of documents with IPackAndGo.GetDocumentSaveToNames and then remove the library files manually from the list based on their file directory. Then you could pass the updated array into IPackAndGo.SetDocumentSaveToNames before completing the procedure.
I haven’t used this myself yet so I’m only assuming that’s how this works based on the help files and few examples. Good Luck!
I will preface this by saying I have only a vague idea about what macros are, with absolutely no knowledge about writing one, but when I’m going a Pack and Go and want to exclude my library parts I don’t select them manually. I use the Select/Replace function, selecting “In Folder”, enter a key word that’s in the file path for my library parts, then select “Uncheck All” (thanks again gupta9665 for teaching me that). Can you get your macro to exclude all components at a certain location? (I assume they’re all in a single location.)
Currently, I’m doing everything on a network drive, so the part I’m messing with isn’t actually in the library. I just want to leave it referencing the original file/file location at this time.
I have hard coded the file names and locations at the moment as I am working through this problem.
So, I have my macro able to find the file in the list of files. I can’t figure out how to change the name for that file to be an empty string. I think that will solve this problem, but I haven’t figured out how to do that. I’m sure it’s something that someone with more experience like gupta9665 would know how to do with no problem.
I am attaching my macro and a zip file of my assembly I am using.
I am in Texas so my work hours are from about 9 - 6 Central time.
Thanks for the suggestions so far. And thanks in advance for any additional pointers. assy-and-macro.zip (2.32 MB)
I made that change and the list it prints looks OK, but I still end up with the screw being renamed as part of the pack and go. The new assembly is referencing the renamed screw.
There is something different between pgFileNames and pgGetFileNames and I’m not figuring out why they have the two different variables or why they do the ReDim on it either.
Once you have retrieved the current file names and have set the hardware item names to empty strings (as shown in my first reply), you have to call IPackAngGo::SetDocumentSaveToNames:
' Get current paths and filenames of the assembly's documents
status = swPackAndGo.GetDocumentNames(pgFileNames)
Debug.Print ""
Debug.Print " 1 Current path and filenames: "
If (Not (IsEmpty(pgFileNames))) Then
For i = 0 To UBound(pgFileNames)
'check if the library part is there and set it to empty
If pgFileNames(i) = "C:\Users\jsculley.AZZ\Desktop\SOLIDWORKS Files\assy-and-macro\96817A310_M3x6_SCREW.SLDPRT" Then
pgFileNames(i) = "" 'Set hardware item to empty string
Debug.Print "We hit the IF " & F1
' swApp.SendMsgToUser2 "in if" & F1, swMbWarning, swMbOk
End If
Debug.Print " The path and filename is: " & pgFileNames(i)
Next i
'Update pack and go file names
status = swPackAndGo.SetDocumentSaveToNames(pgFileNames)
End If
Also, this line will cause an error:
pgGetFileNames(F1) = ""
The variable pgGetFileNames is an array of strings. In this example it is a one-dimensional array, or simply a list of strings. You access the items in the list by their index, which is an integer. So, the third item in the list can be accessed like this:
Dim anItem
anItem = pgGetFileNames(3)
Your F1 variable is a string, so writing this:
pgGetFileNames(F1) = ""
won’t work because the array index is supposed to be an integer and you passed it a string. It would be like asking for the appleth item in a list
So most of the things are being done with the array pgFileNames.
then near the end, you suddenly see pgGetFileNames
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)
Why is there a redim for it?
And why did we suddenly change from pgfileNames to pgGetFileNames?
Looks like they’re just using another variable (pgGetFileNames) to gather the newly renamed file list, to display in your Immediate Window, to show that it’s working. Notice they’re only ‘getting’ the new filenames, without doing anything else with it? Not sure the ReDim is necessary