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: 

Code to check if a file is open

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
Anonymous
1731 Views, 2 Replies

Code to check if a file is open

Hello, I create a ilogic rule to write an excel file. I need a code to check if this excel file it's open before write it.

 

Thanks in advance,

2 REPLIES 2
Message 2 of 3
RodrigoEiras
in reply to: Anonymous

 

Hi, 

 

I found this piece of code in another thread in the forum. I think it can be what you are looking for:

 

Source: http://forums.autodesk.com/t5/inventor-customization/excel-issues-with-ilogic/m-p/3741233/highlight/...

 

 

'excel file name
Dim Filename As String = "c:\temp\GGGGG.xlsx"

'Step 1.  Close workbook if it is already opened
Dim excelApp As Object 
Try 
    excelApp = GetObject(, "Excel.Application")
    For Each wb As Object In excelApp.workbooks
        If UCase(wb.fullname) = UCase(filename) Then
	   'workbook is already opened: Save and close it
	   wb.Save
	   wb.Close   
	   Exit For
        End If
    Next
Catch
Finally
    excelApp = Nothing
End Try

'Step 2.  Open file from disk and fill with iProperties
GoExcel.Open(Filename)
GoExcel.CellValue("A2")=iProperties.Value("Project", "Part Number")		
GoExcel.CellValue("A3")=iProperties.Value("Project", "Description")		
GoExcel.Save
GoExcel.Close

Beep
MsgBox("FILE IS UPDATED")
Message 3 of 3
Anonymous
in reply to: RodrigoEiras

Thanks. I found this other piece of code. I tried it and it works ok.

 

SyntaxEditor Code Snippet

Try
    Dim fs As System.IO.FileStream = System.IO.File.OpenWrite("excel.xlsx")
    fs.Close()
    Catch ex As System.IO.IOException
    ExcelTancat = False
End Try

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

Post to forums  

Autodesk Design & Make Report