I believe I've discovered a bug or unintended behavior. The API is failing despite returning a SUCCESS code.
It has to do with reloading the drawing template while the drawing document is invisible.
The new template is saved (in Sheet properties) but it is not successfully reloaded, visually despite a SUCCESS code. If you re-open the drawing and manually click "RELOAD" -> Apply Changes then it updates visually. But not via API.
Making the document visible (while to macro runs) make the code run correctly.
Also during testing, it was creating some general instability and crashing SolidWorks, I believe this is due to the bug leaving SolidWorks in an unknown state.
Sub FixDrawingTemplate(filepath As String)
swApp.DocumentVisible False, swDocDRAWING 'this will break this function. DO NOT ENABLE
Dim fso As New Scripting.FileSystemObject
Dim filename As String
filename = fso.GetFileName(filepath)
Dim extension As String
extension = UCase(fso.GetExtensionName(filepath))
Dim partnum As String
partnum = fso.GetBaseName(filepath)
Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.OpenDoc6(filepath, swDocDRAWING, swOpenDocOptions_Silent, "", fileError, fileWarning)
Dim swDrawingDoc As DrawingDoc
Set swDrawingDoc = swModel
If Not (swDrawingDoc Is Nothing) Then
Dim swSheet As sheet
Set swSheet = swDrawingDoc.GetCurrentSheet()
Dim newTemplate As String
newTemplate = "C:\\newtemplate.slddrt"
Call swSheet.SetTemplateName(newTemplate)
Dim result As swReloadTemplateResult_e
result = swSheet.ReloadTemplate(False)
Debug.Print "Reload sheet format for <" & swSheet.GetName & ">: " & GetReloadResult(result)
Debug.Print swSheet.GetTemplateName
Call swModel.Save
swApp.CloseDoc swModel.GetTitle
End If
swApp.DocumentVisible True, swDocDRAWING
End Sub
Private Function GetReloadResult(ByVal result As swReloadTemplateResult_e) As String
Select Case result
Case swReloadTemplate_Success
GetReloadResult = "Success"
Case swReloadTemplate_UnknownError
GetReloadResult = "FAIL - Unknown Error"
Case swReloadTemplate_FileNotFound
GetReloadResult = "FAIL - File Not Found"
Case swReloadTemplate_CustomSheet
GetReloadResult = "FAIL - Custom Sheet"
Case swReloadTemplate_ViewOnly
GetReloadResult = "FAIL - View Only"
Case Else
GetReloadResult = "FAIL - "
End Select
End Function 