Hello
I was looking for a macro to do the equivalent of the [Alt + TAB] action (change active document) and I found this code:
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim vModels As Variant
Dim count As Integer
Dim opendocs As New Collection
Dim docname As Variant
Dim currentdocTitle As String
Dim title As String
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
currentdocTitle = swModel.GetTitle
vModels = swApp.GetDocuments
For count = LBound(vModels) To UBound(vModels) 'get list of open documents
Set swModel = vModels(count)
If swModel.Visible <> False Then
opendocs.Add (swModel.GetTitle)
End If
Next
count = 0
If opendocs(opendocs.count) = currentdocTitle Then ' if its the last document in the list, switch to the first document
swApp.ActivateDoc2 opendocs(1), False, 0
End
Else ' else find the next document and activate
For count = 1 To opendocs.count
If opendocs(count) = currentdocTitle Then
swApp.ActivateDoc2 opendocs(count + 1), False, 0
End
End If
Next
End If
End Sub
and it works perfectly when the launches this macro directly via SolidWorks.
My problem is that I am creating a tool that launches via Excel and therefore has to write all my macros in VBA Excel and not VBA Solidworks.
when I copy/paste this code into my Excel macro, here is the error message I get:
run-time error '438'
Object doesn't support this property or method
If I click on debug, it highlights this line for me
Set swApp = Application.SldWorks
I've tried several things but I don't have much of an idea anymore...
On previous macros I have this code:
Set swApp = CreateObject("SldWorks.application")
So I tried to do the same for this one but it doesn't work I get this error message:
Run-time error'13':
Tpe mismatch
pointing to this line:
swApp.ActivateDoc2 opendocs(1), False, 0
on VBA Excel I have the references SldWorks 2017 Type Library, SOLIDWORKS 2017 Commends type library and SOLIDWORKS 2017 Constant type library enabled.
Does anyone have any ideas?
Thanks in advance
Yves