Is the VBE visible?

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Is the VBE visible?

Post by LisaGreen »

Hello everyone,

First off.... Hoping you are all well and weathering C91 okay.

Now for the question!!

I have a userform I use as an inputbox. The form is mostly for use when I'm editing in the VBE and I have code that I got from Chip Pearson's site

Code: Select all

http://www.cpearson.com/Excel/SetParent.aspx
that keeps form in the VBE.

However, sometimes I want to run code from the application and have the inputbox for display there.

Does anyone know of a way of telling which application is ***actually*** visible please?

TIA
Lisa

User avatar
Guessed
2StarLounger
Posts: 103
Joined: 04 Feb 2010, 22:44
Location: Melbourne Australia

Re: Is the VBE visible?

Post by Guessed »

Actually visible depends on whether windows are sitting in front of the desired window. That would be a lot more complicated. However you could see whether a userform is Open AND Visible by checking it's Visible property

Code: Select all

  MsgBox "Userform Visibility: " & UserForm1.Visible
  UserForm1.Show vbModeless
  MsgBox "Userform Visibility: " & UserForm1.Visible
Andrew Lockton
Melbourne Australia

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: Is the VBE visible?

Post by LisaGreen »

Hi,

It's not the userform I want to test it's whether the VBE or the application is showing so I can make the userform a child of the VBE or not.

Lisa

User avatar
Guessed
2StarLounger
Posts: 103
Joined: 04 Feb 2010, 22:44
Location: Melbourne Australia

Re: Is the VBE visible?

Post by Guessed »

This seems to work for me. It doesn't strike me as the most elegant solution but in the absence of any more knowledgeable contributions it might do.

Code: Select all

Sub VBE_Showing()
  Dim wshShell As Object, bRet As Boolean
  Set wshShell = CreateObject("WScript.Shell")
  bRet = wshShell.AppActivate("Microsoft Visual Basic for Applications")
  If bRet = True Then
    MsgBox "Showing"
  Else
    MsgBox "Not Showing"
  End If
End Sub
Andrew Lockton
Melbourne Australia

User avatar
rory
5StarLounger
Posts: 818
Joined: 24 Jan 2010, 15:56

Re: Is the VBE visible?

Post by rory »

Assuming the foreground window would be either the VBE or Excel, you could use something like:

Code: Select all

Declare PtrSafe Function GetForegroundWindow Lib "user32" () As LongPtr

Sub WhichWindow()
    If Application.Hwnd = GetForegroundWindow Then
        MsgBox "excel ui topmost"
    Else
        'assume VBE
        MsgBox "VBE topmost"
    End If
End Sub
Regards,
Rory

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: Is the VBE visible?

Post by LisaGreen »

Heya..

@Guessed
This displays "Showing" when run from the VBE and the Application. Perhaps I wasn't too clear on what was wanted. Apologies. I need to knpow from where the procedure is run. The application or the VBE.

@Rory
Thank you Rory. I will test this further. I often split my windows so I can see the VBE and the application at the same time. Which one is then topmost?

Lisa

User avatar
rory
5StarLounger
Posts: 818
Joined: 24 Jan 2010, 15:56

Re: Is the VBE visible?

Post by rory »

Whichever one is the active window - i.e. would receive keyboard input if you started typing for example.
Regards,
Rory