Announcements

Starting in December, we will archive content from the community that is 10 years and older. This FAQ provides more information.

Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to create a excel file using ilogic? Thanks

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
Anonymous
9588 Views, 11 Replies

How to create a excel file using ilogic? Thanks

 
11 REPLIES 11
Message 2 of 12
Curtis_Waguespack
in reply to: Anonymous

Hi MtigaM, 

 

Here is an iLogic example.

 

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

 

'define the file to create/open
myXLS_File = "C:\Temp\Best_Excel_File_Ever.xls"

‘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 invisibly
excelApp.Visible = True
'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)
ExcelSheet = ExcelWorkbook.Worksheets(1)
Else
'workbook does NOT exist, so create a new one
excelWorkbook = excelApp.Workbooks.Add
End if

'Insert data into Excel.
With excelApp
    .Range("A1").Select
    .ActiveCell.FormulaR1C1 = "Hello, " & myName
End With   

'set all of the columns to autofit
excelApp.Columns.AutoFit   
'save the file
excelWorkbook.SaveAs (myXLS_File)

''close the workbook and the Excel Application
''uncomment if you want to close the xls file at the end
'excelWorkbook.Close
'excelApp.Quit
'excelApp = Nothing    
Message 3 of 12
Anonymous
in reply to: Curtis_Waguespack

Thanks alot, this is what i am looking for!

Message 4 of 12
Anonymous
in reply to: Anonymous

Is it possible to open a template excel file somewhere and then save it as a new excel in another place?

Message 5 of 12
Curtis_Waguespack
in reply to: Anonymous

Hi MtigaM,

 

To specify the template to use you can use this example.

 

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

 

 

'define the file to create/open
myXLS_File = "C:\Temp\Best_Excel_File_Ever.xls"

‘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 invisibly
excelApp.Visible = True
'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)
ExcelSheet = ExcelWorkbook.Worksheets(1)
Else
'create a new spreadsheet from template
excelWorkbook = excelApp.Workbooks.Add (Template: = "C:\Temp\Best_Excel_Template_Ever.xlt")
End if

'Insert data into Excel.
With excelApp
	.Range("A1").Select
	.ActiveCell.FormulaR1C1 = "Hello, " & myName
End With   

'set all of the columns to autofit
excelApp.Columns.AutoFit   
'save the file
excelWorkbook.SaveAs (myXLS_File)

''close the workbook and the Excel Application
''uncomment if you want to close the xls file at the end
'excelWorkbook.Close
'excelApp.Quit
'excelApp = Nothing       

 

Message 6 of 12
Anonymous
in reply to: Curtis_Waguespack

Thank you very much!

Message 7 of 12
Anonymous
in reply to: Curtis_Waguespack

can we specify the directory of the saved file?

Message 8 of 12
Anonymous
in reply to: Curtis_Waguespack

Thank you very much. This is brilliant. Could we create different sheets as well

Message 9 of 12
scottvan
in reply to: Anonymous

I have been using the following code to check if I have the proper amount of worksheets, and add worksheets if necessary:

 

'Define Excel application and run in the background
xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
xlApp.DisplayAlerts = False
xlWorkbook = xlApp.Workbooks.Add
xlWorksheets = xlWorkbook.Sheets

'Check number of sheets, add if necessary
Do While xlApp.Workbooks(1).Sheets.Count < 3
	xlApp.Workbooks(1).Sheets.Add
Loop

'Name Excel Sheets
xlApp.Workbooks(1).Sheets(1).Name = "First Sheet Name"
xlApp.Workbooks(1).Sheets(2).Name = "Second Sheet Name"
xlApp.Workbooks(1).Sheets(3).Name = "Third Sheet Name"

Hope this helps!

Message 10 of 12
Anonymous
in reply to: Curtis_Waguespack

If the last line of code below is commented, after running the rule it open another read-only excel file. Is it possible to update the excel without closing and opening it again? 

 

 

''close the workbook and the Excel Application
''uncomment if you want to close the xls file at the end
'excelWorkbook.Close
'excelApp.Quit
'excelApp = Nothing

 

Message 11 of 12
leoow96
in reply to: Anonymous

Hello 🙂

 

How can I save the Excel file with the Windows form?

For example, how do I write in sheet 2?

Message 12 of 12
tableauzeichner1
in reply to: Anonymous

Hey, for example like this, you can specify the directory and get a variable name for your file. 😉

 

tableauzeichner1_0-1710920191005.png

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

Post to forums  

Autodesk Design & Make Report