Trying to Reference Multiple Excel sheets into idw's

Trying to Reference Multiple Excel sheets into idw's

emilda.chiuye
Enthusiast Enthusiast
2,166 Views
26 Replies
Message 1 of 27

Trying to Reference Multiple Excel sheets into idw's

emilda.chiuye
Enthusiast
Enthusiast

Hello.

I am able to simply reference an excel table into my idw's with no problem. HOWEVER....inventor seems to read the first sheet of the excel spreadsheet only, unless ofcourse I am wrong.

I have multiple idw's that must read from the same excel spreadsheet which has multiple sheets eg .idw 1 should read from Sheet 1 of the Superheater Template. 

.idw 2 should read from Sheet2 of the Superheater Template spreadsheet etc. 

Is there a rule that permits this?

I know that I can reference different cells in the first sheet of the spreadsheet but for this exercise it will prove to very confusing as each sheet is quite involving as it is.

Currently I have no rules, just making reference to an excel table, with limited functionality it seems.

emildachiuye_1-1635693404060.png

 

emildachiuye_0-1635692694112.png

Please help. 

Thank you

0 Likes
Accepted solutions (4)
2,167 Views
26 Replies
Replies (26)
Message 21 of 27

emilda.chiuye
Enthusiast
Enthusiast

Hello @theo.bot 

Hope you are well.

I have received a table that has values in every column and naturally the code you so kindly provided is suited for a table that contains blank columns. In the case where all column headers are filled could you perhaps tweak the current code to suite a table where all cells have a value?

Please and Thank You

 

emildachiuye_0-1636295972267.png

 

0 Likes
Message 22 of 27

theo.bot
Collaborator
Collaborator

@emilda.chiuye ,

 

I don't think the code needs an update, because it simply reads the cells from excel. Only if a cell is empty, we fill the string with a empty string value. When you have a table where we don't provide titles for the column, Inventor shows the default column names. So simple use the exsisting code and it should work.

0 Likes
Message 23 of 27

emilda.chiuye
Enthusiast
Enthusiast

 @theo.bot thanks for the response. 

Well before sending the message yesterday I realised  I was only getting 4 columns instead of the required number of columns I needed when I ran the code on a different table. 

I have attached the spreadsheet and the code you provided so that you have better insight to what I am stumbling across.

Thank you

 

Sub main
	
Dim oDrawDoc As DrawingDocument
 oDrawDoc = ThisApplication.ActiveDocument

Dim oSheet As Sheet

'ophalen template path definitie
oExcel = "D:\SUPERHEATER_Template1.xlsx"
i=1
For Each oSheet In oDrawDoc.Sheets
	
	CreateTable(oExcel, oSheet, i, "A2", "A3", 16, 48)
	i=i+1
Next
	
End Sub
Sub CreateTable(ByVal oExcel As String, oSheet As Sheet,i As Integer, SC_Titles As String,SC_Content As String, oColumns As Integer,oRows As Integer)

	Dim oSheetName, oCell As String
	oSheetName = "Element" & i
	
	'calculate number of columns cells
	oCells = oColumns * oRows
	
	'get the column character and row number
	SC_Titles_Column = Left(SC_Titles,1)
	SC_Titles_row = Right(SC_Titles,Len(SC_Titles)-1)
	SC_Content_Column = Left(SC_Content,1)
	SC_Content_Row = Right(SC_Content, Len(SC_Content) -1)
	SC_Content_LastColumn = Chr(Asc(SC_Content_Column) + oColumns)
	SC_Content_LastRow = SC_Content_Row+oRows
	oCell = ""

    Dim oTitles(oColumns - 1) As String
	Dim k As Integer
	k=0
	Do Until k = oColumns
		
		oCell = SC_Titles_Column & SC_Titles_row
		'Get value from Excel
		oTitles(k) = GetExcelValue(oExcel, oSheetName, oCell)
		'Set the next Column
	 	SC_Titles_Column = Chr(Asc(SC_Titles_Column) + 1)

		k=k+1
	Loop
	
	Dim oContents(oCells - 1) As String
	Logger.Info(oCells - 1)
	Dim j As Integer
	j=0
	Do Until j = oCells
		
		'check if last cell in row is reached	
		If SC_Content_Column = SC_Content_LastColumn Then
			SC_Content_Column = Left(SC_Content, 1)
			SC_Content_Row = SC_Content_Row+1
		End If
		
		oCell = SC_Content_Column & SC_Content_Row
		
		'Get value from Excel
		oContents(j) = GetExcelValue(oExcel, oSheetName, oCell)

		'Set the next Column
	 	SC_Content_Column = Chr(Asc(SC_Content_Column) + 1)

		j=j+1
	Loop
	
	Dim InsP As Point2d
    InsP = ThisApplication.TransientGeometry.CreatePoint2d(33, 38)

    Dim oCustomTable As CustomTable
    oCustomTable = oSheet.CustomTables.Add("EXISTING WELDS ON STRAIGHT TUBES", InsP, oColumns, oRows, oTitles,oContents)


End Sub
Function GetExcelValue(ByVal oExcel As String, oSheetName As String, oCell As String) As String

If GoExcel.CellValue(oExcel, oSheetName, oCell) Is Nothing Then
GetExcelValue = ""
Else
GetExcelValue = GoExcel.CellValue(oExcel, oSheetName, oCell)
End If

