Community
Inventor Forum
Welcome to Autodesk’s Inventor Forums. Share your knowledge, ask questions, and explore popular Inventor topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

iLogic code to select excel page.

5 REPLIES 5
Reply
Message 1 of 6
andrewb
1298 Views, 5 Replies

iLogic code to select excel page.

I am very new to the iLogic function of Inventor (also poor with coding), and am looking to have the iProperties of a part autofill from an excel spreadsheet.

While I have found a line of coding that prompts the user to choose a specific row to pull the information from, the spreadsheet itself has multiple pages within itself. I was wondering if there was a line of code that would have the capacity to search through all of the pages for the line item, or prompt the user to make a selection as to what page it needs to reference. I have looked everywhere that I can think of to no avail.

Thanks in advance!

5 REPLIES 5
Message 2 of 6
Curtis_Waguespack
in reply to: andrewb

Hi andrewb,

 

Here is a quick iLogic rule that looks at each sheet in the excel file and searches for a value (myString).

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

'define the string to search for
Dim myString As String
myString = "Bingo"

'define the file to open
myXLS_File = "C:\Temp\ExcelWB-01.xlsx"

'define Excel Application object
excelApp = CreateObject("Excel.Application")

'set Excel to run visibly, change to false if you want to run it invisibly
excelApp.Visible = False
'suppress prompts (such as the compatibility checker)
excelApp.DisplayAlerts = False

'check for existing file 
If Dir(myXLS_File) <> "" Then
'workbook exists, open it
excelWorkbook = excelApp.Workbooks.Open(myXLS_File)
oSheetCount = excelWorkbook.Sheets.Count 
Else
'workbook does NOT exist
MessageBox.Show("The Excel workbook does NOT exist.", "iLogic")
End if

Dim sCellValue As String

i2 = 1
Do until i2  = oSheetCount +1
'set the excel sheet to search
GoExcel.Open(myXLS_File, "Sheet" & i2 )
	'count row data
	CurrentRow = 1 ' first row used
	i = 0
	Do Until i <> 0
		'look for empty string
		If String.isNullOrEmpty(GoExcel.CellValue("A" & CurrentRow)) Then 
			i = i + 1
		Else 
			sCellValue = GoExcel.CellValue("A" & CurrentRow)
			If sCellValue = myString Then
			'tell user which sheet and cell has the data
			MessageBox.Show("Value found on:  Sheet" _ 
			& i2 & ", A" & CurrentRow, "iLogic")
			End If
			CurrentRow = CurrentRow +1		
		End If
	Loop
'last used row
NumberOfRows = CurrentRow -1
i2 = i2 +1
Loop

''close the workbook and the Excel Application
excelWorkbook.Close
excelApp.Quit

 

Message 3 of 6
andrewb
in reply to: Curtis_Waguespack

Thanks for the response, it looks like that should do what I'm looking for.

Though, the sheets in the document have names that aren't Sheet1, Sheet2, etc... Would that change the command entirely, or could I just drop out the loop and write it out to reference every sheet specifically?

Message 4 of 6
Curtis_Waguespack
in reply to: andrewb

Hi andrewb,

 

Here is a quick update to this rule that uses the sheet's actual name, rather than building the name string from "sheet" & sheets.count.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

'define the string to search for
Dim myString As String
myString = "Bingo"

'define the file to create/open
myXLS_File = "C:\Temp\ExcelWB-01.xlsx"

‘get the Inventor user name from the Inventor Options
myName= ThisApplication.GeneralOptions.UserName

'define Excel Application object
excelApp = CreateObject("Excel.Application")

'set Excel to run visibly, change to false if you want to run it invi2ibly
excelApp.Visible = False
'suppress prompts (such as the compatibility checker)
excelApp.DisplayAlerts = False

'check for existing file
If Dir(myXLS_File) <> "" Then
'workbook exists, open it
excelWorkbook = excelApp.Workbooks.Open(myXLS_File)
oSheetCount = excelWorkbook.Sheets.Count
Else
'workbook does NOT exist
MessageBox.Show("The Excel workbook does NOT exist.", "iLogic")
End if

