way to close hidden excel applications? VB6 classic VB VBA - excel

I am playing around with some code that may allow a hidden instance of an excel application to be created and remain hidden and running. I wanted to run a quick check to find these and close them.
I came up with the following code which doesn't work. Any ideas?
Dim xl As Excel.Application
For Each xl In Applications
If xl.Visible = False Then xl.Quit
Next

What you can do is, BEFORE instantiating a new excel application, iterate to all current processes containing "EXCEL.EXE", store the process ids, create you new instance of the Excel application, iterate again (there should be one more process with "EXCEL.EXE"), then you have the process id to kill after .. not an elegant solution, but works for killing your zombie processes

Related

Creating endless copies of objects

I have a macro which theoretically works perfectly but I think it is creating unnecessary processing and calculation requirements which then crashes the file. It's in a file which I use regularly so maybe the problem is getting worse over time as well- hard to say. In the VBA Project window under Microsoft Excel Objects I have endless copies of all objects referenced which seem to have been created automatically ie
Sheet11
Sheet111
Sheet1111
ThisWorkbook
ThisWorkbook1
ThisWorkbook10
ThisWorkbook11
etc...
Does anyone know why this is/if it's an issue? If it's an issue how do I get rid of it?
The other related question I have is that I've been using the following macro to access data in a workbook without fully opening it, but I'm concerned that the wb.close command doesn't work when the workbook is open as a background process ie I suspect that maybe the workbook stays open in the background, slowing things down. Below is an example of the code which does this:
Dim xlApp As Application
Set xlApp = GetObject("X:\Power\UK\GA\doc.xlsx")
Set wbcheck = xlApp.Workbooks("doc.xlsx")
'Do stuff!!
wbcheck.Close False
Apologies that this is a fairly general question but essentially I want to write cleaner code that won't make my spreadsheet crash!
Thanks
Rachel

Add-ins not loading when opening excel file programmatically

I've seen some similar problems described by others before but no real solution. And I'm sure there is one.
I have a .XLA-add in configured to be loaded when I open up Excel. It works fine when I open documents or Excel it self. However, when my BI-system programmatically creates and opens an Excel-file the add-in does not get loaded. The BI-system opens Excel in a new instance so it does not help to have opened Excel on beforehand (and thereby the .XLA-add in)
If i Check Options-Add Ins it looks like the add-in is loaded but it is not!
What I've tried (and that does work) is to insert this function into the created excel-file and "reload" the add-ins, but I want to find an automated solution!
Function ReloadXLAddins(TheXLApp As Excel.Application) As Boolean
Dim CurrAddin As Excel.AddIn
For Each CurrAddin In TheXLApp.AddIns
If CurrAddin.Installed Then
CurrAddin.Installed = False
CurrAddin.Installed = True
End If
Next CurrAddin
End Function
Is there any way to load my Add ins automatically when instancing excel programmatically?
Any tips, solutions or workarounds are highly appreciated!
Best regards.
This may not be possible in VBA. I have come across this problem before, and used the implementation found here: Excel interop loading XLLs and DLLs
However, that solution is for C#. The steps required may not be possible when coming from inside of an Excel VBA script. Perhaps there are some VBA implementations you can look at, but wanted to give you some sort of starting place because I know that is a frustrating place to be.

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

Check if Excel workbook is open using VBScript

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.

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