This website uses cookies to ensure you get the best experience on our website. By using our website you agree on the following Cookie Policy, Privacy Policy, and Terms Of Use
SOLIDWORKS macro is using the title of the model (e.g. inserting the note, linking the custom property value, generating new file name for exporting).
As the result macro misbehaves (inserting extension twice) or displays the error: Run-time Error '5': Invalid procedure call or argument
The extension is extracted from the document title via IModelDoc2::GetTitle SOLIDWORKS API method.
There are several factors which affect the way title is displayed to the user:
Extension visibility in the model's title is displayed based on the windows setting 'Hide extension for known file types'.
Depending on this setting title of the model can either include or exclude extension (e.g. *Part1 *or Part1.sldprt)
For the newly created files (i.e. files which were never saved) extension is never displayed
For drawings the title is a composition of a name and the active sheet. The extension is never displayed for drawings
Resolution
Change the setting based on the macro requirement
Modify the macro code to consider both options. The example below provides two functions to get the title with or without extension regardless of the conditions.
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
IfNot swModel IsNothingThen
Debug.Print GetTitleWithoutExtension(swModel)
Debug.Print GetTitleWithExtension(swModel)
Else
MsgBox "Please open the model"EndIfEndSubFunction GetTitleWithExtension(model As SldWorks.ModelDoc2) AsStringDim title AsStringDim ext AsStringSelectCase model.GetTypeCase swDocumentTypes_e.swDocPART
ext = ".sldprt"Case swDocumentTypes_e.swDocASSEMBLY
ext = ".sldasm"Case swDocumentTypes_e.swDocDRAWING
ext = ".slddrw"EndSelectIf model.GetPathName() = ""Then
title = model.GetTitle + ext 'extension is not shown for file which is not savedElseIf IsExtensionShown() Then
title = model.GetTitle
Else
title = model.GetTitle + ext
EndIfEndIfIf model.GetType() = swDocumentTypes_e.swDocDRAWING Then
title = model.GetTitle() 'drawing extension never included into the title
title = Left(title, InStrRev(title, "-") - 2) + ext 'removing the sheet name from the drawing titleEndIf
GetTitleWithExtension = title
EndFunctionFunction GetTitleWithoutExtension(model As SldWorks.ModelDoc2) AsStringConst EXT_PATTERN = ".sldxxx"Dim title AsStringIf model.GetPathName() = ""Then
title = model.GetTitle 'extension is not shown for file which is not savedElseIf IsExtensionShown() Then
title = model.GetTitle
title = Left(title, Len(title) - Len(EXT_PATTERN))
Else
title = model.GetTitle
EndIfEndIfIf model.GetType() = swDocumentTypes_e.swDocDRAWING Then
title = Left(title, InStrRev(title, "-") - 2)
EndIf
GetTitleWithoutExtension = title
EndFunctionFunction IsExtensionShown() AsBooleanConst REG_KEY AsString = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\HideFileExt"Const UNCHECKED AsInteger = 0
Dim wshShell AsObjectSet wshShell = CreateObject("WScript.Shell")
IsExtensionShown = wshShell.RegRead(REG_KEY) = UNCHECKED
EndFunction
Notifications
Join session by SOLIDWORKS and PDM API expret Artem Taturevych at 3DEXPERIENCE World 2025 on Feb 26 at 08:30 AM CST to explore 10 essential macros for automating drawings, assemblies, custom properties, and more