Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding the Excel application during run-time

Tags:

excel

vba

I'm trying to hide Excel during a long script in which I do some web-scraping. I'm able to hide the application just fine, the problem is that when I change .Visible back to True, I'm getting 1-2 more additional applications (just empty Excel shells). I'm guessing one of these are my PERSONAL.xlsb workbook, but I'm not sure what the other one is - sometimes I get one extra, sometimes I get two. The only way I can close these shell files is by ending the EXCEL.EXE process via task manager.

I've tried hiding just the main window (Windows(1)) as well to no avail (it just hides the workbook, not the application):

Sub Test()

Windows(ThisWorkbook.Name).Visible = False

Application.Wait (Now + TimeValue("0:00:05"))

Windows(ThisWorkbook.Name).Visible = True

End Sub

How can I just have my main workbook re-appear?

Sample code:

Sub Test()

Application.Visible = False

Application.Wait (Now + TimeValue("0:00:05"))

Application.Visible = True

End Sub

enter image description here

Edit: This is on Windows 7, Excel 2016

Edit2: Running just Application.Visible = True by itself also gives me these two phantom applications.

Edit3: The issue definitely has to do with having macros stored in the PERSONAL.xlsb file - when I go onto a fresh computer and add a new macro to this workbook, I can reproduce the issue. However, I'm still not sure how to avoid it...

Task manager:

img1

The script that opens Excel from Filemaker Pro:

Open URL [With dialog:Off; "C:\Users\Username\Desktop\TestFile.xlsm"]

Inside TestFile.xlsm:

Private Sub Workbook_Open()

Application.Visible = False

'Refresh a query in the Excel workbook that is linked to Filemaker Pro

'Webscrape, webscrape, webscrape from a worksheet inside this Excel document
'to a hidden Internet Explorer Window (ewww, IE!)

Application.Wait (Now + TimeValue("0:00:05"))

Application.Visible = True

'Either close Excel completely or reload my main instance of Excel

End Sub

I've realized that I can just completely quit Excel with Excel.Application.Quit, but I haven't decided if I want to exit out right away, or repaint a UserForm in Excel that summarizes the import process

like image 614
dwirony Avatar asked Sep 26 '18 18:09

dwirony


People also ask

How do I hide my screen when running a macro?

At the beginning of the macro add 'Application. ScreenUpdating = False'. At the end of the macro add 'Application. ScreenUpdating = True'.

How do I enable disable screen update or hide the process of running a macro?

Turn OFF Screen Updating in VBAFirst, type the keyword “Application”. After that, press a dot “.” to open the properties and methods list. Now, select “ScreenUpdating”. In the end, specify “False” to it.

How do you stop Excel macro when it is running?

If the Macro is simply in a continuous loop or is running for too long you can use one of these keyboard shortcuts to kill it: Esc hit the Escape key. Ctrl + Break hit Ctrl key and then the break key, which is also the pause key.


1 Answers

I was able to reproduce your issue. First, I tested hiding the Application without having PERSONAL.xlsb loaded, and it worked fine. Then I loaded PERSONAL.xlsb and got the same behavior you did: an extra Excel shell became visible after Application.Visible = True.

I'm not sure why you sometimes get two extra shells, but maybe you have another addin (.xlam) loaded? You could try adding some code to unload all addins first, but I have an alternative solution: why not just launch a new instance of Excel, load your macro workbook in it and run the macro? For example, if your workbook is called C:\Book1.xlsb and the macro in it is "MyMacro" then create a second workbook with code that will launch Book1. Like this:

Sub LaunchIt()

    Dim xlApp As Excel.Application
    Dim wb As Workbook

    Set xlApp = New Excel.Application

    With xlApp
        Set wb = .Workbooks.Open("C:\Book1.xlsb")
        .Run "'" & wb.Name & "'!MyMacro"
        wb.Close SaveChanges:=False
        .Quit
    End With

End Sub

The new instance of Excel is not visible by default, so no need to set visibility. I tested it and it worked for me.

like image 165
Excel Developers Avatar answered Oct 14 '22 12:10

Excel Developers