Wait till Excel is saved

Wait till Excel is saved

FeelGoodGirl
Advocate Advocate
535 Views
9 Replies
Message 1 of 10

Wait till Excel is saved

FeelGoodGirl
Advocate
Advocate

Hi,
I have a piece of code that sends values to Excel. The Excel is then saved. Excel then calculates the values. And as a final step I retrieve the values from Excel.

 

Now I have a problem with saving. It seems that Inventor doesn't wait for Excel is saved. Is there a way to program Inventor to wait for the Excel to be saved?

 

I tried this, but this doesn't work:

'Retrieve the name of the excel file from the user parameter
ExcelFile = Parameter("ExcelName")

' Send to Excel
GoExcel.CellValue(ExcelFile & ".xlsx", "Values from Inventor", "B7") = ExcelHeight
GoExcel.Save

' Wait till excel is saved
ThisApplication.UserInterfaceManager.DoEvents

' Build the model
iLogicVb.RunRule("Update")
0 Likes
536 Views
9 Replies
Replies (9)
Message 2 of 10

JhoelForshav
Mentor
Mentor

Hi @FeelGoodGirl 

Is the cell you're updating used in any formula in your excel document? If you haven't already, I'd suggest changing the excel engine for GoExcel to COM and see if that helps.

See this thread:
https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/new-default-for-ilogic-excel-functio...

0 Likes
Message 3 of 10

lmc.engineering
Advocate
Advocate

Hi @FeelGoodGirl 

 

You can try something like this:

 

 

Dim filePath As String = "C:\temp\test.xlsx"
Dim iteration As Integer = 0 ' used to exit the loop if too many iterations have passed

' Start the loop
While True
	Try
		Using stream As System.IO.FileStream = System.IO.File.Open(filePath, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None)
			' The file has finished saving, as it is no longer being written to by any other processes
			MessageBox.Show("The file has finished saving.")
			Exit While
		End Using
	Catch
	End Try

	' Increment the iteration counter
	iteration += 1

	' If the loop has run for 100 iterations, exit the loop
	If iteration >= 100 Then
		Exit While
	End If
End While

 For things like this, it is always wise to put in an exit route to stop it from hanging, as the While loop is otherwise infinite. I have set it to 100 iterations, you may want to play with that number.

 

Alternatively, if you don't mind putting your code in to subs, the following might be of better use. This uses Thread.sleep between each try, meaning it's less resource intensive:

 

Public Sub test(fullfileName As String)
	Dim busyflag As Boolean = False
	Dim iterations As Integer = 0
	While FileInUse(fullfileName)
		If iterations = 5 Then 
			busyflag = True
			MsgBox("File is busy")
			Exit While
		End If
		System.Threading.Thread.Sleep(1000)
		iterations+=1
	End While
If Not busyflag Then
	MsgBox("File has been saved")
End If
	
End Sub

Private Function FileInUse(ByVal sFile As String) As Boolean
	Try
		Using f As New System.IO.FileStream(sFile, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None)
			f.Close()
		End Using
	Catch
		Return True
	End Try
	Return False
End Function

You can call the 'test' routine from a sub main as below:

Sub main()
	test("C:\Temp\test.xlsx")
End Sub
0 Likes
Message 4 of 10

FeelGoodGirl
Advocate
Advocate

hi @JhoelForshav ,

 

Thank you for your comment. I went looking for the place to convert this. We are still working with Inventor 2020. I don't see this option. Do you have a idea where i could look:

ymvdbosch_0-1671701393342.png

 

 

 

 

0 Likes
Message 5 of 10

FeelGoodGirl
Advocate
Advocate

hi @lmc.engineering,

 

Thank you for your comment. I've tried it, unfortunately without success. It has become the following code:

 

' Send tot Excel
Sub main
	
	'Retrieve the name of the excel file from the user parameter
	ExcelFile = Parameter("ExcelName")

	GoExcel.CellValue(ExcelFile & ".xlsx", "Values from Inventor", "B9") = ExcelJobNumber
	GoExcel.CellValue(ExcelFile & ".xlsx", "Values from Inventor", "B4") = ExcelWidth
	GoExcel.CellValue(ExcelFile & ".xlsx", "Values from Inventor", "B7") = ExcelHeight
	GoExcel.Save

	test("C:\LocalVaultFolder\test\GateGenerator_B300_TR100.xlsx")

	' Make the leaf
	iLogicVb.RunRule("Update")

End Sub

Public Sub test(fullfileName As String)
	Dim busyflag As Boolean = False
	Dim iterations As Integer = 0
	While FileInUse(fullfileName)
		If iterations = 20 Then
			busyflag = True
			MsgBox("File is busy")
			Exit While
		End If
		System.Threading.Thread.Sleep(1000)
		iterations += 1
	End While
	If Not busyflag Then
		MsgBox("File has been saved")
	End If

End Sub

Private Function FileInUse(ByVal sFile As String) As Boolean
	Try
		Using f As New System.IO.FileStream(sFile, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None)
			f.Close()
		End Using
	Catch
		Return True
	End Try
	Return False
End Function

 

 

I've tried incrementing the "iterations", but it keeps coming back in "MsgBox("File is busy")"-mode. Do you have maby a other idea what i can try?

0 Likes
Message 6 of 10

A.Acheson
Mentor
Mentor

Hi @FeelGoodGirl 

Have you tried closing the excel sheet after save or what is your exact workflow. Is it always open or closed?  Is the excel sheet on a network ? Testing with local seems to have no issues on Inventor 2020. With the file monitoring subs turned on I got the busy message even though the excel sheet formula had updated. If none of that is working I'm  sure you could go the long route to excel application and get a method for checking formulas are up to date. 

GoExcel.Close  

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 7 of 10

FeelGoodGirl
Advocate
Advocate

My workflow is following:

- The user opens Inventor
- In a form it chooses the variant
- The variant is created at the touch of a button
                  + The values are then sent to Excel in the background.
                  + The Excel is saved
                  + The Excel is linked in the parameters, and the data is retrieved in this way

 

The idea is that only Inventor is open and Excel is filled in and saved in the background.

I tried eding the "GoExcel.Close" afther the save, but that didnt change any thing.

0 Likes
Message 8 of 10

A.Acheson
Mentor
Mentor

"The Excel is linked in the parameters, and the data is retrieved in this way"

Are you reading the excel again through code or linked parameters via excel? 

 

We have only seen the setting of the excel sheet so it's a little difficult to test without the full workflow/data set how it is being read later. . 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 9 of 10

lmc.engineering
Advocate
Advocate

I've tested on Inventor 2022, and all works as intended. If you are getting file busy message then it would suggest that Inventor still has reference to the Excel object somewhere in your workflow. Are you referencing the Excel workbook anywhere else?

0 Likes
Message 10 of 10

FeelGoodGirl
Advocate
Advocate

Hi,

I'm completely lost by now. I wanted to make a simplified variant so that I put it here. Unfortunately I can't put the variant I'm working on online, because this is for a company.

 

Anyway, I make a simplified variant...... test...... and of course it works. I expect it to be because the Excel has to calculate more, but I don't know for sure. I'm going to try to create the problem to be able to share it.

0 Likes