Library code snippets
Excel inside MDI
By nzjonboy, published on 29 Mar 2002
You will need to add a reference to Microsoft Excel.
Add an MDI window to your project and set it as the starting window. Then add this code and run it.
Add an MDI window to your project and set it as the starting window. Then add this code and run it.
Option Explicit
Private Const GWL_STYLE = (-16)
Private Const WS_DLGFRAME = &H400000
Private Const WS_CHILD = &H40000000
Private Const WS_CHILDWINDOW = (WS_CHILD)
Private Const WS_VSCROLL = &H200000
Private Const WS_CAPTION = &HC00000
Private Const WS_BORDER = &H800000
Private Const WS_THICKFRAME = &H40000
Private Const WS_SIZEBOX = WS_THICKFRAME
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function GetParent Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function SetParent Lib "user32" _
(ByVal hWndChild As Long, _
ByVal hWndNewParent As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Declare Function MoveWindow Lib "user32" _
(ByVal hwnd As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal nWidth As Long, _
ByVal nHeight As Long, _
ByVal bRepaint As Long) As Long
Private Declare Function SetWindowPos Lib "user32" _
(ByVal hwnd As Long, _
ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, _
ByVal cx As Long, _
ByVal cy As Long, _
ByVal wFlags As Long) As Long
Private Declare Function FindWindowEx Lib "user32" _
Alias "FindWindowExA" _
(ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
Private WithEvents mobjXL As Excel.Application
Private mlngXLHwnd As Long
Private Sub MDIForm_Load()
Dim lngStyle As Long
Dim c As Object
Dim lngStatusBar As Long
Set mobjXL = New Excel.Application
mobjXL.Caption = "10001"
mobjXL.Visible = True
For Each c In mobjXL.CommandBars
c.Enabled = False
Next
mlngXLHwnd = FindWindow("XLMAIN", "10001")
SetParent mlngXLHwnd, Me.hwnd
lngStyle = GetWindowLong(mlngXLHwnd, GWL_STYLE)
lngStyle = lngStyle Xor WS_CAPTION
lngStyle = lngStyle Xor WS_SIZEBOX
SetWindowLong mlngXLHwnd, GWL_STYLE, lngStyle
MoveWindow mlngXLHwnd, 0, 0, _
(Me.ScaleWidth / Screen.TwipsPerPixelX), _
(Me.ScaleHeight / Screen.TwipsPerPixelY), 1
mobjXL.Workbooks.Add
End Sub
Private Sub MDIForm_QueryUnload(Cancel As Integer, UnloadMode As Integer)
SetParent mlngXLHwnd, 0
DoEvents
mobjXL.Quit
Set mobjXL = Nothing
End Sub
Private Sub MDIForm_Resize()
If Not Me.WindowState = vbMinimized Then
MoveWindow mlngXLHwnd, 0, _
(Picture1.Height / Screen.TwipsPerPixelY), _
(Me.ScaleWidth / Screen.TwipsPerPixelX), _
(Me.ScaleHeight / Screen.TwipsPerPixelY), 1
End If
End Sub
Private Sub mobjXL_WindowResize(ByVal Wb As Excel.Workbook, ByVal Wn As Excel.Window)
MoveWindow mlngXLHwnd, 0, 0, _
(Me.ScaleWidth / Screen.TwipsPerPixelX), _
(Me.ScaleHeight / Screen.TwipsPerPixelY), 1
End Sub
Related articles
Related discussion
-
VB6 system conversion using VBA to Word 2007
by b.macgregor@vodamail.co.za (0 replies)
-
How to open .bat application from excel VBA or VB6
by NaseemAhmed (0 replies)
-
Outlook VBA query
by James Crowley (1 replies)
-
Excel 2007 Error : ' the digital signature is deleted when Excel users tries to save the Master Excel Spreadsheet.'
by jdierkes (0 replies)
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
Brilliant example that this is, be warned!
With Excel 2000 under Windows 98 (I havent tested it with any other OS combos but I would have thought it was the same) this sample turns the toolbars and menus off permanently. e.g. if you go to open excel after running this example you get a spreadsheet and thats it, no amount of right clicking etc will let you turn the toolbars back on.
How to get round it...
Open up a VB project and copy the code from the MDIFormload into the formload of a new VB form. Alter the code so the excel toolbars are set to true and the border and caption bits of the window long are set. Run the form et voila! Sanity restored and excel back to normal.
Quite why M$ saves the windowstate like this is beyond a simple soul like me, but theres probably a method in their madbess...
cheers
Mike
This thread is for discussions of Excel inside MDI.