Detailed file name extraction

I made a macro to take only project number and writes it into custom properties but If I could also take part number and part name from file name that would be awesome. 

taking the project number was easy because it is always 6 digits 

sPathName = swModel.GetTitle 'Get File Name & Path
Partnumber = Left(sPathName, 6) 'take part number

Set swCustProp = swModel.Extension.CustomPropertyManager("")
swCustProp.Set "PROJECT #", Partnumber 'write part number in custom properties

I almost want to do remove Left(sPathName - Parnumber - " - ", 2)

and just remove " - " from the rest of it to pull part name but I don't know hot to do that or is it even possible to do?

below is my file name structure.

Project number - part number - part name

200471 - 01 - Tray.sldprt

200471 - 02 - Length Panel.sldprt

SolidworksApi/macros
Comments 13
DG
DG 2021-03-26

Mehmet Gultepe wrote:

I am getting an error at this line. Can't figure out the issue

 

Set cusPropMgr = config.CustomPropertyManager

 

What error are you getting?

DG
DG 2021-03-26

You can also use the split function.

AS
AS 2021-03-26

Hadn't thought of that, Deepak. Split is a much cleaner option, IMO.

MG
MG 2021-03-26

This is awesome. I am trying to reduce manual work as much as possible.  I can think the algorithm but don't know all the functions. Next project is automate this for all the .sldprt in the entire asm. 

MG
MG 2021-03-26

what is the syntax for that function? it would be great if you have an example. 

MG
MG 2021-03-26

This example will be good start for me. Thanks for the ideas.

Split function (Visual Basic for Applications) | Microsoft Docs 

Dim strFull As String
Dim arrSplitStrings1() As Variant
Dim arrSplitStrings2() As Variant
Dim strSingleString1 As String
Dim strSingleString2 As String
Dim strSingleString3 As String
Dim i As Long

strFull = "Some - Old - Hags - Can - Always - Hide - Their - Old - Age" ' String that will be used.

arrSplitStrings1 = Split(strFull, "-") ' arrSplitStrings1 will be an array from 0 To 8.
' arrSplitStrings1(0) = "Some " and arrSplitStrings1(1) = " Old ".
' The delimiter did not include spaces, so the spaces in strFull will be included in the returned array values.

arrSplitStrings2 = Split(strFull, " - ") ' arrSplitStrings2 will be an array from 0 To 8.
' arrSplitStrings2(0) = "Some" and arrSplitStrings2(1) = "Old".
' The delimiter includes the spaces, so the spaces will not be included in the returned array values.

'Multiple examples of how to return the value "Can" (array position 3).

strSingleString1 = arrSplitStrings2(3) ' strSingleString1 = "Can".

strSingleString2 = Split(strFull, " - ")(3) ' strSingleString2 = "Can".
' This syntax can be used if the entire array is not needed, and the position in the returned array for the desired value is known.

For i = LBound(arrSplitStrings2, 1) To UBound(arrSplitStrings2, 1)
If InStr(1, arrSplitStrings2(i), "Can", vbTextCompare) > 0 Then
strSingleString3 = arrSplitStrings2(i)
Exit For
End If
Next i

MG
MG 2021-03-26
Hi ,
I advanced the macro above and it is changing the custom properties in the entire folder.
Now my problem is if the custom property is not there below code doesn't work. I think I need to use Add for that but some files have the property and some don't have it.
I have seen in your one of the conversations in the forum that there is an if statement checking the property, if it is exist it uses set, if it doesn't it uses add but I can't find that topic. 
Set swCustProp = swModel.Extension.CustomPropertyManager("")
swCustProp.Set "Item #", PartNumber
swCustProp.Set "Description", PartName
-
-
-
-
-
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim sFileName As String
Dim Path As String
Dim nErrors As Long
Dim nWarnings As Long
Dim QualityNum As Integer
Dim swCustProp          As CustomPropertyManager
Dim sPathName As String
Dim PathSize As String
Dim FileName As String
Dim PartNumber As String
Dim PartName As String
Dim arrFileName As Variant
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object

