- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Id like to be able to write to an excel file that's open, I'm not entirely sure if that's possible with the way I've set this up but having the program close every time is counter productive and id like for the excel file to remain open and writeable.
However, every time I run my rule it wants to open a new instance of inventor which I assume is due to the highlighted code below:
'define Excel Application object
excelApp = CreateObject("Excel.Application")
'workbook exists, open it
excelWorkbook = excelApp.Workbooks.Open(xlsPath)
However modifying this code, even removing the "Open" command still makes it create a new instance of Excel
the full code is below:
'define the file to create/open
xlsPath = "C:\BOM Calculator\Test.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 = True
'suppress prompts (such as the compatibility checker)
excelApp.DisplayAlerts = False
'check for existing file
If Dir(xlsPath) <> "" Then
'workbook exists, open it
excelWorkbook = excelApp.Workbooks.Open(xlsPath)
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
If .Range("A1").Value < 2
RowNum = 2
.Range("A1").Value = 2
Else
RowNUM = .Range("A1").Value
End If
.Range("B" & RowNum) = iProperties.Value(modelName,"Project", "Part Number")
.Range("C" & RowNum) = iProperties.Value(modelName, "Project", "Description")
End With
'set all of the columns to autofit
excelApp.Columns.AutoFit
'save the file
excelWorkbook.SaveAs(xlsPath)
''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
Solved! Go to Solution.
Link copied