Using the immediate window from within the Excel desktop

Hummm, not the most robust code - you may have to flick between the VBE and the Excel window, I don't have time to develop it further, but it gets the job done for now.

Â

imwindow.JPG

Â

VBA:
  1. Private Declare Function SetParent Lib "user32" ( _
  2. ByVal hWndChild As Long, _
  3. ByVal hWndNewParent As Long) As Long
  4.  
  5. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
  6. ByVal lpClassName As String, _
  7. ByVal lpWindowName As String) As Long
  8.  
  9. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
  10. ByVal hWnd1 As Long, _
  11. ByVal hWnd2 As Long, _
  12. ByVal lpsz1 As String, _
  13. ByVal lpsz2 As String) As Long
  14.  
  15. Private Declare Function SetForegroundWindow Lib "user32" ( _
  16. ByVal hWnd As Long) As Long
  17.  
  18. Private Declare Function SetWindowPos Lib "user32" ( _
  19. ByVal hWnd As Long, _
  20. ByVal hWndInsertAfter As Long, _
  21. ByVal x As Long, ByVal y As Long, _
  22. ByVal cx As Long, _
  23. ByVal cy As Long, _
  24. ByVal wFlags As Long) As Long
  25. Public Sub GetIMWin()
  26. Dim PWHand As Long
  27. Dim CWHand As Long
  28. Dim Res As Long
  29.  
  30. '''Get excel desktop handel
  31. PWHand = FindWindowEx(Application.hWnd, 0&, "XLDESK", vbNullString)
  32. '''open VBE
  33. If Application.VBE.MainWindow.Visible = False Then
  34. Application.VBE.MainWindow.Visible = True
  35. End If
  36. '''open IM window if not open
  37. If Application.VBE.Windows("immediate").Visible = False Then
  38. Application.VBE.MainWindow.SetFocus
  39. Application.SendKeys ("!G") '''oh send keys;-(
  40. End If
  41. '''Get IM window handel
  42. CWHand = GetIM
  43. '''Set IM as child of excel desktop  window
  44. Res = SetParent(CWHand, PWHand)
  45.  
  46. Application.VBE.MainWindow.Visible = True
  47.  
  48. With Application.VBE.Windows("immediate")
  49. .Visible = True
  50. .Height = 100
  51. .Width = 400
  52. .Top = 100
  53. .Left = 400
  54.     End With
  55. End Sub
  56.  
  57. Function GetIM() As Long
  58. Dim VBEHand As Long
  59. VBEHand = FindWindow("wndclass_desked_gsk", Application.VBE.MainWindow.Caption)
  60. GetIM = FindWindowEx(VBEHand, 0&, "VbaWindow", "immediate")
  61. If GetIM = 0 Then
  62. GetIM = FindWindow("VBFloatingPalette", "immediate")
  63. End If
  64. End Function

Related posts:

  1. Selection to CSV
  2. Get Excel Instance without IDTextensibility2 in .Net
  3. Get RGB value of control at runtime

Comments

  1. simon murphy says:

    Ross
    I had a little play around with this the other day - brilliant idea.
    I can see a few uses for the window and for the technique itself.
    cheers
    Simon

  2. Ross says:

    Thanks Simon,
    It's never something that I've ever really wanted but I have hear it ask about before, so when I saw the API code somewhere I gave it a go. To be honest I used a Z order approach to begin with but that did not work, this kind does.

    cheers
    Ross

  3. simon murphy says:

    Ross
    I never wanted it till I saw it, and tried it. It so much better than clicking back and forth to the VBE. I have some plans to play around with this in the near future - I'll let you know how I get on.
    cheers
    Simon

  4. [...] That was until I found Ross’s bit of code to rip the immediate window out of VBA and put it in the Excel desktop instead. What a great idea, I wish I’d thought of it. I didn’t, but it did inspire this little form based tool. Its as tatty as, just a mega rough and ready thing that I probably wont bother to develop further. No idea if its any use, of if I’ve missed something obvious, and there is a more simple way to do this. If so, be sure to let us know via comments. [...]

  5. [...] It is a bit like a spreadsheet with a code editing window tacked below. Both Ross and I have tackled this issue with VBA in different ways in the past. Here is Ross’s code to unlock the VBAIDE Immediate window to float over the grid. [...]

Submit a Comment