Check if Excel workbook is open using VBScript - excel

I have a vbs script which writes to an Excel spreadsheet. To be able to save the spreadsheet I need to ensure that it is not already open.
Can someone suggest the best way to do this?
Some research:
I tried to create a Word Tasks objects to show me all running processes, but on Windows 7 it only tells me whether Excel is running or not.
Creating an Excel.Application object does not give me access to all running instances of Excel.
I thought about opening the spreadsheet for writing but the only way I can think of doing this is with the File System Object OpenTextFile method which does not seem to be the correct approach as the Excel file is a binary.
Any other ideas?

Since the Excel.Application object doesn't give you access to all instances of Excel, what about using a simple error trap? ie. if it is unable to save the file, throw a warning message, save as another file name, etc.

Related

Convert XLS to CSV without login

I have a PowerShell script to convert my XLS files to CSV every week. The script works well, but I have to run it manually as a login user to trigger an Excel instance in the background on my computer (as shown below).
$Excel_app = new-object -ComObject Excel.Application
I was wondering if anyone could give me some advice on how to automate this process, for example, if even my computer is logged out, the converting would still happen etc.
I am also open to rewrite the script in other languages, but I am not sure if others would also require a login user to open Excel etc.
Thanks in advance!
You can use Task Scheduler to run the task without a user logged in, but you may have to configure the DCOM permissions to get it to work non-interactively.
A better solution might be to use the ImportExcel PowerShell module, which is based on EPPlus so it doesn't need Excel at all. That should just work from Task Scheduler since there's no COM Objects at all, but there's a small chance of incompatibility if you're doing something particularly unusual with your Excel documents. It also has the advantage of not requiring Excel to be installed.

Reading Excel updates with UFT

Hi everyone,
I'am using HP UFT for testing.
I get data from an Excel file and also write data to the same Excel file.
My issue is that when i write data to the Excel file, I can't use it until the test is closed and UFT stops running (UFT won't read it, it's still blank to it).
I need a way to update UFT "view" of the Excel while running, i can't just close and open again the Excel file while test is still running.
Language used: VBScript
Can you help me?
Thank you all
You should use the UFT DataTable. The DataTable utility object gives you ways to import the Excel File programmatically at the beginning of the Test, write the files to there and when it ends, dump it back to a file. As a Bonus, you would also be able to see the DataTable content in the RunReports at the end(under the Data Tab)
Do not forget that everything you do on the DataTable while running the test is written into the memory only. In case the test cannot reach the end where you persist it again, all is lost. You can try error handling with On Error Resume Next, Recovery Scenarios or Class_Terminate based destructors (Create and instantiate a Dummy class, and if the test crashes and stops the Class_terminate will automatically be called where you can do cleanup activities.

I found an Excel configuration error: Unable to get the Object property of the OLEObject class

I want to know about OLEObject's nature and where OLEObject is located. I'm not a VBA programmer, but an VBA developer in my office found that error message, and she can't run the Excel VBA program in question on her machine. This program runs completely in another machine.
I would first ensure ActiveX controls are enabled on her machine. This has been, in my experience, the typical cause when a VBA program will run on one machine but not another.
You could also:
Try activating the object first by using .Activate beforehand
Try referencing the object by its index rather than name

close Excel from PowerBuilder

Working with PowerBuilder, I'm using an OLE object to make some changes in an Excel document, but when I disconnect the object, the Task Manager shown that it's still running. Also, if I open another Excel document, I can then open the Excel document that I made changes in.
I've tried just about everything I have seen on here, but most are using C# or something other than PB. The code that I've see and tried, doesn't run in PB.
Any ideas?
ole_excel = create oleobject
ole_excel.ConnectToNewObject("excel.application")
ole_excel.application.quit()
ole_excel.DisconnectObject()
In Task manager, I see the following:
EXCEL.EXE *32
Thanks,
Queue
Sometimes the process remained for whatever reason, I code a
Garbagecollect()
and the process vanishes.
Take care.
Georg

Open Excel workbook as read-only via VB6

I have an application written in VB6 that writes data to a spreadsheet. I'm using the MS Excel 11.0 Object library to create an instance of Excel and open the book:
Dim xlApp As Excel.Application, remoteBook As Workbook
Set xlApp = New Excel.Application
Set remoteBook = xlApp.Workbooks.Open(sheetName)
In addition to writing to the workbook "sheetName", the program also allows the user to launch the workbook in order to view the cumulative results.
There is a chance, however slim it may be, that a user could have the workbook open for viewing the results while someone else is trying to write to it. I want to give the user writing to the sheet priority. Is there a way I can launch the sheet for viewing as read-only? There is a read-only property of the excel application object, but it is (of course) read-only.
How can I set up my program to write data to the workbook even if someone has accidentally left the file open at their desk?
Simply do this:
Set remoteBook = xlApp.Workbooks.Open( sheetName, , true)
Where true is whether or not to open as Read Only. ReadOnly is the third parameter to this method.
I think you might be able to do it via the Workbook.ChangeFileAccess method as described here. Not sure if it will suit your circumstances though.
Let me make sure I have properly interpreted your issue:
Your app writes an excel file
The App launches the file in Excel
to the User
Now here's what I think you're saying:
Once the user is viewing the sheet, they may or may not want to edit that sheet.
In other words, you don't want to use
Set remoteBook = xlApp.Workbooks.Open( sheetName, , true)
100% of the time because the user viewing may want to change the data.
The downside is that this dastardly user may leave the file open to prevent other users from writing to that file.
Is that correct?
If so, it sounds like you may need to explicit state in your app to "Open for viewing" or "open for read-only" access and then toggle the Read Only property appropriately; which is probably undesirable.
However, you can't force a save on an office doc once someone else has it open.

Resources