Reputation: 143
The key steps I would like to achieve:
This API help page shows the usage for renaming the doc, and under the "Remarks" heading, includes links to the next two steps. https://help.solidworks.com/2020/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IModelDocExtension~RenameDocument.html?verRedirect=1
Usually I get by with the 'record' function then tidy things up but undertaking the steps above manually does not result in anything being recorded.
Assuming I am able to pass in the item to be renamed (I will define a variable at the start of the Sub for this e.g. swModel = swApp.ActiveDoc
), and the new name (NewName = "NEW NAME HERE"
), how would I translate the Help API into a Sub?
Two of them suggest declaring as a Function, and one as a Public Interface - I've never used these before - do these run in a standard Module? Do I need to write a 'master Sub' to call the different functions sequentially, or could these be included directly in the sub, if they are only to be used once?
Upvotes: 0
Views: 626
Reputation: 395
The "record" function is sometimes a good point to start but there are a lot of functions it can't recognize while you execute them manually.
The API Help is then useful to find out how to use a specific function. In almost every example the use of a specific method (e.g. RenameDocument) is only shown abstract. There is always a instance variable which shows you the object-type needed to call this method. So you can use these in every sub you want, but beforehand need access to the specific instance objects.
For your example the RenameDocument method is called with an object of the type IModelDocExtension. First thing for you to do is to get this object and then you can call the method as described in the help article. Under Remarks in the article you find additional information for what you maybe have to do before or after calling a method. For your example it is mentioned that the renaming takes permanently place after saving the document.
And finally here is what you want to do with some VBA code:
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Sub main()
' get the solidworks application object
Set swApp = Application.SldWorks
'get the current opened document object
Set swModel = swApp.ActiveDoc
' get the modeldocextension object
Dim swModelExtension As ModelDocExtension
Set swModelExtension = swModel.Extension
Dim lRet As Long
lRet = swModelExtension.RenameDocument("NEW NAME")
If lRet = swRenameDocumentError_e.swRenameDocumentError_None Then
MsgBox "success renaming"
Else
MsgBox "failed with error: " & lRet
End If
End Sub
Afterwars you have to process the return value to check for errors described in this article: https://help.solidworks.com/2020/English/api/swconst/SolidWorks.Interop.swconst~SolidWorks.Interop.swconst.swRenameDocumentError_e.html
Upvotes: 1