Dim sCellValue As String

i2 = 1
Do until i2  = oSheetCount +1
'set the excel sheet to search
oSheetItem = excelWorkbook.Sheets.Item(i2)
oSheetName = oSheetItem.Name
GoExcel.Open(myXLS_File, oSheetName )
    'count row data
    CurrentRow = 1 ' first row used
    i = 0
    Do Until i <> 0
        'look for empty string
        If String.isNullOrEmpty(GoExcel.CellValue("A" & CurrentRow)) Then
            i = i + 1
        Else
            sCellValue = GoExcel.CellValue("A" & CurrentRow)
            If sCellValue = myString Then
            'tell user which sheet and cell has the data
            MessageBox.Show("Value found on: " & oSheetName _
            & ", A" & CurrentRow, "iLogic")
            End If            
            CurrentRow = CurrentRow +1        
        End If
    Loop
'last used row
NumberOfRows = CurrentRow -1
i2 = i2 +1
Loop

''close the workbook and the Excel Application
excelWorkbook.Close
excelApp.Quit

 

Message 5 of 6
andrewb
in reply to: Curtis_Waguespack

So I've had this endeavor take on somewhat of a new form, and I needed it to do a bit more than I had originally anticipated.
I wrote out this code so that the user fills out a form with the iProperty data, then it searches for an existing part number/page in the excel spreadsheet and populates the correct cells. Also, if the part number needed is not in the excel file, it searches out the next unused row and populates the data there.
However, when I made the changes to seek out existing part numbers, find the next unused row, etc., the rule seems to have stopped working for some reason that I can't seem to find. I get no errors, and still get prompted to fill out the data on the form, but nothing at all happens past that.

I know that it is probably very cumbersome (as I am still new to the world of visual basic) but could someone take a look at my code to see maybe where I went wrong?

 

Sub Main
iLogicForm.Show("iProperty Update")
'Open iProperty form
Call CallData
'Run sub to find correct sheet for data input
End Sub

Sub CallData
GoExcel.Open("C:\Path\FileName.xlsx")
'Open Excel file
MySheet = (Left(iProperties.Value("Project", "Part Number"), 2))
'Locate correct sheet for data unput
If MySheet = ("AN") Then
Call AN_Value
ElseIf MySheet = ("BM") Then 
Call BM_Value
ElseIf MySheet = ("CH") Then
Call CH_Value
ElseIf MySheet = ("EM") Then
Call EM_Value
ElseIf MySheet = ("FB") Then
Call EM_Value
ElseIf MySheet = ("GS") Then
Call GS_Value
ElseIf MySheet = ("PL") Then
Call PL_Value
ElseIf MySheet = ("PG") Then
Call PG_Value
ElseIf MySheet = ("SH") Then
Call SH_Value
ElseIf MySheet = ("RT") Then
Call RT_Value
ElseIf MySheet = ("SP") Then
Call SP_Value
ElseIf MySheet = ("ST") Then
Call ST_Value
ElseIf MySheet = ("UH") Then
Call UH_Value
ElseIf MySheet = ("UR") Then
Call UR_Value
End If
End Sub

Sub AN_Value
Worksheet = ("C:\Path\FileName.xlsx")
'Find correct worksheet
Sheetno = ("Angle")
'Open excel
GoExcel.Open(Worksheet, Sheetno)
'Define Range
RowStart = 2
RowEnd = 10000
For count = RowStart To RowEnd
'If it's blank count it
Next
RowNumber = GoExcel.FindRow("FileName.xlsx", "Angle", "New DWG #", "=", "Part Number")
'Locate present part number in excel file
If RowNumber >= 1 Then
GoExcel.CellValue("A" & RowNumber) = iProperties.Value("Project", "Part Number")
'Return present part number to iProperty
Else If RowNumber = 0 Then
Call AddRow
'Run sub to add data to new row
End If
 End Sub

 

