Macro help to display part of filename

Library for macros
chancegarrison
Posts: 16
Joined: Wed Feb 01, 2023 5:10 pm
Answers: 0
x 1

Macro help to display part of filename

Unread post by chancegarrison »

I am looking for help to display part of a file name that is after a certain number of characters or a dash symbol.

For example, I have a part with file name 1-123456-001
I want to be able to display only the 001 from the end after the second dash on a drawing. How can this be trimmed and displayed?

This would save lots of tedious work.
User avatar
AlexB
Posts: 501
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 28
x 269
x 445

Re: Macro help to display part of filename

Unread post by AlexB »

Try this. The variable "s" is your initial string.

Code: Select all

Dim substring As String
substring = Right(s, Len(s) - InStrRev(s, "-"))
chancegarrison
Posts: 16
Joined: Wed Feb 01, 2023 5:10 pm
Answers: 0
x 1

Re: Macro help to display part of filename

Unread post by chancegarrison »

AlexB wrote: Fri Nov 22, 2024 9:12 pm Try this. The variable "s" is your initial string.

Code: Select all

Dim substring As String
substring = Right(s, Len(s) - InStrRev(s, "-"))
Alex, this would be great but I’m no coder.
User avatar
gupta9665
Posts: 413
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 435
x 452

Re: Macro help to display part of filename

Unread post by gupta9665 »

Here is another version, it can be simplified but I prefer it.

Code: Select all

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim FileName As String

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc


FileName = swModel.GetPathName
FileName = Mid(FileName, InStrRev(FileName, "\") + 1)
FileName = Left(FileName, InStrRev(FileName, ".") - 1)
FileName = Mid(FileName, InStrRev(FileName, "-") + 1)

Debug.Print FileName


End Sub
Deepak Gupta
SOLIDWORKS Consultant/Blogger
chancegarrison
Posts: 16
Joined: Wed Feb 01, 2023 5:10 pm
Answers: 0
x 1

Re: Macro help to display part of filename

Unread post by chancegarrison »

gupta9665 wrote: Sat Nov 23, 2024 8:47 am Here is another version, it can be simplified but I prefer it.

Code: Select all

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim FileName As String

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc


FileName = swModel.GetPathName
FileName = Mid(FileName, InStrRev(FileName, "\") + 1)
FileName = Left(FileName, InStrRev(FileName, ".") - 1)
FileName = Mid(FileName, InStrRev(FileName, "-") + 1)

Debug.Print FileName


End Sub

Deepak,
How is this code intended to be used? I run this but nothing happens that I can see.
chancegarrison
Posts: 16
Joined: Wed Feb 01, 2023 5:10 pm
Answers: 0
x 1

Re: Macro help to display part of filename

Unread post by chancegarrison »

chancegarrison wrote: Sun Nov 24, 2024 5:01 pm Deepak,
How is this code intended to be used? I run this but nothing happens that I can see.
I should add that ideally I could run the macro and have it plug into a custom property value (ex PRP: "End of filename") so I could run this across hundreds of drawings.
chancegarrison
Posts: 16
Joined: Wed Feb 01, 2023 5:10 pm
Answers: 0
x 1

Re: Macro help to display part of filename

Unread post by chancegarrison »

gupta9665 wrote: Sat Nov 23, 2024 8:47 am Here is another version, it can be simplified but I prefer it.

Code: Select all

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim FileName As String

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc


FileName = swModel.GetPathName
FileName = Mid(FileName, InStrRev(FileName, "\") + 1)
FileName = Left(FileName, InStrRev(FileName, ".") - 1)
FileName = Mid(FileName, InStrRev(FileName, "-") + 1)

Debug.Print FileName


End Sub
Deepak, based on this post and your help from the past I was able to cobble something together that works on Solidworks Parts, but I would like it to work on the drawing files as well.
Also, it would be a bonus for this macro to automatically run when saving the document.

Code: Select all

Option Explicit

Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swCustPropMgr As SldWorks.CustomPropertyManager
Dim bRet As Boolean
Dim PropValue As String

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swCustPropMgr = swModel.Extension.CustomPropertyManager("Default")

If Not swModel Is Nothing And swModel.GetPathName <> "" Then
PropValue = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
PropValue = Left(PropValue, InStrRev(PropValue, ".") - 1)
PropValue = Mid(PropValue, InStrRev(PropValue, "-") + 1)
bRet = swCustPropMgr.Add3("DETAIL #", 30, PropValue, 1)
End If
swModel.Save
End Sub
User avatar
gupta9665
Posts: 413
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 435
x 452

Re: Macro help to display part of filename

Unread post by gupta9665 »

Replace this line

Set swCustPropMgr = swModel.Extension.CustomPropertyManager("Default")

with

If swModel.GetType = 3 Then
Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")
Else
Set swCustPropMgr = swModel.Extension.CustomPropertyManager("Default")
End If

And refer the macro example from @artem here https://www.codestack.net/solidworks-ap ... ment-save/
Deepak Gupta
SOLIDWORKS Consultant/Blogger
Post Reply