Sub main()
Set swApp = Application.SldWorks
'Get active document
Set swModel = swApp.ActiveDoc
Debug.Print swModel.GetPathName
Path = Strings.Left(swModel.GetPathName, Strings.Len(swModel.GetPathName) - Strings.Len(swModel.GetTitle)) 'Remember to end with \
MsgBox (Path)
sFileName = Dir(Path & "*.sldprt")
Do Until sFileName = ""
Set swModel = swApp.OpenDoc6(Path + sFileName, swDocPART, swOpenDocOptions_Silent, "", nErrors, nWarnings)
Set swModel = swApp.ActiveDoc
swApp.Visible = False
'custom properties
sPathName = swModel.GetTitle 'Get File Name & Path
PathSize = Strings.Len(sPathName)
FileName = Strings.Left(sPathName, PathSize - 7)
arrFileName = Split(FileName, " - ")
PartNumber = arrFileName(1)
PartName = arrFileName(2)
Set swCustProp = swModel.Extension.CustomPropertyManager("")
swCustProp.Set "Item #", PartNumber
swCustProp.Set "Description", PartName
'save
swModel.Save 'Should use save3 for proper control
swApp.CloseDoc swModel.GetTitle
Set swModel = Nothing
sFileName = Dir
Loop
Set swModel = Nothing
End Sub
DG
DG 2021-03-26

You are close, look for the Add3 function in API  help files.

MG
MG 2021-03-26

swCustProp.Add3 "Item #", swCustomInfoType_e.swCustomInfoText, PartNumber, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd
swCustProp.Add3 "Description", swCustomInfoType_e.swCustomInfoText, PartName, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd

Yes I found after I sent the message. This works much better

but now I have another issue. Some people have the same custom property in configuration specific tab and It dominates in the BOM. I am looking for the code that deletes in the configuration specific (if it is exist) before goes the to above code.

DG
DG 2021-03-26

Mehmet Gultepe wrote:

but now I have another issue. Some people have the same custom property in configuration specific tab and It dominates in the BOM. I am looking for the code that deletes in the configuration specific (if it is exist) before goes the to above code.

Look into the Delete2 method.

MG
MG 2021-03-26

I am getting an error at this line. Can't figure out the issue

Set cusPropMgr = config.CustomPropertyManager

Option Explicit<br />Dim swApp As SldWorks.SldWorks<br />Dim swModel As SldWorks.ModelDoc2<br />Dim config As SldWorks.Configuration<br />Dim sFileName As String<br />Dim Path As String<br />Dim nErrors As Long<br />Dim nWarnings As Long<br />Dim QualityNum As Integer<br />Dim swCustProp          As CustomPropertyManager<br />Dim cusPropMgr         As CustomPropertyManager<br />Dim sPathName As String<br />Dim PathSize As String<br />Dim FileName As String<br />Dim PartNumber As String<br />Dim PartName As String<br />Dim arrFileName As Variant<br />Dim Part As Object<br />Dim SelMgr As Object<br />Dim boolstatus As Boolean<br />Dim longstatus As Long, longwarnings As Long<br />Dim Feature As Object<br /><br /><br />Sub main()<br />Set swApp = Application.SldWorks<br />'Get active document<br />Set swModel = swApp.ActiveDoc<br /><br />Debug.Print swModel.GetPathName<br /><br />Path = Strings.Left(swModel.GetPathName, Strings.Len(swModel.GetPathName) - Strings.Len(swModel.GetTitle)) 'Remember to end with \<br />MsgBox (Path)<br />sFileName = Dir(Path & "*.sldprt")<br /><br />Do Until sFileName = ""<br />'Open .sldprt files<br />Set swModel = swApp.OpenDoc6(Path + sFileName, swDocPART, swOpenDocOptions_Silent, "", nErrors, nWarnings)<br />Set swModel = swApp.ActiveDoc<br />swApp.Visible = False<br /><br /><br /><br /><br />'custom properties<br /><br />sPathName = swModel.GetTitle 'Get File Name & Path<br />PathSize = Strings.Len(sPathName)<br />FileName = Strings.Left(sPathName, PathSize - 7)<br />arrFileName = Split(FileName, " - ")<br />PartNumber = arrFileName(1)<br />PartName = arrFileName(2)<br /><br />Set swCustProp = swModel.Extension.CustomPropertyManager("")<br /><br />swCustProp.Add3 "Item #", swCustomInfoType_e.swCustomInfoText, PartNumber, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd<br />swCustProp.Add3 "Description", swCustomInfoType_e.swCustomInfoText, PartName, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd<br /><br />Set config = swModel.GetActiveConfiguration<br />Set cusPropMgr = config.CustomPropertyManager<br /><br />cusPropMgr.Delete2 "Item #"<br />cusPropMgr.Delete2 "Description"<br /><br />'save<br />swModel.Save 'Should use save3 for proper control<br /><br />swApp.CloseDoc swModel.GetTitle<br />Set swModel = Nothing<br />sFileName = Dir<br />Loop<br />Set swModel = Nothing<br />End Sub<br /><br /><br /><br /><br /><br />‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍<span class="line-numbers-rows">‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍</span>
MG
MG 2021-03-26

