Get part name using VBA
Get part name using VBA
(OP)
I'm trying to insert a custom property that contains the weight of a part using a vba macro.
In order to do this I need the partname to insert "weight@partname.sldprt" in the property field of that property.
I can do this using GETPATHNAME and then filtering out the partname using the VB 'split' function.
BUT this does NOT work if the part isn't saved!!!
A new part seem to return NOTHING if GETPATHNAME is used, although it has been given a standard name in SolidWorks(for ex. part2).
Is there a way to get the standard name of a part that is not yet saved???
In order to do this I need the partname to insert "weight@partname.sldprt" in the property field of that property.
I can do this using GETPATHNAME and then filtering out the partname using the VB 'split' function.
BUT this does NOT work if the part isn't saved!!!
A new part seem to return NOTHING if GETPATHNAME is used, although it has been given a standard name in SolidWorks(for ex. part2).
Is there a way to get the standard name of a part that is not yet saved???






RE: Get part name using VBA
RE: Get part name using VBA
Thanks a lot, TheTick.
I'll give it a try.
RE: Get part name using VBA
You should be aware that ModelDoc2::GetTitle has problems. What it returns is the string that SW places in the window and that can vary from machine to machine. The value it returns is dependent on the Windows Explorer Folder Option “Hide extensions for known file types” check box. If Unchecked it will return “ABC.SldPrt” for a part - If checked it returns just “ABC”. Further – in a drawing it returns Filename–Sheetname.
A better solution is to use GetPathName and extract what you need. As an example: if the active file is stored at "C:\Folder1\Folder2\SolidPart.SLDPRT" then
FName = Part.GetPathName ' will return "C:\Folder1\Folder2\SolidPart.SLDPRT"
MyPath = FName
FName = Dir(FName) ' will contain "SolidPart.SLDPRT"
MyPath = Left(MyPath, InStr(MyPath, FName) - 1) ' will contain "C:\Folder1\Folder2\"
MyExt = Right(FName, 6) ' will contain "SLDPRT"
FName = Left(FName, InStr(FName, ".") - 1) ' will contain "SolidPart"
Naturally - you can omit as much of this as you need
The best leaders inspire by example. When that is not an option, brute force and intimidation works pretty well, too.
RE: Get part name using VBA
Seems like 1/3 of programming revolves around data validation for cases like this.
to Hacaro:
One thing I should have passed along. The value you get for weight (or any mass properties) is dependent on the units settings in the options of the measuring tool (click the "options" button when you go to check mass properties). The units are not affected by what goes on undet "tools-->Options-->Document Options".
RE: Get part name using VBA
FName = Part.GetPathName
MyPath = FName
FName = Dir(FName)
MyPath = Left(MyPath, InStr(MyPath, FName) - 1)
MyExt = Right(FName, 6)
FName = Left(FName, InStr(FName, ".") - 1)
is:
FName = Replace(UCase(Dir(Part.GetPathName)),".SLDPRT","")
Which does the same thing but saves a few steps and variables...
The only thing is that the Name is Converted to UPPER CASE
If you know the exact case of the extension you could use:
FName = Replace(Dir(Part.GetPathName),".sldprt","")
without the UCase but if the ext. is SldPrt and you search for sldprt, it will not replace it...
You could also make it a little longer and say:
FName = Replace(Dir(Part.GetPathName),Right(Part.GetPathName,7),"")
Or:
FName = Part.GetPathName
FName =Replace(Dir(FName),Right(FName,7),"")
(which will probably work best for all types of SW documents)
StarrRider's method will not work for us because we sometimes use numbers like:
123.12345.SldPrt
and FName = Left(FName, InStr(FName, ".") - 1)
would return "123" instead of "123.12345"
Good Luck,
Josh
When dealing with computers, there are 10 things you need to know: one, zero, and the rest is Binary.
-Josh S