Message 6 of 6
andrewb
in reply to: andrewb

Ignore the last post's code.. I grabbed the wrong version. This should be the one that I am currently trying to debug.

Sub Main
iLogicForm.Show("iProperty Update")
'Open iProperty form
Call CallData
'Run sub to find correct sheet for data input
End Sub
Sub CallData GoExcel.Open("K:\Riley Vault\New Riley Part Log - Copy.xlsx") 'Open Excel file MySheet = (Left(iProperties.Value("Project", "Part Number"), 2)) 'Locate correct sheet for data unput If MySheet = ("AN") Then Call AN_Value ElseIf MySheet = ("BM") Then Call BM_Value ElseIf MySheet = ("CH") Then Call CH_Value ElseIf MySheet = ("EM") Then Call EM_Value ElseIf MySheet = ("FB") Then Call EM_Value ElseIf MySheet = ("GS") Then Call GS_Value ElseIf MySheet = ("PL") Then Call PL_Value ElseIf MySheet = ("PG") Then Call PG_Value ElseIf MySheet = ("SH") Then Call SH_Value ElseIf MySheet = ("RT") Then Call RT_Value ElseIf MySheet = ("SP") Then Call SP_Value ElseIf MySheet = ("ST") Then Call ST_Value ElseIf MySheet = ("UH") Then Call UH_Value ElseIf MySheet = ("UR") Then Call UR_Value End If End Sub Sub AN_Value Worksheet = ("K:\Riley Vault\New Riley Part Log - Copy.xlsx") 'Find correct worksheet Sheetno = ("Angle") GoExcel.Open(Worksheet, Sheetno) 'Open excel RowStart = 2 RowEnd = 10000 'Define Range For count = RowStart To RowEnd 'If it's blank count it Next RowNumber = GoExcel.FindRow("New Riley Part Log.xlsx", "Angle", "New DWG #", "=", "Part Number") 'Locate present part number in excel file If RowNumber >= 1 Then GoExcel.CellValue("A" & RowNumber) = iProperties.Value("Project", "Part Number") 'Return present part number to iProperty MsgBox("Inserting Data on Row " & row) GoExcel.CellValue("D" & row) = iProperties.Value("Summary", "Title") GoExcel.CellValue("E" & row) = iProperties.Value("Summary", "Subject") GoExcel.CellValue("F" & row) = iProperties.Value("Summary", "Category") GoExcel.CellValue("G" & row) = iProperties.Value("Summary", "Keywords") GoExcel.CellValue("A" & row) = iProperties.Value("Project", "Part Number") GoExcel.CellValue("C" & row) = iProperties.Value("Project", "Stock Number") GoExcel.CellValue("H" & row) = iProperties.Value("Project", "Description") GoExcel.CellValue("I" & row) = iProperties.Value("Summary", "Author") Else If RowNumber = 0 Then Call AddRow 'Run sub to add data to new row End If End Sub Sub AddRow If String.IsNullOrEmpty(GoExcel.CellValue("A" & count)) Then i = i + 1 End If 'Next empty row is max rows minus blank rows plus one row = RowEnd - i + 2 MsgBox("Inserting Data on Row " & row) GoExcel.CellValue("D" & row) = iProperties.Value("Summary", "Title") GoExcel.CellValue("E" & row) = iProperties.Value("Summary", "Subject") GoExcel.CellValue("F" & row) = iProperties.Value("Summary", "Category") GoExcel.CellValue("G" & row) = iProperties.Value("Summary", "Keywords") GoExcel.CellValue("A" & row) = iProperties.Value("Project", "Part Number") GoExcel.CellValue("C" & row) = iProperties.Value("Project", "Stock Number") GoExcel.CellValue("H" & row) = iProperties.Value("Project", "Description") GoExcel.CellValue("I" & row) = iProperties.Value("Summary", "Author") 'Insert data on new row GoExcel.Save End Sub

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report