Page 1 of 1
Macro help to display part of filename
Posted: Fri Nov 22, 2024 6:38 pm
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.
Re: Macro help to display part of filename
Posted: Fri Nov 22, 2024 9:12 pm
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, "-"))
Re: Macro help to display part of filename
Posted: Fri Nov 22, 2024 9:57 pm
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.
Re: Macro help to display part of filename
Posted: Sat Nov 23, 2024 8:47 am
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