it is possible to check if an file is alway open ?

it is possible to check if an file is alway open ?

Darkforce_the_ilogic_guy
Advisor Advisor
255 Views
1 Reply
Message 1 of 2

it is possible to check if an file is alway open ?

Darkforce_the_ilogic_guy
Advisor
Advisor

I have an code da get some data form the model , copy an templete Excel fil , and then open it 

 

My problem is that If i run the code without close the excel the Excel file have the same name as the part numer the code will fail.. that I want it to do is to check if the file is open , and if it is close it before copy the templete and open it again. 

 

is there a way to do this ?

0 Likes
256 Views
1 Reply
Reply (1)
Message 2 of 2

Ralf_Krieg
Advisor
Advisor

Hello

 

You can try following

Add in the Header of your rule

AddReference "Microsoft.Office.Interop.Excel.dll"
Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices 

 

In your rule add

Dim objApp As Excel.Application= TryCast(Marshal.GetActiveObject("Excel.Application"), Excel.Application)

If objApp IsNot Nothing Then
	Dim objBooks As Excel.Workbooks
	objBooks = objApp.Workbooks     
	     
	Dim objBook As Excel._Workbook
	For Each objBook In objBooks
		If objBook.Name= "Document-Name_With-Extension-Without_Path" Then
		'If objBook.FullName= "DocumentName_With-FullPath-and-Extension" Then 'alternative, if document name alone is not specific enough
			objBook.Close
			Exit For
		End If
	Next
End If

R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes