EDN Admin
Well-known member
I am try embed Excel to WinForm with following code:
Imports System.Collections<br/>
Imports System.ComponentModel<br/>
Imports System.Drawing<br/>
Imports System.Data<br/>
Imports System.Windows.Forms<br/>
Imports System.Runtime.InteropServices<br/>
Imports Microsoft.Office.Interop<br/>
Imports Microsoft.Office.Interop.Excel
Public Class Form1<br/>
<DllImport("User32.dll", EntryPoint:="GetWindowLong")> _<br/>
Private Shared Function GetWindowLong(ByVal HWND As IntPtr, ByVal Index As Integer) As Int32<br/>
End Function
<DllImport("User32.dll", EntryPoint:="SetWindowLong")> _<br/>
Private Shared Function SetWindowLong(ByVal HWND As IntPtr, ByVal Index As Integer, dwNewLong As Int32) As Int32<br/>
End Function
<DllImport("user32.dll", EntryPoint:="SetWindowPos")> _<br/>
Private Shared Function SetWindowPos(ByVal hWnd As Integer, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, _<br/>
ByVal uFlags As UInteger) As Boolean<br/>
window-positioning options<br/>
End Function
<DllImport("user32.dll", EntryPoint:="MoveWindow")> _<br/>
Private Shared Function MoveWindow(ByVal hWnd As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Boolean) As Boolean<br/>
End Function
<DllImport("user32.dll")> _<br/>
Private Shared Function SetParent(ByVal hWndChild As Integer, ByVal hWndNewParent As Integer) As Integer<br/>
End Function
<br/>
Public Excel As Excel.Application = Nothing<br/>
Public Workbook As Microsoft.Office.Interop.Excel.Workbook
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click<br/>
Excel = New Excel.Application<br/>
Dim ExcelWnd As Integer = Excel.Hwnd
Workbook = Excel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet)<br/>
Workbook.Activate()<br/>
Const GWL_STYLE As Integer = -16<br/>
Const WS_POPUP As Int32 = &H80000000<br/>
Const WS_CHILD As Int32 = &H40000000<br/>
Const WS_CAPTION As Int32 = &HC00000<br/>
Const WS_SIZEBOX As Int32 = &H40000
Dim Style As Int32 = GetWindowLong(HWND, GWL_STYLE)<br/>
Style = (Style And Not (WS_POPUP)) Or WS_CHILD<br/>
<br/>
SetParent(ExcelWnd, Me.Handle.ToInt32())<br/>
Excel.Visible = True
ResizeExcel(ExcelWnd)<br/>
End Sub
Private borderWidth As Integer = SystemInformation.Border3DSize.Width<br/>
Private borderHeight As Integer = SystemInformation.Border3DSize.Height<br/>
Private captionHeight As Integer = SystemInformation.CaptionHeight<br/>
Private statusHeight As Integer = SystemInformation.ToolWindowCaptionHeight<br/>
Public Sub ResizeExcel(ExcelWnd As Integer)<br/>
Try<br/>
MoveWindow(ExcelWnd, -3 * borderWidth, -4 * borderHeight - captionHeight, Me.Bounds.Width - 20, Me.Bounds.Height - 20, True)
Catch ex As Exception
End Try
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing<br/>
Excel.Quit()<br/>
Excel = Nothing<br/>
Workbook = Nothing
End Sub
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize<br/>
ResizeExcel(Excel.Hwnd)<br/>
End Sub
End Class
after SetParent function excel set up in ReadOnly mode and i canto edit content. Could anybody help my to fix this problem.
Me not interesting ether hosting in webbrowser control nor DSOFramer solutions
<hr class="sig alex
View the full article
Imports System.Collections<br/>
Imports System.ComponentModel<br/>
Imports System.Drawing<br/>
Imports System.Data<br/>
Imports System.Windows.Forms<br/>
Imports System.Runtime.InteropServices<br/>
Imports Microsoft.Office.Interop<br/>
Imports Microsoft.Office.Interop.Excel
Public Class Form1<br/>
<DllImport("User32.dll", EntryPoint:="GetWindowLong")> _<br/>
Private Shared Function GetWindowLong(ByVal HWND As IntPtr, ByVal Index As Integer) As Int32<br/>
End Function
<DllImport("User32.dll", EntryPoint:="SetWindowLong")> _<br/>
Private Shared Function SetWindowLong(ByVal HWND As IntPtr, ByVal Index As Integer, dwNewLong As Int32) As Int32<br/>
End Function
<DllImport("user32.dll", EntryPoint:="SetWindowPos")> _<br/>
Private Shared Function SetWindowPos(ByVal hWnd As Integer, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, _<br/>
ByVal uFlags As UInteger) As Boolean<br/>
window-positioning options<br/>
End Function
<DllImport("user32.dll", EntryPoint:="MoveWindow")> _<br/>
Private Shared Function MoveWindow(ByVal hWnd As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Boolean) As Boolean<br/>
End Function
<DllImport("user32.dll")> _<br/>
Private Shared Function SetParent(ByVal hWndChild As Integer, ByVal hWndNewParent As Integer) As Integer<br/>
End Function
<br/>
Public Excel As Excel.Application = Nothing<br/>
Public Workbook As Microsoft.Office.Interop.Excel.Workbook
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click<br/>
Excel = New Excel.Application<br/>
Dim ExcelWnd As Integer = Excel.Hwnd
Workbook = Excel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet)<br/>
Workbook.Activate()<br/>
Const GWL_STYLE As Integer = -16<br/>
Const WS_POPUP As Int32 = &H80000000<br/>
Const WS_CHILD As Int32 = &H40000000<br/>
Const WS_CAPTION As Int32 = &HC00000<br/>
Const WS_SIZEBOX As Int32 = &H40000
Dim Style As Int32 = GetWindowLong(HWND, GWL_STYLE)<br/>
Style = (Style And Not (WS_POPUP)) Or WS_CHILD<br/>
<br/>
SetParent(ExcelWnd, Me.Handle.ToInt32())<br/>
Excel.Visible = True
ResizeExcel(ExcelWnd)<br/>
End Sub
Private borderWidth As Integer = SystemInformation.Border3DSize.Width<br/>
Private borderHeight As Integer = SystemInformation.Border3DSize.Height<br/>
Private captionHeight As Integer = SystemInformation.CaptionHeight<br/>
Private statusHeight As Integer = SystemInformation.ToolWindowCaptionHeight<br/>
Public Sub ResizeExcel(ExcelWnd As Integer)<br/>
Try<br/>
MoveWindow(ExcelWnd, -3 * borderWidth, -4 * borderHeight - captionHeight, Me.Bounds.Width - 20, Me.Bounds.Height - 20, True)
Catch ex As Exception
End Try
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing<br/>
Excel.Quit()<br/>
Excel = Nothing<br/>
Workbook = Nothing
End Sub
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize<br/>
ResizeExcel(Excel.Hwnd)<br/>
End Sub
End Class
after SetParent function excel set up in ReadOnly mode and i canto edit content. Could anybody help my to fix this problem.
Me not interesting ether hosting in webbrowser control nor DSOFramer solutions
<hr class="sig alex
View the full article