Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations MintJulep on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get part name using VBA

Status
Not open for further replies.

Hacaro

Industrial
Aug 14, 2002
10
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???
 
Replies continue below

Recommended for you

There is ModelDoc2::GetTitle. It gets the part title in the window.

[bat]Someday, someone may kill you with your own gun, but they should have to beat you to death with it because it is empty.[bat]
 
You seem to know all the answers to my questions!
Thanks a lot, TheTick.

I'll give it a try.
 
TheTick
030203usf_prv.gif


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

Random_Shapes_Pointed_shapes_prv.gif
Lee
Random_Shapes_Pointed_shapes_prv.gif


The best leaders inspire by example. When that is not an option, brute force and intimidation works pretty well, too.
 
Thanks, StarrRider.

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".

[bat]All this machinery making modern music can still be open hearted.[bat]
 
A shorter method for this:
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

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor