any one can help me ,How to setting a part of file name link to the file property? thank you very much.
for example , fliename is "R.125.212 Stop Part"， and i want to get "R.125.212" link to my file property.
I have created for my self a function, which you can modify to get what you want. I did add comments
At the location where you want to get the partial file name add:
Dim filename As Stringfilename = getFilename(swModel)
Then add the function below your main (e.g. at the end of your file / after the last "End Sub" or "End Function"):
Function getFilename(swModel As SldWorks.IModelDoc2) As String Dim path As String 'Retrieve file name: path = swModel.GetPathName 'Remove the path info from the file name: getFilename = Mid(path, InStrRev(path, "\") + 1) ' With extension 'Remove the extension from the filename. This trick you can also use to to remove more info. 'I use " - " (space - space) in my filename and split on this to get different parts of the info. 'That trick is not shown in this example though, but replace the "." with " - " to get that effect. getFilename = Left$(getFilename, InStrRev(getFilename, ".") - 1) ' Remove extension 'Use message boxes to get the exact result you need. 'MsgBox getFilename, vbOKOnly, "The resulting file name = " 'To get part of the filename, use the "Left", "Right" or "Mid" VBA functions. 'https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/left-function 'If you allways have the same amount of characters, use simply add it to the left function. 'If you're not sure about how many charakters you have, search for a separator (e.g. a space, or like me the " - ")End Function
Thank you Christof, but i still don't know how to setting, i not very good at Macro. or i not tell clealy, below the picture can tell specifically.
Hello. Try this:
Option ExplicitSub main() Dim swApp As SldWorks.SldWorks Dim swModel As SldWorks.ModelDoc2 Dim swCustPropMgr As SldWorks.CustomPropertyManager Dim FileName As String Set swApp = Application.SldWorks Set swModel = swApp.ActiveDoc Set swCustPropMgr = swModel.Extension.CustomPropertyManager(Empty) FileName = swModel.GetPathName FileName = Left(FileName, InStr(FileName, " ") - 1) FileName = Right(FileName, Len(FileName) - InStrRev(FileName, "\")) 'Debug.Print "Part Name : " & FileName swCustPropMgr.Add3 "PartNo", swCustomInfoType_e.swCustomInfoText, FileName, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAddEnd Sub
Thank you Fifi, it running, i see different answer you post in the solidworks forums, you are a nice guy!
Retrieving data ...