Sorting excel range through iLogic

Sorting excel range through iLogic

Anonymous
Not applicable
1,255 Views
7 Replies
Message 1 of 8

Sorting excel range through iLogic

Anonymous
Not applicable

Hello everyone,

 

How can I sort a range in Excel through iLogic?

 

When I try the following code:

Sub Main()
	Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oBOM As BOM = oDoc.ComponentDefinition.BOM
	Dim objExcel = CreateObject("Excel.Application")
	ThisBOM.Export("Parts Only", "C:\Users\ben.wouters\Desktop\TEST.xlsx", kMicrosoftExcelFormat)
	Dim objWorkbook = objExcel.Workbooks.Open("C:\Users\ben.wouters\Desktop\TEST.xlsx")
	excelSheet = objWorkbook.Worksheets(1)
	objExcel.Columns.AutoFit
	excelSheet.Range("A1:I16").Sort(Key1:=excelSheet.Range("C1"), Order1:=xlAscending, Header:=xlYes)
	objExcel.Application.Visible = True
End Sub

 

I get an error message saying "Method Sort of class Range has failed".

However when I run the rule with the sort line commented out, it runs just fine. It just doesn't get sorted.

I'v tried a bunch of stuff, but either it throws up the error message above or I get the following: "Exception from HRESULT: 0x800A03EC".

 

Any help would be greatly appreciated.

0 Likes
1,256 Views
7 Replies
Replies (7)
Message 2 of 8

bradeneuropeArthur
Mentor
Mentor
Sub Main()
	Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oBOM As BOM = oDoc.ComponentDefinition.BOM
	Dim objExcel = CreateObject("Excel.Application")
	ThisBOM.Export("Parts Only", "C:\Users\ben.wouters\Desktop\TEST.xlsx", kMicrosoftExcelFormat)
	Dim objWorkbook = objExcel.Workbooks.Open("C:\Users\ben.wouters\Desktop\TEST.xlsx")
	excelSheet = objWorkbook.Worksheets(1)
	objExcel.Columns.AutoFit
	excelSheet.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1:I16"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
	objExcel.Application.Visible = True
End Sub

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 8

Anonymous
Not applicable

Hey Arthur,

 

Thanks for your reply. Your code doesn't work for me.

Let me start by mentioning that I'm working with Inventor Professional 2016.

When I copy and paste your code, it doesn't run saying that Methodarguments must be between brackets and that Range isn't declared.

So I tried the following:

	excelSheet.Worksheets("Sheet1").Sort.SortFields.Add (Key:=excelSheet.Range("A1:I16"), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal)

But that gives me the error that public member Worksheets for type Worksheet wasn't found.

Replacing the word excelSheet with objWorkbook gives me the error: Exception from HRESULT: 0x800A03EC.

 

 

0 Likes
Message 4 of 8

bradeneuropeArthur
Mentor
Mentor

Try this:

 

excelSheet.Worksheets("Sheet1").Sort.SortFields.Add (Key:=excelSheet.Range("A1:A16"), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal)

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 5 of 8

Anonymous
Not applicable

Hey Arthur,

 

I get the public member Worksheets for type Worksheet wasn't found error again.

0 Likes
Message 6 of 8

bradeneuropeArthur
Mentor
Mentor

maybe this will help you:

 

https://stackoverflow.com/questions/21451458/vba-excel-sort-range-by-specific-column

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 7 of 8

Anonymous
Not applicable

Hey Arthur,

 

Thanks for the help. I managed to do what I wanted to do by using the FindRow snippet in Inventors iLogic editor.

0 Likes
Message 8 of 8

Peterjan.Schakel
Explorer
Explorer

Hello Wouteb,

 

I am currently traying the same thing, Sorting data of excel trough iLogic. I have tried every code described above, with no positive outcome (no error, just nothing happens). Is it possible that you share the code you used?

 

greets,

 

Peterjan Schakel

0 Likes