SolidWorks macro to add characters to the beginning of the file name to be backed up

Hello

I'm taking up the torch of macro editing at my job, but without the basics in VBA, so I'm mostly doing DIY/adaptation rather than coding for now, and after several unsuccessful searches and tests, here I am.

The problem is simple, today my PDF recording macro simply keeps the same file name as the DRW: 2263-4B.slddrw -> 2263-4B.pdf

I would like the macro to automatically add characters just before the file name when generating PDFs, as follows: 2263-4B.slddrw -> CK-2263-4B.pdf

Below is the part that concerns the recording in PDF:

-------------------------------------------------------------------------------------------

Sub main()

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim PathName As String

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

    If MsgBox("Would you like to save as PDF?", vbQuestion + vbYesNo, "PDF") = vbNo Then
        MsgBox "Cancelled", vbOKOnly, "PDF"
        Exit Sub
    End If

    PathName = Mid(swModel.GetPathName, 1, Len(swModel.GetPathName) - 7)

    swModel.SaveAs2 PathName & ".PDF", 0, True, False

End Sub

-------------------------------------------------------------------------------------------

(Lynkoa's "Insert code snippet" option was bugging my message)

I tried several basic things, like just adding "CK-" & right after PathName = , but without success. Does anyone have a solution to this little problem?

Thank you in advance.

Hello

Normally:

PathName = Mid(swModel.GetPathName, 1, Len(swModel.GetPathName) - 7)

PathName = "Ck-" & PathName

 

Thank you for your help, unfortunately this method doesn't seem to work here, no pdf is generated when I launch the macro with this addition.

Sorry, misread: 

PathName = Mid(swModel.GetPathName, 1, InStrRev(swModel.GetPathName, "\"))
FileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
FileName = Left(FileName, Len(FileName) - 7)
FileName = "CK-" & FileName
PathName = PathName & FileName
swModel.SaveAs2 PathName & ".PDF", 0, True, False


 

2 Likes

Great, it works!

Thank you again.