How to know if a txt file is already open with vba? - excel

I need to open/create a txt file with Excel VBA, but before opening the file I need to make sure it is not already open.
How can I know if it's open, and if so, close it before opening it again ?
Something like :
If IsOpen(File_Name) Then
Close(File_Name)
EndIf

Unless you want to re-create the same file from scratch, if the file tests open, then why don't you just use it? If it tests NOT open, then open it. Then, if it doesn't exist create it.
You may just clear out the text file if it is open and you want to restart the text instead of overwriting by creating the file again.

Related

Save Excel-data with Matlab

Here's my problem:
I want to open an Excel file which serves as a template. This file should be edited and then saved. The name is a variable, so it should always change. I.e. variable A that changes all the time.
With the function [xlsread] I open the template and with the function [xlswrite] I can change it as I like and it works without problems. The problem is I can't save the file separately, that means that after editing the template I only get 1 file back, but I want to keep the template and get an edited file so I can use the template again. I could not find a way to save the edited Excel file.
Please help me
There is a mistake in your thinking. xlsread reads the whole file and keeps it in memory. When you use xlswrite your variables from memory are written to a file, with no link to the file read (as long as you change the file name).
That having said, please note that Matlab recommends to use writetable/writematrix/writecell instead of xlswrite. The same is true for xlsread => readtable/readmatrix/readcell.

Need to monitor user downloads folder for new .xlsx files, then open Excel, save the file, and close Excel

At work I use a tool that requires me to download a .xlsx file, open it, then save it to remove the workbook protection. I'd like to create a script that automatically opens and saves any .xlsx files that are saved in the downloads folder.
I'm assuming PowerShell is the right tool to use. I found this article that explains how to do something similar, but I need help with configuring the parameters.
https://social.technet.microsoft.com/Forums/scriptcenter/en-US/1faa97e0-2288-4bb2-b8ad-283df32779d4/can-i-open-an-excel-workbook-when-files-with-a-certain-extension-are-saved-in-a-specified-folder?forum=ITCG
What I need is for the script to monitor the $env:USERPROFILE\downloads folder for any .xlsx files. Then I need it to open that file in Excel (preferably in a minimized window), save it with the same filename, then close Excel.
You can use IO.FileSystemWatcher to monitor a directory. See Start-FileSystemWatcher for an example implementation in PowerShell.

Recover unsaved .csv file changes

Does anyone know a way to recover changes made in a .csv that were not saved when excel 2007 was closed.
At the moment, I don't see any way to solve this.
Check all the temporary files created recently. Especially alongside the file you opened. There are a few temp folders in the system that Excel may use. C:\Windows\temp is the main one, but it is usually under the Users folder in later versions of windows. Eg: C:\Users\YourUserName\Local Settings\Temp
If you find any files that look like Excel temps, take a copy and rename the extension and then try to open it.
(Your only real chance is if auto-save kicked in and saved a copy - to a temp file - when you still had your new changes in the document. Otherwise the changes are lost I'm afraid)
If option #1 doesn't resolve your problem in Excel, go to File->Options->Save. Hopefully your AutoRecover file location, under the third box, will be populated with an address.

Matlab: open files 'outside Matlab' by default

I'm looking for a way to have Excel files in my Matlab folder open 'outside Matlab' (i.e., by MS Excel in most cases) directly by double-clicking the file, rather than right-clicking and selecting 'Open Outside Matlab'.
The .xls files reader built in Matlab can be terribly slow for large files, and an unwanted double-click on a file can cost quite some time in which Matlab is unresponsive.
Thanks.
When you click something in the Current Folder tab, it's actually running the open command, which itself calls finfo to determine what it means by "open" for a given extension. You can see this by creating a breakpoint in open.m directly after the line [~, openAction] = finfo(fullpath); and double clicking - when it hits the breakpoint you'll see it returns openAction as uiimport.
In theory, you can create custom methods for extensions by creating on the path a function openabc where abc is the extension, which should be returned as the openAction.
However, if I look at my finfo.m it first searches for said functions and then regardless of whether or not it finds them if there is an inbuilt method it overwrites them with the standard behaviour. There's even a comment:
% this setup will not allow users to override the default EXTread behavior
If you are willing to muck about in the inbuilts, you may be able to do it like this (backup first! - this could affect other things). I did it temporarily by shadowing the existing finfo like this:
edit finfo.m (Now save a copy to the current folder)
Add these lines after the loop that defines the openAction (in my version, around line 85):
if any(strcmp(['.' ext], matlab.io.internal.xlsreadSupportedExtensions))
openAction = 'winopen';
end
From the folder containing your edited finfo.m, type which finfo -all. You should see two copies, the MATLAB one labelled as shadowed. Opening something from the current folder window should now open Excel externally.
I don't believe there's any straightforward way to do that. It's built in to MATLAB that Excel files will open in the import tool when you double click on them, and there's no way to change that.
You might be able to get around it by changing the file extension on your Excel files to something other than .xls or .xlsx. That would stop MATLAB from opening it in the import tool. Then in Windows, you could associate the new file extension with Excel.

Opening Excel and PDf files with Tcl Tk

I am having problems opening an existing Excel file with Tcl Tk. I am able to open an existing MS Word file with no problems. The code that I am using is as follows, also my test application has "package require tcom" included:
proc OpenFile {} {
#Path to file
set app [::tcom::ref getobject "C:\\Users\\Me\\Desktop\\Test.doc"]
#Change path to application
set this [$app Application]
#Open application
$this Visible 1
}
This code is executed by a button. Basically, Test.doc gets opened after the button is pressed.
I tried changing the file to an existing Excel file, and when I press the button the file opens for a split second, and then closes. This also happens with MS Access files, as well.
Does anyone know how to open an existing Excel file with Tcl Tk, and make it stay open? Additionally, for PDFs and text files, I understand that I cannot use Tcom to open these files. Does anyone know how to open PDFs, text, and other non-MS files with Tcl Tk?
I really appreciate your help!
Thank you,
DFM
Assuming you're on Windows and you just want to open a file (.xls, .pdf, ...) with its usual application (ie. not modifying the file from your script) you can just use "start" like this:
set TestDoc "My Test.xls"
eval exec [auto_execok start \"\" [list $TestDoc]

Resources