Creating endless copies of objects - excel

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

Related

Macro Enable Workbook is creating new file with SaveAsUI instead of saving itselft

I have to call the below line to save my workbook from another file:
Application.ActiveWorkbook.Save
For the needs of my project, this code needs to be and run in a different file than the one I have open and active.
When this code runs from another workbook, the file that I have it open and actived calls the SaveAsUI to save the file again, even if he was already saved.
Afther run of the code above, the original workbook is losted. I cannot save anymor the active workbook, manually or by macros, and I don't want to save as my original file. Several errors occur, such as:
random filename in the SaveAsUI
Unespected Error, AutoRecover Disabled
Share Rule Violation (PT/BR)
Sorry, I cannot catch this erro in english. Here its translation:
"Your changes could not be saved in 'File' due to a sharing violation. Try saving to another file."
I noticed that this error started after I enabled auto-save for the first time*. I don't know if it is something related to the cloud and Excel gets lost when saving. Also, testing saving the original file outside the network the problem did not appear
I haven't found anything like it on the internet and it has never happened before either.
PS: The workbook with I have enabled AutoSave has nothing to do with those that are giving error. That was a workbook without macros for a much simpler project.
I was able to find what was causing the problem. I initially thought it was the VBA macro part and the "ActiveWorkbook.save" command, but in the end the problem was much deeper.
It turns out that in the same macro that contained the save command there was a call to another function that updated the queries.
the error would stop when going into PowerQuery Editor, going into the spreadsheet query that would later give the share violation error, and in the steps remove the action of promoting the headers.
I have no idea why this is the error. But I tried several other ways in several other places (like every tip in this link1, or this link2, or this link3), the only way that resulted in the solution was this above (that I don't find in any place).
But the promotion of the headers is necessary, so to continue without giving problems I needed to go into the advanced editor and change the second parameter of the line below from "null" to "true" (which is basically to promote headers at the query stage).
let
Source = Excel.Workbook(File.Contents(FilePath), true, true),
With this, everthing now is working fine.

way to close hidden excel applications? VB6 classic VB VBA

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

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.

INsert value with Asp to excelsheet and call function?

I've got a real problem..
I got an webpage writen in traditionel Asp. What i need to do now is to insert a value to a field in a excelsheet. The thing is that the excelfile also contains som sub routines and i also what to call one of thoose routines after i inserted the value in the field.. Is this even posible?
Best regards
It's all possible (although you may have problems with security in automating Office from ASP, since the IUSR user generally does not have permission to do much), but it's not a great idea, since Office is not designed to be used in a multi-threaded / multi-user environment. If this is for an intranet, then it may be OK. If it's for a public website, I'd suggest finding some other route.
Those issues aside, driving Excel from ASP is no different from driving it from VBA, VBScript or VB. You can open workbooks, make changes, even call methods defined as VBA within the workbook. For the latter, use the Application.Run method.
EDIT: some sample code (from memory - may be full of syntax errors!)
Set oExcelApp = CreateObject("Excel.Application")
Set oWorkbook = oExcelApp.Open("file.xls")
Set oWorksheet = oWorkbook.Worksheets(1)
oWorksheet.Cells(1,1).Value = "foo"
oExcelApp.Run "'file.xls'!macroname"
oWorkbook.Save
oWorkbook.Close
oExcelApp.Quit

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