End Function 

 

0 Likes
Message 24 of 27

theo.bot
Collaborator
Collaborator
Accepted solution

I looks like a Inventor doesn't like the same column names when you are using the API to create tables. When you manualy create a table you can use the same column names. So i made a small work around. so I firts create a table with the unique titles, then rename all coumns after creation. 🙂

below the visual representation, but when you run the code you won't even notice that this is happend.

theobot_0-1636371605704.png

here is the new code.

Sub main
	
Dim oDrawDoc As DrawingDocument
 oDrawDoc = ThisApplication.ActiveDocument

Dim oSheet As Sheet

'ophalen template path definitie
'oExcel = "D:\SUPERHEATERS\SUPERHEATER_Template1.xlsx"
oExcel = "D:\01 Data\Workingfolder Vault\Designs\Projects\P900 - Support\SUPERHEATER_Template1.xlsx"
i=1
For Each oSheet In oDrawDoc.Sheets
	
	CreateTable(oExcel, oSheet, i, "A2", "A3", 10, 5)
	i=i+1
Next
	
End Sub
Sub CreateTable(ByVal oExcel As String, oSheet As Sheet,i As Integer, SC_Titles As String,SC_Content As String, oColumns As Integer,oRows As Integer)

	Dim oSheetName, oCell As String
	oSheetName = "Element" & i
	
	'calculate number of columns cells
	oCells = oColumns * oRows
	
	'get the column character and row number
	SC_Titles_Column = Left(SC_Titles,1)
	SC_Titles_row = Right(SC_Titles,Len(SC_Titles)-1)
	SC_Content_Column = Left(SC_Content,1)
	SC_Content_Row = Right(SC_Content, Len(SC_Content) -1)
	SC_Content_LastColumn = Chr(Asc(SC_Content_Column) + oColumns)
	SC_Content_LastRow = SC_Content_Row+oRows
	oCell = ""

    Dim oTitles(oColumns - 1) As String
	Dim oOrgTitles(oColumns - 1) As String
	Dim k As Integer
	k=0
	Do Until k = oColumns
		
		oCell = SC_Titles_Column & SC_Titles_row
		'Get value from Excel
		oOrgTitles(k) = GetExcelValue(oExcel, oSheetName, oCell)
		Logger.Info(oTitles(k))
		
		If oTitles.Contains(oOrgTitles(k))Then
			oTitles(k)=""
		Else
			oTitles(k)=oOrgTitles(k)
		End If
		
		'Set the next Column
	 	SC_Titles_Column = Chr(Asc(SC_Titles_Column) + 1)

		k=k+1
	Loop
	
	Dim oContents(oCells - 1) As String
	Logger.Info(oCells - 1)
	Dim j As Integer
	j=0
	Do Until j = oCells
		
		'check if last cell in row is reached	
		If SC_Content_Column = SC_Content_LastColumn Then
			SC_Content_Column = Left(SC_Content, 1)
			SC_Content_Row = SC_Content_Row+1
		End If
		
		oCell = SC_Content_Column & SC_Content_Row
		
		'Get value from Excel
		oContents(j) = GetExcelValue(oExcel, oSheetName, oCell)

		'Set the next Column
	 	SC_Content_Column = Chr(Asc(SC_Content_Column) + 1)

		j=j+1
	Loop
	
	Dim InsP As Point2d
    InsP = ThisApplication.TransientGeometry.CreatePoint2d(15, 15)

    Dim oCustomTable As CustomTable
    oCustomTable = oSheet.CustomTables.Add("CHURCH WINDOWS EXISTING WELDS", InsP, oColumns, oRows, oTitles,oContents)

i=0
For Each oTableColumn In oCustomTable.Columns
	oTableColumn.Title = oOrgTitles(i)
	i=i++1
	Next



End Sub
Function GetExcelValue(ByVal oExcel As String, oSheetName As String, oCell As String) As String

If GoExcel.CellValue(oExcel, oSheetName, oCell) Is Nothing Then
GetExcelValue = ""
Else
GetExcelValue = GoExcel.CellValue(oExcel, oSheetName, oCell)
End If

End Function

 

0 Likes
Message 25 of 27

emilda.chiuye
Enthusiast
Enthusiast

So I have deleted the previous code and pasted the new one and made reference to my excel spreadsheet and element number.

Unfortunately the output I get is nowhere near the cells I am reading from. Starting at Title A2 and begin cell A3

CreateTable(oExcel, oSheet, i, "A2", "A3", 10, 5)
	i=i+1

Then I get this, which is not correct. 

emildachiuye_0-1636378693799.png

This is the table from which I am abstracting information

emildachiuye_1-1636378933317.png

Where could I have possibly got it wrong for the table to read incorrectly?

 

 

0 Likes
Message 26 of 27

theo.bot
Collaborator
Collaborator

When i run the code it's result is ok:

 

theobot_0-1636380626059.png

Did you copy the code correctly, because your table contains custome titles. They should be renamed.

Message 27 of 27

emilda.chiuye
Enthusiast
Enthusiast

Hello @theo.bot 

I cannot tell you what the mix up was between yesterday and today, however, 

I will only say Thank you once more for your genius generosity 😀

0 Likes