From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to close a CSV file if it is already opened.


@sportsguy99 wrote:

I am not trying to open any file. I just need to make sure the Excel spreadsheet file is closed in the beginning of the program because the GUI will not append to the excel file without it being closed. I never open any files in this program. 


If you're trying to read or write to the CSV, you're opening it.  You wouldn't be having any of these questions if you weren't trying to open it.

0 Kudos
Message 21 of 39
(1,367 Views)

@sportsguy99 wrote:

Yes, that the file is opened in Excel. How would i use OS calls? Is there any other ways? I would rather not make a pop-up message.


Even Windows Explorer uses a pop-up when you try to do something to a file that is opened in another process.   This is a "User Error" and needs the User to choose what they really wanted to do.  Keep the file opened in the other application or close the file over there and, open it for editing in this application.  

 

Yes, you theoretically could grab a ref to the application that has the file opened and attempt to shut it down BUT, that will still likely throw a pop-up from that application asking if you want to save changes that you should not interfere with.  Just let the user know they made a mistake.Smiley Wink


"Should be" isn't "Is" -Jay
0 Kudos
Message 22 of 39
(1,362 Views)

The question I have been trying to ask, I might have made it a bit confusing is that the User who might use this program might have the Excel file opened and thus the GUI would not be able to append the file. I wish to make my program to close the Excel file at the start to prevent this. Yes, I can provide a popup, but wishing to just find a way to close the file.

0 Kudos
Message 23 of 39
(1,357 Views)

@sportsguy99 wrote:

Yes, but I want to make my program robust in case of errors if another user uses the program. Just in case they have the file opened when running the GUI, i want my program to close the file.


Well there is making a robust program and there is trying to fix stupid.

 

You can't fix stupid.

 

If someone has the file open in Excel and then tries to open the same file in another program they are going to get a sharing violation message like the file is already open in another program.

 

That's just the way it is.

 

After they have to abort, close the file in Excel, and start over a few times they will get a clue.

 

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 24 of 39
(1,351 Views)

They would not be trying to open it up again. The GUI just is not able to append to the file since it is already opened in excel. Is there any way to close the excel file at the start? 

0 Kudos
Message 25 of 39
(1,347 Views)

@sportsguy99 wrote:

They would not be trying to open it up again. The GUI just is not able to append to the file since it is already opened in excel. Is there any way to close the excel file at the start? 


Yes, display a dialog to tell the user to close the fileSmiley Very Happy


"Should be" isn't "Is" -Jay
Message 26 of 39
(1,352 Views)

How about using something like this? i have been trying to implement it but I cant get the visible node. I found this example on another forum.

0 Kudos
Message 27 of 39
(1,346 Views)

@sportsguy99 wrote:

They would not be trying to open it up again. The GUI just is not able to append to the file since it is already opened in excel. Is there any way to close the excel file at the start? 


Well as people have suggested there is no easy way to close a file that Excel has open beyond finding the running Excel process and killing it.

 

Even doing that may not be seamless...

 

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 28 of 39
(1,342 Views)

@sportsguy99 wrote:

How about using something like this? i have been trying to implement it but I cant get the visible node. I found this example on another forum.


That just pops up a prompt from Excel to save any changes to EVERY file that might be open in that application (Not just your csv)


"Should be" isn't "Is" -Jay
0 Kudos
Message 29 of 39
(1,336 Views)

Okay thanks. I will try to implement that solution if not, I will continue with a popup message

0 Kudos
Message 30 of 39
(1,333 Views)