Fixed my issue. swModel SldWorks.ModelDoc2 was conflicting with active file and the file opening so I defined another SldWorks.ModelDoc2 and called it sModel. Fixed the problem.

Option Explicit<br />Dim swApp As SldWorks.SldWorks<br />Dim swModel As SldWorks.ModelDoc2<br />Dim sModel As SldWorks.ModelDoc2<br />Dim config As SldWorks.Configuration<br />Dim sFileName As String<br />Dim Path As String<br />Dim nErrors As Long<br />Dim nWarnings As Long<br />Dim QualityNum As Integer<br />Dim swCustProp As CustomPropertyManager<br />Dim cusPropMgr As SldWorks.CustomPropertyManager<br />Dim sPathName As String<br />Dim PathSize As String<br />Dim FileName As String<br />Dim PartNumber As String<br />Dim PartName As String<br />Dim arrFileName As Variant<br />Dim Part As Object<br />Dim SelMgr As Object<br />Dim boolstatus As Boolean<br />Dim longstatus As Long, longwarnings As Long<br />Dim Feature As Object<br /><br /><br />Sub main()<br />Set swApp = Application.SldWorks<br />'Get active document<br />Set swModel = swApp.ActiveDoc<br /><br />Debug.Print swModel.GetPathName<br /><br />Path = Strings.Left(swModel.GetPathName, Strings.Len(swModel.GetPathName) - Strings.Len(swModel.GetTitle)) 'Remember to end with \<br />'MsgBox (swModel.GetPathName)<br />'MsgBox (swModel.GetTitle)<br />'MsgBox (Path)<br />sFileName = Dir(Path & "*.sldprt")<br /><br />Do Until sFileName = ""<br />'Open .sldprt files<br />Set sModel = swApp.OpenDoc6(Path + sFileName, swDocPART, swOpenDocOptions_Silent, "", nErrors, nWarnings)<br />Set sModel = swApp.ActiveDoc<br />swApp.Visible = False<br /><br /><br /><br /><br />'custom properties<br /><br />sPathName = sModel.GetTitle 'Get File Name & Path<br />PathSize = Strings.Len(sPathName)<br />FileName = Strings.Left(sPathName, PathSize - 7)<br />arrFileName = Split(FileName, " - ")<br />PartNumber = arrFileName(1)<br />PartName = arrFileName(2)<br />'MsgBox (FileName)<br />'MsgBox (PartNumber)<br />'MsgBox (PartName)<br /><br />Set swCustProp = sModel.Extension.CustomPropertyManager("")<br /><br /><br />swCustProp.Add3 "Item #", swCustomInfoType_e.swCustomInfoText, PartNumber, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd<br />swCustProp.Add3 "Description", swCustomInfoType_e.swCustomInfoText, PartName, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd<br /><br />'Set config = swModel.GetActiveConfiguration<br />Set cusPropMgr = sModel.GetActiveConfiguration.CustomPropertyManager<br /><br />cusPropMgr.Delete2 "Item #"<br />cusPropMgr.Delete2 "Description"<br /><br />'save<br />sModel.Save 'Should use save3 for proper control<br /><br />swApp.CloseDoc sModel.GetTitle<br />Set sModel = Nothing<br />sFileName = Dir<br />Loop<br />Set swModel = Nothing<br />End Sub<br /><br /><br /><br /><br /><br /><span class="line-numbers-rows">‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍</span>
AS
AS 2021-03-26

Hi Mehmet,

Utilize the functions below to manipulate the path name that you need:

InStr function (Visual Basic for Applications) | Microsoft Docs 

InStrRev function (Visual Basic for Applications) | Microsoft Docs 

Left function (Visual Basic for Applications) | Microsoft Docs 

Mid function (Visual Basic for Applications) | Microsoft Docs 

Right function (Visual Basic for Applications) | Microsoft Docs 

Something like this could work. Look for the hyphen separators, and then strip the part name from its extension with the last line.

firstHyphenLocation = InStr(1, pthName, " - ") - 1
secHyphenLocation = InStrRev(pthName, " - ") - 1

projNum = Left(pthName, firstHyphenLocation)


partNum = Mid(pthName, firstHyphenLocation + 4, 2)


prtNameExtension = Right(pthName, secHyphenLocation)
prtName = Left(prtNameExtension, InStr(1, prtNameExtension, ".", 0) - 1)