Hello From solidworks I launch a macro with a USF which should display values from an Excel workbook when opening it
This code works in Excel retrieves cell H2 from the tab ' List '
sub Excel_test()
Url = "C:\Users\xx\Documents\SYSTEL\02-PROG\Macros\"
Fichier_Excel = "PART_Step.xlsm"
Cells(1, 10) = ExecuteExcel4Macro("'" & Url & "[" & Fichier_Excel & "]Liste'!" & Range("H2").Address(, , xlR1C1)) 'URL entrée
end sub
since Solidworks I have this error Runtime error ‹ 1004 ›: The ' Range ' method of the object ' _Global ' failed
Private Sub UserForm_Initialize()
Dim swApp As SldWorks.SldWorks
Set swApp = Application.SldWorks
Url = "C:\Users\xx\Documents\SYSTEL\02-PROG\Macros\"
Fichier_Excel = "PART_Step.xlsm"
label3= ExecuteExcel4Macro("'" & Url & "[" & Fichier_Excel & "]Liste'!" & Range("H2").Address(, , xlR1C1)) 'URL entrée
end sub
if anyone has an idea
Maclane
September 4, 2024, 9:03am
2
Bonjour;
Ne manquerait-il pas les déclaration/references pour Excel ?
`Dim xlApp As Excel.Application
Set xlApp = New Excel.Application`
Voir (pour plus d’informations):
Given the ubiquity of Microsoft Excel in engineering departments worldwide, it is no surprise that our “Using Microsoft Excel with the SolidWorks API” tutorial, available to Premium members, is one of our most popular. While I am not going to cover...
Est. reading time: 7 minutes
3 Likes
I added
Dim xlApp As Excel.Application Set xlApp = New Excel.Application'
it doesn't work either
Cyril_f
September 4, 2024, 12:08pm
4
Hello
Unless I'm mistaken, in VBA, it is not possible to access the content of an Excel file without opening it. If it's for some kind of " database " to feed a drop-down list for example, the easiest way is to write it in a text file and read it.
1 Like
I just tested it's possible on Excel, I can't find the match with Solidworks
sub Excel_test()
Url = "C:\Users\xx\Documents\SYSTEL\02-PROG\Macros\"
Fichier_Excel = "PART_Step.xlsm"
Cells(1, 10) = ExecuteExcel4Macro("'" & Url & "[" & Fichier_Excel & "]Liste'!" & Range("H2").Address(, , xlR1C1)) 'URL entrée
end sub
Maclane
September 4, 2024, 12:49pm
6
et en remplaçant
Cells(1, 10) = ExecuteExcel4Macro(.....
par
Set xlapp = CreateObject("Excel.application")
Label13 = xlapp.ExecuteExcel4Macro(.....
des pistes relativement identiques ici:
excel, vba, excel-4.0
1 Like
Cyril_f
September 4, 2024, 1:16pm
7
I just tested, so it does work on Excel but on SW not (error on the range line). At first glance I think it's a compatibility problem between SW which is in 64bit and Office in 32bit.
Maclane
September 4, 2024, 1:37pm
8
essayez avec
GetObject(“Excel.Application”)
à la place de
CreateObject(“Excel.Application”)
… il faut aussi penser à « tuer » tous les processus excel residuels:
Shell “TASKKILL /F /IM Excel.exe”, vbHide
a voir aussi:
Fixing the error in the macro which connects to database (e.g. SQL, MySql, Oracle, MS Access etc.) or any other data source and doesn't read/write values or displays error
mais aussi:
Merci Yves.T ,
Il me maquait la référence "Microsoft Excel XX.X Object library".
Comment fait-on pour savoir quelles références sont nécessaires pour quel code ??
Sinon, pour alimenter ma Combobox j'ai utilisé le code suivant :
Private Sub UserForm_Initialize()
Dim i As Byte
Dim e As String
i = 1
Do
ComboBox1.AddItem ExecuteExcel4Macro("'C:\Documents and Settings\mimi\dossier\excel\[ClasseurBase.xlsx]Feuil1'!R" & i & "C1")
i = i + 1
e = ExecuteExcel4Macro("'C:\Documents and …
Cyril_f
September 4, 2024, 2:16pm
9
It's not so much the ExecuteExcel4Macro line that crashes but the call to Range.
The error message is as follows, as quoted The 'Range' method of object '_Global' failed
Cyril_f
September 5, 2024, 7:43am
11
Hello
The range method is not supported via the version of vba integrated into SW, so there are two solutions left (not tested the second one at this stage):
Create a loop with a function to retrieve each desired column/row, see ExecuteExcel4Macro - Microsoft Community
Try ADODB-like functions. Connection, see Read and Write in Closed Excel Workbooks (developpez.com)
So I only tested the first one that works properly.
2 Likes
can you put the appropriate macro, I can't do it with solution 1
Cyril_f
September 5, 2024, 9:29am
13
Can you just tell me on which range you want to retrieve the values? (type A1:C2 for example)
here is my original code that doesn't work, H2
Private Sub UserForm_Initialize()
Dim swApp As SldWorks.SldWorks
Set swApp = Application.SldWorks
Url = "C:\Users\xx\Documents\SYSTEL\02-PROG\Macros\"
Fichier_Excel = "PART_Step.xlsm"
label3= ExecuteExcel4Macro("'" & Url & "[" & Fichier_Excel & "]Liste'!" & Range("H2").Address(, , xlR1C1)) 'URL entrée
end sub
Cyril_f
September 5, 2024, 9:45am
15
To fully understand, do you just want the H2 cell?
Cyril_f
September 5, 2024, 10:38am
17
In this case, much simpler:
label3= ExecuteExcel4Macro("'" & Url & "[" & Fichier_Excel & "]Liste'!" & "R2C8") 'URL entrée
1 Like
Great, it works
Here's another example for those who are interested
Label2 = ExecuteExcel4Macro("'" & url & "[" & Fichier_Excel & "]Liste'!" & "R11C8") & "\" & _
ExecuteExcel4Macro("'" & url & "[" & Fichier_Excel & "]Liste'!" & "R11C7") & _
"-STEP\" 'URL sortie
2 Likes
Maclane
September 5, 2024, 12:53pm
19
It's still strange that the "ExecuteExcel4Macro" function accepts table coordinates such as " Row "+" Column " but not the "Range... The joys of computing and its mysteries...
1 Like
Cyril_f
September 5, 2024, 1:00pm
20
As I said, I think that the SW API is on 64 bit functions where in Excel it's 32. If Excel was in 64 I think there would be no problem. I have the case of some macros that can only be launched from SW and are in error if I launch them from Excel
2 Likes