×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Get part name using VBA

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???

RE: Get part name using VBA

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

Someday, someone may kill you with your own gun, but they should have to beat you to death with it because it is empty.

RE: Get part name using VBA

(OP)
You seem to know all the answers to my questions!
Thanks a lot, TheTick.

I'll give it a try.

RE: Get part name using VBA

TheTick

     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

Lee

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

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

All this machinery making modern music can still be open hearted.

RE: Get part name using VBA

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources