Hosting Excel in a WinForms application

R

RNG

Hello:

Is it possible to host Excel inside a .Net WinForms application?
My application dynamically generates an Excel workbook. I want to open
it, but don't want to launch Excel as a separate application, but rather I
want to open the workbook in a pop-up dialog from my WinForms application.

Thanks in advance!
Ram
 
R

RNG

Robin:

Thanks for your suggestion, it worked great! The only detail is that I
don't see any option for Print.
Is there any way of printing from the Office Web Components?

Thanks!
Ram
 
P

Peter Huang [MSFT]

Hi

From the OWC document, the spreadsheet did not have inside print. It is
printed when it is hosted in a web page. I think that is why it is called
Office Web Component. :)

Print a spreadsheet
To print a spreadsheet on a Web page, you must print the entire Web page.
¡¤ To print a spreadsheet from a design window, see Help in your
design program for specific instructions about printing a Web page.
¡¤ To print a spreadsheet from the browser, click Print on the
Microsoft Internet Explorer File menu.
Note If the spreadsheet is wider or longer than the printed page, the
areas that don't fit on the page are not printed. For more control over how
the data is printed, including the ability to print a long list and set
page breaks, you can export the spreadsheet to Microsoft Excel by clicking
Export to Microsoft Excel on the toolbar. For information about printing
from Excel, see Excel Help.

For your scenario, I think it is better to export the spreadsheet in xls
file and then print via Excel automation.

If you still have any concern, please feel free to post here.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang [MSFT]

Hi

Thanks for your feedback!

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
V

Vincent Melia

Is there a way to directly embed it in a Winform? It would also be great to
have the embedded Excel docs toolbars to merge with the Winforms toolbars
when activated.

I've tried a WebBrowser control but working with it was kludgy and obscure.

Any ideas?
 
M

Michel Pierron

Hi Vincent,
amusing idea; why not in a userform ?
You can try in a userform module:

Option Explicit
Private Declare Function FindWindowA& Lib _
"user32" (ByVal lpClassName$, ByVal lpWindowName$)
Private Declare Function SetParent& Lib _
"user32" (ByVal hWndChild&, ByVal hWndNewParent&)
Private Declare Function SetWindowLongA& Lib _
"user32" (ByVal hwnd&, ByVal nIndex&, ByVal dwNewLong&)
Private Declare Function MoveWindow& Lib _
"user32" (ByVal hwnd&, ByVal x&, ByVal y& _
, ByVal nWidth&, ByVal nHeight&, ByVal bRepaint&)

Private WithEvents oXL As Excel.Application
Private xlHwnd&, hwnd&

Private Sub UserForm_Activate()
DoEvents
oXL.Visible = True
SetParent xlHwnd, hwnd
SetWindowLongA xlHwnd, -16, &H140F0000
Call UserForm_Resize
oXL.Workbooks.Add
End Sub

Private Sub UserForm_Initialize()
Me.StartUpPosition = 3
hwnd = FindWindowA(vbNullString, Me.Caption)
SetWindowLongA hwnd, -16, &H84CC0080
Dim c As Object
Set oXL = New Excel.Application
oXL.Caption = "IsMine"
' Without CommandBars.
'For Each c In oXL.CommandBars
'If Not c.Name = "Cell" Then c.Enabled = False
'Next
xlHwnd = FindWindowA(vbNullString, "IsMine")
End Sub

Private Sub UserForm_QueryClose(Cancel%, CloseMode%)
On Error Resume Next
SetParent xlHwnd, 0
oXL.Visible = False
DoEvents
Me.Hide
DoEvents
Dim c As Object
For Each c In oXL.CommandBars
c.Enabled = True
Next
oXL.Quit
Set oXL = Nothing
End Sub

Private Sub UserForm_Resize()
Dim w!: w = Me.InsideWidth * 4 / 3
Dim h!: h = Me.InsideHeight * 4 / 3
MoveWindow xlHwnd, 0, 0, w, h, 1
End Sub

Regards,
MP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top