How to read an excel file via the VBA SW?

Hello 

I would like to retrieve data from an Excel according to the name of my assembly and write it in the properties of it.

But as soon as I run my code, I get an error message "Library not registered" but for all that the Microsoft Excel 15.0 Object Library references is checked.

Do you have an idea? 

 

Sub OpenExcel()

Dim xlApp As Excel.Application
Set xlApp = Excel.Application
Dim wbk As Excel.Workbook
Dim sht As Excel.Worksheet

With xlApp
    . Visible = True
    . Workbooks.Open ("P:\05-Solidworks\CAD\Developer Request.xlsm")
    
End With

End Sub
 

I don't know why it doesn't work but here is a site with many working examples, including one with an xls file:

https://www.codestack.net/solidworks-api/document/tables/write-excel-table-into-general-table/

 

A 2nd site with example:

https://www.cadsharp.com/blog/using-microsoft-excel-with-the-solidworks-api/

Hopefully this will help you and then you can give us feedback.

Hello 

I've already seen these sites and tested them but nothing helps, he always tells me that I have a reference problem:( 

I found this code which is functional (sw2020 excel 16.0)

Option Explicit

 

Dim swApp As Object
Dim swModel As Object
Dim xlApp As Object

Dim xlSheet As Object
Dim xlBooks As Object
 

Sub Main()

 

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

 

Set xlApp = CreateObject("Excel.Application")

xlApp.Visible = True

xlApp.Workbooks.Open "\\Hul-fps1\fsm$\BOLSTERS.xls"

Set xlSheet = xlApp.ActiveSheet

 

End Sub

Edit:

Replacing your line:

Set XlApp=Excel.Application by:

Set xlApp = CreateObject("Excel.Application")

This opens up an excel for me.

 

aaaaah yes, it opens up excel for me too:D!  

Weird I don't understand the difference...  

Well, I'd have to make an ADODB connection to make it faster, but I'll do it later  

Thank you Denis! 

Hello

It was the CreateObject that was missing. Or else you had to code as below in order to create an Excel instance

Set xlApp = New Excel.Application

 

3 Likes