Results 1 to 34 of 34

Thread: Prevent opening second instance of excel?

  1. #1

    Thread Starter
    Banned
    Join Date
    Oct 2015
    Location
    Earth
    Posts
    175

    Prevent opening second instance of excel?

    Hi there,
    My question is regarding VBA and vb.net.

    I found following VBA codes from this source http://www.mrexcel.com/forum/excel-q...lications.html
    Following codes prevents opening second instance of excel.
    Let me tell you how following code works.
    If someone try to open second excel file then following code close second excel file immediately.
    Code:
    Public WithEvents myApp As Application
    Private Sub myApp_WorkbookOpen(ByVal Wb As Workbook)
        Wb.Close
    End Sub
    Code:
    Public aApp As Class1
    
    Sub Test()
        Set aApp = New Class1
        Set aApp.myApp = Application
    End Sub
    
    Sub UnTest()
        Set aApp = Nothing
    End Sub

    The question is how to convert that VBA codes to vb.net codes with windows form application?

    Best regards...

    Another link...
    http://www.mrexcel.com/forum/excel-q...-instance.html
    Last edited by Herry Markowitz; Nov 23rd, 2015 at 10:44 AM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Prevent opening second instance of excel?

    the code posted above does not do what you want, read the follow ups, it only prevents the opening of a workbook in the same instance of excel

    the last post in the thread may do so and could be converted to .net easily enough, but would use a timer or similar type loop to detect new instances at whatever interval, or with an addin, you could call the code in a workbook open event

    it should also be possible to monitor processes, with a callback when a new process is created, not that i have done that

    a search on monitor processes with callback, will return many results
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Banned
    Join Date
    Oct 2015
    Location
    Earth
    Posts
    175

    Re: Prevent opening second instance of excel?

    Quote Originally Posted by westconn1 View Post
    the code posted above does not do what you want, read the follow ups, it only prevents the opening of a workbook in the same instance of excel
    Yes you are right. The code I posted prevents opening second excel file not second instance.(Start > Microsoft excel).

    Quote Originally Posted by westconn1 View Post
    it should also be possible to monitor processes, with a callback when a new process is created, not that i have done that
    a search on monitor processes with callback, will return many results
    Could you please provide a code for this?

    Edit:
    My main request is prevent opening second instance of excel at first.
    I mean I dont want to close second excel as soon as opening...
    Last edited by Herry Markowitz; Nov 22nd, 2015 at 11:14 AM.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Prevent opening second instance of excel?

    Could you please provide a code for this?
    as i said above, i have never done this

    a simple option is to use an addin or put code in the personal workbook to create an application object with events, then you can test for instances of excel in the application.workbookopen event like
    Code:
    Private Sub myApp_WorkbookOpen(ByVal Wb As Workbook)
        if GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2").ExecQuery("select * from win32_process where name='EXCEL.EXE'").Count > 1 then
            set prxl = GetObject(, "Excel.Application")
            fn = wb.fullname
            wb.close false
            prxl.workbooks.open(fn)
            set prxl = nothing
            myapp.quit
    
        end if
    
    End Sub
    this is not in anyway tested, it is an excel solution, that should use an addin, if you want an external solution, using .net, you would need to research my previous suggestion, using google or similar searches, to find who has done similar code
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Banned
    Join Date
    Oct 2015
    Location
    Earth
    Posts
    175

    Re: Prevent opening second instance of excel?

    Hi westconn1,
    Thanks for code but I need a vb.net code for this...

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Prevent opening second instance of excel?

    Quote Originally Posted by Herry Markowitz View Post
    My main request is prevent opening second instance of excel at first.
    I mean I dont want to close second excel as soon as opening...
    This is how I would do it in VB.Net

    Code:
    Imports Excel = Microsoft.Office.Interop.Excel
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim ExcelInstance As Integer = Process.GetProcessesByName("excel").GetLength(0)
    
            If ExcelInstance > 0 Then
                MessageBox.Show("Excel is running. You may not open a new instance")
            Else
                '~~> Define your Excel Objects
                Dim xlApp As New Excel.Application
                Dim xlWorkBook As Excel.Workbook
    
                xlWorkBook = xlApp.Workbooks.Open("C:\MyFile.xlsx")
    
                '~~> Display Excel
                xlApp.Visible = True
            End If
        End Sub
    End Class
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7

    Thread Starter
    Banned
    Join Date
    Oct 2015
    Location
    Earth
    Posts
    175

    Re: Prevent opening second instance of excel?

    Hi Siddharth,
    Your code do one time check if excel is open or not.
    I need everytime check if excel is open or not.
    Because second excel can not to be opened until my project finished...
    Maybe instead of everytime check code we need a code which provides preventing opening second excel?
    Any other idea?

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Prevent opening second instance of excel?

    I am not sure I understand. Where are you opening the 2nd instance of Excel from? VB.Net or somewhere else?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  9. #9

    Thread Starter
    Banned
    Join Date
    Oct 2015
    Location
    Earth
    Posts
    175

    Re: Prevent opening second instance of excel?

    I am designing excel sheets from vb.net.
    I want user dont open another excel file during my project is running (during designing excel sheets from vb.net).

  10. #10
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Prevent opening second instance of excel?

    Do you mean not allow the user to open Excel file manually while your application is running?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  11. #11

    Thread Starter
    Banned
    Join Date
    Oct 2015
    Location
    Earth
    Posts
    175

    Re: Prevent opening second instance of excel?

    Quote Originally Posted by Siddharth Rout View Post
    Do you mean not allow the user to open Excel file manually while your application is running?
    Yes it is.

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Prevent opening second instance of excel?

    you should be trying to avoid the user upsetting your application, without locking them out of any part of their computer, or preventing them from running some application, you should find some alternative to what you are trying to do, i personally would look on your application with horror, if it tried to stop me using excel or any other application
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13

    Thread Starter
    Banned
    Join Date
    Oct 2015
    Location
    Earth
    Posts
    175

    Re: Prevent opening second instance of excel?

    Quote Originally Posted by westconn1 View Post
    you should find some alternative to what you are trying to do
    I found another way which makes mouse and keyboard disable but that way is the worst
    If you have any other advice I will be happy to hear it

  14. #14
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Prevent opening second instance of excel?

    I agree with Pete here. You should not try and change the User's setting without his/her approval. You can always pop up a message box alerting the user to not open another excel instance till the time your application has stopped finishing whatever it is doing. For example many of the application while installing tell you to close all windows... That is the right way to do it
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  15. #15

    Thread Starter
    Banned
    Join Date
    Oct 2015
    Location
    Earth
    Posts
    175

    Re: Prevent opening second instance of excel?

    I want this because if user opens second excel then my application fails...
    And if my application fails then it is not easy to fix errors...
    That is why I want to be sure that user must not open second excel file!

  16. #16
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Prevent opening second instance of excel?

    Why would a user open a second instance

    1. When you as a programmer has instructed not to do it?
    2. When the user knows that opening a second instance will hamper the installation?

    Not that I don't know the answer to your question. I do but somehow I am not convinced of your intentions...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  17. #17

    Thread Starter
    Banned
    Join Date
    Oct 2015
    Location
    Earth
    Posts
    175

    Re: Prevent opening second instance of excel?

    All right.
    If there is no solution of this problem lets stop here.
    Thanks westconn1.
    Thanks Siddharth.
    Have a nice day.

  18. #18
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Prevent opening second instance of excel?

    I want this because if user opens second excel then my application fails...
    you should fix your application, not to fail
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  19. #19

    Thread Starter
    Banned
    Join Date
    Oct 2015
    Location
    Earth
    Posts
    175

    Re: Prevent opening second instance of excel?

    Quote Originally Posted by westconn1 View Post
    it should also be possible to monitor processes, with a callback when a new process is created, not that i have done that
    a search on monitor processes with callback, will return many results
    Hi Pete,
    Could you please provide a sample results for this?

  20. #20
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Prevent opening second instance of excel?


  21. #21

    Thread Starter
    Banned
    Join Date
    Oct 2015
    Location
    Earth
    Posts
    175

    Re: Prevent opening second instance of excel?

    Hi vbfbryce,
    Thanks for the link.
    But the code in the link is not dynamic code.
    Because the code in the link doesnt run whenever Notepad opens.
    But thanks anyway.

  22. #22
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: Prevent opening second instance of excel?

    Seriously, you should not be preventing a user from running an application that has nothing to do with your own. That is hijacking the user's machine without their consent. Windows is going to fight you every step of the way and with good reason - if you were able to do this it would be a wide open door to installing and, more importantly, preventing the removal of, malicious code.

    If your application fails when the user opens a second copy of excel then there is something seriously flawed in your application. Fix the flaw.

    What, exactly, causes your application to fail? There is nothing about just opening excel that would cause a fail. So what, exactly, are you trying to do with excel and what is the user interaction that causes it to fail.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  23. #23

    Thread Starter
    Banned
    Join Date
    Oct 2015
    Location
    Earth
    Posts
    175

    Re: Prevent opening second instance of excel?

    Hi Funky,
    According to following reasons I am insist on preventing opening second instance of excel or minimize excel whenever user try to open excel.
    1- If user clicks any excel cell then my application fails.(And there will be some errors which are not easy to clear)
    2- I dont want users see my excel formulas and excel objects which I am using in order to avoid my idea and my formulas are being copied.
    Any idea?

  24. #24
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Prevent opening second instance of excel?

    Have your app work in its own instance of Excel, and have it stay hidden.

  25. #25

    Thread Starter
    Banned
    Join Date
    Oct 2015
    Location
    Earth
    Posts
    175

    Re: Prevent opening second instance of excel?

    My app designed work with excel.
    That is why I want to prevent opening second instance of excel or minimize excel.
    I dont want to hide excel because some codes doesnt work when excel is hidden.

  26. #26
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Prevent opening second instance of excel?

    1- If user clicks any excel cell then my application fails.(And there will be some errors which are not easy to clear)
    no excuse for this, fix it

    2- I dont want users see my excel formulas and excel objects which I am using in order to avoid my idea and my formulas are being copied.
    lock the appropriate worksheets and VBA projects, or create all at runtime
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  27. #27
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Prevent opening second instance of excel?

    "...some codes doesnt work when excel is hidden."

    Maybe this can be fixed.

  28. #28
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: Prevent opening second instance of excel?

    no excuse for this, fix it
    lock the appropriate worksheets and VBA projects, or create all at runtime
    Maybe this can be fixed.
    I agree with all these statements.

    What errors are you getting? What code throws the error? What doesn't work when it's hidden? I guarantee you that it will be easier to fix these errors in your code than it will be to take control of the users machine. Taking control of the users machine is something your user will not want you to do and something that Windows will actively try and prevent you from doing because it represents a massive security risk. None of us are going to help you do it for exactly the same reason - it would be a hacker's delight. When you need to enter a house you use the right key in the front door - you don't start smashing the Windows.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  29. #29
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Prevent opening second instance of excel?

    you don't start smashing the Windows.
    lol, i like this
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  30. #30

    Thread Starter
    Banned
    Join Date
    Oct 2015
    Location
    Earth
    Posts
    175

    Re: Prevent opening second instance of excel?

    My question is very simple.
    Please do not make my question so complicated.

    Let me summarize my question again;
    Following code minimizes excel.
    I need dynamic version of following code.
    Dynamic code means the code must work whenever user try to open second excel during my app is running!

    Code:
     Dim ExcelApp As Excel.Application
            ExcelApp = GetObject(, "Excel.Application")
            ExcelApp.WindowState = Excel.XlWindowState.xlMinimized


    I tried to use application event for dynamic code but couldnt succeded.
    Look at this;
    http://prntscr.com/990rqi
    Last edited by Herry Markowitz; Dec 1st, 2015 at 05:48 AM.

  31. #31
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: Prevent opening second instance of excel?

    And the answer's very simple: you can't.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  32. #32
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Prevent opening second instance of excel?

    This is what I'd use to open an instance "in the background" that your users would not (easily) be able to stumble onto:

    Code:
    Dim oXLApp As Excel.Application
            Dim oXLBook As Excel.Workbook
    
            oXLApp = New Excel.Application
    
            'oXLApp.Visible = True  don't make it visible, nor minimized
    
            oXLBook = oXLApp.Workbooks.Open("c:\yourPath\testDotNet.xlsx")

    If I run this, then open a 2nd instance of Excel manually, it has no impact on the code I run after the above.

  33. #33
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: Prevent opening second instance of excel?

    +1 to the suggestion above. You can run your own code in an isolated instance. What you can't do is stop the user from using their machine.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  34. #34
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: Prevent opening second instance of excel?

    In light of your last post (which I have deleted) I am closing this thread
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width