Unhide Excel Application Session - excel

I have an Excel VBA method (I didn't write it) that runs and one of the first things it does is hide the Excel session Application.Visible = False.
However, when the method has finished, it does not unhide the Excel session so it remains open and listed in the Task Manager but is hidden and seemingly unusable.
Does anyone know, without have the VBE open (so one can access the Immediate Window and run Application.Visible = True), how to unhide this Excel session? At the moment, I'm simply having to kill the session using the Task Manager.
This isn't a massive deal but I'm just interested if anyone knows how to resurrect such a session.

Like I said, it's not a big deal but was just interested if anyone knew of shortcut key or anything to bring it back.
There is no shortcut as such that I am aware of but you can do this.
Open MS Word and paste this code in the VBA Editor. Close all open instances of Excel which are visible and then run and this code. This will make a hidden instance visible. Manually close the instance and repeat the process if there are more instances.
Option Explicit
Sub Sample()
Dim oXLApp As Object
'~~> Get an existing instance of an EXCEL application object
On Error Resume Next
Set oXLApp = GetObject(, "Excel.Application")
On Error GoTo 0
oXLApp.Visible = True
Set oXLApp = Nothing
End Sub
I am not deliberately using a loop as the hidden instance can have a workbook which you might like to save?
If you want you can convert the above code to a VB Script document which you can directly run from the desktop.
Unfortunately, I don't have the control to make the changes required.
What do you exactly mean? Is the VBA Password Protected? If no then my suggestion is still the same as earlier
This is a case of poor programming. Even if we give a code to close
all hidden Excel instances, that won't help you. Because next time you
run that macro, you will face the same problem again. Why not edit the
existing code and add Application.Visible = True at the end? Is the
VBA password protected? – Siddharth Rout 28 mins ago

A good solution!
Open up Word, assuming you have it, and open the VBA Editor there, then open the Immediate Window (Ctrl+G) and type:
Getobject(, "Excel.Application").Visible = true
and press enter.

I had a similar problem and solved it with code line reordering.
Look for a line like this ActiveWorkbook.Close that might be the reason you cannot unhide the session.
If you can find it, put Application.Visible = True just before it and voila.

as code:
sub runthis()
dim xl as object
set xl = new excel.application 'create session
xl.workbooks.open filename:= "«yourpath»" 'open wb in the new session
xl.visible=true 'this is what you need, show it up!
'rest of the code
end sub

No need for word macro at all.
Open up another excel workbook.
Hit Ctrl+F11 to go to the VBA editor and there yoy will see the running but hidden excel file on the left.
Search the code of the hidden application file for Application.Visible = False and comment it out. Save and restart the file.
Alternatively you can get back the application to show without closing if you type Application.Visible = True in the immediate window (Ctrl+G)

Related

Why additional excel window appears

Scenario
I am using a macro whereby I use Application.Visible = False to hide the workbooks. Also I use Application.Visible = True to unhide the workbook. At certain situation, I use Windows(ThisWorkbook.Name).Visible = False and Windows(ThisWorkbook.Name).Visible = True to hide and unhide only the workbook which contains macro.
Problem
I noticed during these operations, some additional excel windows(without any workbook) appear other than the workbook. Please see the picture below. You can see a grey window behind with a name Excel. That is the window I am talking about
If I closed that window, the whole excel will close. Does anyone know why this extra window appearing and how to prevent it from appearing?
I am not sure if this will meet the needs of your specific situation. But, what if you kept Application.Visible = False at the beginning of your code and changed Application.Visible = True to
Application.Windows(ThisWorkbook.Name).Visible = True at the end. This worked for me.
Hiding Excel
With the following code
Sub AppVisibleTrue
Application.Visible = True
End Sub
Sub AppVisibleFalse
Application.Visible = False
End Sub
you are showing or 'hiding' the 'whole' excel application, so you have to 'unhide' it in the same code, otherwise you won't be able to use the open files after you hide it, e.g. open a new workbook, in VBE add a new module and paste the above code into the module. Now, stay in VBE!!! Run the 'False' Sub. You will notice Excel has 'vanished', but you can still find it in the Task Manager's processes. Now run the 'True' Sub. Excel has 'reappeared'.
The following process will make Excel 'vanish'. The only way to close it will be via the Task Manager. If you not too familiar with doing this, just take my word for it.
Close the VBE. Now run the 'False' Sub. Excel has 'vanished'.
To conclude, this is obviously an error in your code, so I would suggest if you want to show a window (worksheet) that isn't 'ThisWorkbook' and you're dancing from one to the other, you should declare a variable
Const strSheet as String = "Sheet2"
Dim oSheet as Worksheet
'...
Set oSheet = ActiveWorkbook.Worksheets(strSheet)
Now you do with oSheet whatever you want.
Try to lose Active, Select and similar methods in the code.
If you would provide the actual scenario with the codes, a better assessment of the problem could be done.
The additional Excel window(s) could be related to your Windows Explorer. If you are previewing a document (in this case an Excel document) then the application to view that document is running in the background.
In this case, forcing the application to be visible will also make the background "preview" windows visible.

Copy and Paste between different Excel instances

I bought a software (with a large database), and its output is a simple Excel workbook, not saved anywhere (no path), named generically "Book1", that simply pops up on my screen.
Every time I ask the software for this output, I need to copy the content of this workbook and paste into another workbook, a mother-workbook, as I named it, to consolidate all the data.
I have to repeat this action dozens of times a day, so I thought it would be a great idea to create some VBA code to automate this task.
So... I made a very simple one:
ActiveWorkbook.ActiveSheet.Range("A1:C32").Copy
Workbooks("Mother-Workbook.xlsm").Worksheets("Sheet1").Range("B6:D37").PasteSpecial Paste:=xlPasteValues
The problem is... Each time the software outputs a new workbook, it seems that it is created in a new instance of Excel, which my macro can't reach. I mean, I run the code, but nothing happens, because my mother-workbook doesn't find the generic, unsaved and located in another excel instance "Book1".
If I open the mother-workbook after the output is opened, OK, the code works, because both are in the same instance. But as I need to keep the mother-workbook open all the time, I can't do this. I don't want to save each new output file either. It would take me a lot of time.
I'm using the 2016 version of Excel, but already tried the 2010 as well. My OS is Windows 10 Pro.
Any thoughts?
This code should do it.
Dim xlapp As Object
Set xlapp = GetObject("Book1").Application
xlapp.ActiveWorkbook.ActiveSheet.Range("A1:C32").Copy
Workbooks("Mother-Workbook.xlsm").Worksheets("Sheet1").Range("B6:D37").PasteSpecial Paste:=xlPasteValues
xlapp.DisplayAlerts = False
xlapp.Quit
Note that you need to close "Book1" at the end of your code to make sure that the next time an Excel file is created it will also be called "Book1" and not "Book2". And might as well close the Excel instance while we are at it!
For more information on the GetObject function, you can have a look at this page
Thanks a lot, DecimalTurn and Patrick Lepelletier!
The GetObject really helped me. The "closing" command worked better like this:
Sub CollectA()
Dim oApp As Application
Dim oWb As Workbook
Set oWb = GetObject("Book1")
Set oApp = oWb.Parent
oWb.ActiveSheet.Range("A1:C32").Copy
Workbooks("Mother-Workbook.xlsm").Worksheets("Sheet1").Range("B6:D37").PasteSpecial Paste:=xlPasteValues
oWb.Close False
oApp.Quit
End Sub
Cheers!

Open Excel workbook in full screen mode

I have an Excel workbook that I want to open in Full screen mode.
This workbook should open with a password lock(Which I will implement) upon double-clicking the file.
Is there a way I can open the Excel workbook in something like 'Kiosk' mode
I don't want any toolbars,Ribon interfaces,Statusbars,Cell select indicators(see below) ...
I am also looking towards locking ALL cells,so they cant be edited
Security wise,is there a way to truly protect a document.
For instance,I am looking at securing this file so that it cant be opened in any other computer,Something like a unique hash.
Looking forward to hear your ideas.
Thanks,
I have figured out 80 % of my requirement from the macro code below
Private Sub Workbook_Deactivate()
On Error Resume Next
With Application
.DisplayFullScreen = False
.CommandBars("Worksheet Menu Bar").Enabled = True
End With
End Sub
In addition to this,I have Hid the Gridlines,Heading and Formula Bar.
What I only need to do is hide the status and title bar(where it shows title.xlx)
How can I hide these both.
Can you write a macro that performs these 2 tasks and merge it with the existing macro seen above
thanks,

Refreshing an excel pivot table using windows scheduler

I am responsible for updating an Excel spreadsheet which pulls its information from an Access database on a daily basis. All the data that i need for my excel spreadsheet is available for me and all that i need to do is open the document, provide the password, enable to content and click the refresh button.
The database is very large and updating this during normal working hours causes problems as it slows down other users on the network. How would i use Windows Scheduler to do this for me outside of working hours? I'm not sure how to set up my script to follow my steps required.
I've had to do something quite similar to this recently, and with the help of this forum I've found something that works for me, and by the sounds of it may work for you too!
I created a notepad file with the following .vbs script
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
oExcel.DisplayAlerts = False
oExcel.AskToUpdateLinks = False
oExcel.AlertBeforeOverwriting = False
Set oWorkbook = oExcel.Workbooks.Open("Full Path of your file.xlsx")
oWorkbook.RefreshAll
oWorkbook.Save
oExcel.Quit
Set oWorkbook = Nothing
Set oExcel = Nothing
What this does, it opens the file, refreshes any data connections, then saves the file and exits.
I then put this as a scheduled task to run at an off peak time, so that when the user opens the workbook, it's up to date.
I hope this helps!
I managed to achieve this through the VBA
hit Alt - F11
right click ThisWorkbook and click view code.
the code is as follows:
Private Sub Workbook_Open()
Workbooks.Open ("location of your workbook"), Password:="whatever your password is"
ThisWorkbook.RefreshAll
End Sub
i save this document and ask the task scheduler to run it at a specific time.

VBS Start an instance of excel then detach from it

So I basically have a VBS script that's supposed to post data to an Excel sheet asynchronously. I currently do this by using GetObject on the workbook's path like so:
Set xlBook = GetObject(strPath & "\Runner.xlsm")
This seems to work fine, except that the workbook will close at the end of the script if it was not open previously (not desired, I have a macro that will close and save the book when necessary).
This is similar to Question 7708039, EXCEPT I want to intentionally keep the excel instance OPEN, not force it to close (the reverse of his problem).
I think it's closing because the variables referencing the object get destroyed at the end of the script, but I can't figure out how to release those handles without destroying them (i.e. set to Nothing).
Instead of getting a reference to a specific workbook, have you tried getting a reference to Excel and then opening the workbook?
' 1a. Get an existing Excel instance...
Set Excel = GetObject(, "Excel.Application")
' 1b. Or, create one. Make it visible for testing.
Set Excel = CreateObject("Excel.Application")
Excel.Visible = True
' Load the workbook...
Set Workbook = Excel.Workbooks.Open(strPath & "\Runner.xlsm")
' Do stuff and save, if desired.
' Close workbook...
Workbook.Close
' Excel stays open. If you want to close Excel, use:
Excel.Quit
Per phd443322's two comments Comment 1 and Comment 2, this is apparently by design.
The solution here is to trick the object (in this case Excel) into thinking the user will need to interact with it or maintain interaction after the reference is destroyed.
Thus, the proper workaround is to make it interactive, in this case using:
xlApp.Visible = True
Thus Excel becomes visible and won't close just because the reference is destroyed.
Since I don't want this instance of Excel visible, I then have the VBS use xlApp.OnTime to call a macro (after one second, plenty of time for the VBS script to have exited) to hide the application window again.
This makes the application blink up on the screen for a second, but it's the best I can do in this instance.
I had the same problem using
xlApp.Visible = True
with this call on windows
wscript my-script.vbs C:/path/to/file.xml
But when I changed the separator from / to \ it worked:
wscript my-script.vbs C:\path\to\file.xml

Resources