-2

That's basically my question. Doing googling didn't return anything that I am looking for, but basically I am running SOLIDWORKS from Excel and for that I need "sldworks 2016 Type Library" and "SOLDIWORKS 2016 Constant Type Library" to be enabled. Of course you'd say to do it manually, BUT my program is being run both, by people with and without Solidworks installed and if a user doesn't have SOLDIWORKS on their PC - the entire thing won't even run. So I am looking to enable and disable those two type libraries upon necessity in the code.

Could, someone, please help me?

P.S. I am not looking for any workarounds etc.

Eduards
  • 68
  • 2
  • 20
  • 2
    Late binding is a better option, IMO. – Rory Jul 07 '21 at 10:50
  • Thanks for the comment - What's that? – Eduards Jul 07 '21 at 10:54
  • Declare the SolidWorks objects as `Object`, use `CreateObject` for anything you would have used `New` for, and declare any constants you use from the referenced library. Then you don't need the reference at all. – Rory Jul 07 '21 at 10:57
  • I am not using no "New" but I am using Createobject and getobject and seems like type libraries are mandatory – Eduards Jul 07 '21 at 10:59
  • 1
    Well is [THIS](https://stackoverflow.com/questions/9879825/how-to-add-a-reference-programmatically) what you are looking for. However I agree with @Rory. Use late binding. – Siddharth Rout Jul 07 '21 at 10:59
  • Possibly, but how would I remove (uncheck it)? – Eduards Jul 07 '21 at 11:03
  • "seems like type libraries are mandatory" - on what basis do you say that? – Rory Jul 07 '21 at 11:07
  • On my assumption. I just need to find a way to programmatically do exactly what's said in the OP. To simulate manual checking the checkboxes next to the mentioned type libraries on demand when I need them in my code and to uncheck them when I don't need them – Eduards Jul 07 '21 at 11:09
  • The point is that with late binding, they are not required at all. – Rory Jul 07 '21 at 11:10
  • I am already doing "Dim swApp As Object" – Eduards Jul 07 '21 at 11:12
  • Then what do you need the reference for? – Rory Jul 07 '21 at 11:14
  • I already explained it in the OP, mate. It seems that It's mandatory and I am looking for a way to do exactly what's asked, and not any way or work around. – Eduards Jul 07 '21 at 11:16
  • 2
    "it seems that it's mandatory" is not an explanation. – Rory Jul 07 '21 at 11:18
  • Likewise, mate! I tried your suggestion and the code for Solidworks wouldn't work to it's fullest, for example it doesn't open teh drawing files no more. VBA editing code is totally fine for me as it's a thing that needs to run once and that's it. Next time user starts from a fresh workbook. If anything I could delete the whole macro as a final function and it'd be fine. I kinda just need to get past the compiling on a machine that doesn't have Solidworks – Eduards Jul 07 '21 at 15:00

1 Answers1

1

BUT my program is being run both, by people with and without Solidworks installed and if a user doesn't have SOLDIWORKS on their PC - the entire thing won't even run.

Is this what you are trying? The below code will first try to bind with an open instance of SOLIDWORKS. If it is not open, then it will try to create a new instance. Obviously if SOLIDWORKS is not installed then the CreateObject will fail but the code will not crash because of On Error Resume Next. Finally check if objSolid is not nothing. This is late binding and you do not have to set any references.

Dim objSolid As Object
    
'~~> Establish an SOLIDWORKS application object
On Error Resume Next
Set objSolid = GetObject(, "SldWorks.Application")

'~~> If not found then create new instance
If Err.Number <> 0 Then Set objSolid = CreateObject("SldWorks.Application")
Err.Clear
On Error GoTo 0

If objSolid Is Nothing Then
    MsgBox "SOLIDWORKS not installed"
    Exit Sub
End If

'
'~~> Rest of your code
'

EDIT

You cannot say Solidworks is not properly documented without putting in the right efforts to search. It took me less than 30 seconds to find this SOLIDWORKS Example of Late Binding. Of course their code will fail if the user doesn't have SOLIDWORKS and that is because they have not done proper error handling. My answer above does that for you.

Their website has all the information that you need. You just need to put in the right efforts to search. As I mentioned in the chat below, when you convert the code into late binding, you will have to search for the value of those constants. No one will give them to you in a platter. :) You can either search Google with swDocPART Constant value or as @FunThomas pointed out, type ?swDocPART in Immediate Window to get the value when the reference to SOLIDWORKS has been established.

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • Looks like it this code just goes around running the application BUT the problem is that the entire code (all modules, userforms and all) won't run or compile or however you call it, if there's no solidworks installed and the fix is to uncheck the type libraries that reference Solidworks – Eduards Jul 07 '21 at 11:18
  • No no... You got it wrong. You do need to set reference to solidworks in the first place :) – Siddharth Rout Jul 07 '21 at 11:19
  • I was editing when you posted the comment. Refresh the page to see the edit. – Siddharth Rout Jul 07 '21 at 11:20
  • Still looks liek that's not it just by the look of it, the entire macro thing won't even run. That solidworks subroutine is buried deep down in modules and subroutines, but the very initial thing won't run, because I figure that it still goes through the entire code and says that user doesn't have everything required. The error appears WAAAAAAY prior to running the actual sub that has to do with Solidworks in teh whole another module and sub – Eduards Jul 07 '21 at 11:23
  • Ok let me ask you a question. Who is setting the reference? You or someone else? – Siddharth Rout Jul 07 '21 at 11:23
  • The user that has Solidworks currently doing it by hand "Tools/References" each time and that's is something that must not be done by hand – Eduards Jul 07 '21 at 11:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/234614/discussion-between-siddharth-rout-and-eduards). – Siddharth Rout Jul 07 '21 at 11:25