Question Microsoft Word Component

rostamiani

Member
Joined
Oct 9, 2009
Messages
15
Programming Experience
10+
Hi

Can I integrate Microsoft Word into a form as a component ?
Can Microsoft Office PIA do this ?

Thanks :)
 
You can use the WebBrowser control to host Office documents (when Office is installed).
 
Thanks a lot

How can I do that ?
Use the Navigate method.
Can I gain full access to a word document ? for example Printing, Opening files & capturing Events ?
The Rob Marsh answer seems to indicate you can get the Office application object from the browser and do regular Office automation from there. Problem with Excel inside WebBrowser control

Btw, printing you should be able to do by calling the Print method of the browser.
 
I just tested this and can verify it works, here's the VB.Net sample I used, it also has the simplification that it don't require reference to MS Internet Controls (SHDocVw).
VB.NET:
Dim ax As Object = Me.WebBrowser1.ActiveXInstance
Dim doc As Word.Document = CType(ax.GetType.InvokeMember("Document", Reflection.BindingFlags.GetProperty, Nothing, ax, Nothing), Word.Document)
As usual, requires reference to MS Word Object Library and this import:
VB.NET:
Imports Word = Microsoft.Office.Interop.Word
 
Can I print or save the documents ?
Yes, use the Office automation object model. The Word.Document has a Save method for example.
How can I access Word.Document events?
You can declare WithEvent variables and point them to the objects, or use AddHandler/RemoveHandler.
 
I just tested this and can verify it works, here's the VB.Net sample I used, it also has the simplification that it don't require reference to MS Internet Controls (SHDocVw).
VB.NET:
Dim ax As Object = Me.WebBrowser1.ActiveXInstance
Dim doc As Word.Document = CType(ax.GetType.InvokeMember("Document", Reflection.BindingFlags.GetProperty, Nothing, ax, Nothing), Word.Document)
As usual, requires reference to MS Word Object Library and this import:
VB.NET:
Imports Word = Microsoft.Office.Interop.Word

Thanks
I wanted to test it with Visual Studio 2003, But could not find Web Browser Component :confused:

Then I used VS 2008 Express

But now the code does not do anything and without any errors :confused:

This is my code :

VB.NET:
Imports Word = Microsoft.Office.Interop.Word

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ax As Object = Me.WebBrowser1.ActiveXInstance
        Dim doc As Word.Document = CType(ax.GetType.InvokeMember("Document", Reflection.BindingFlags.GetProperty, Nothing, ax, Nothing), Word.Document)

        WebBrowser1.Refresh()
    End Sub
End Class

I attached my project

Is there any Web Browser Component for VS 2003 ?
Where is th e problem ?

Thanks a lot
 

Attachments

  • Word Test.zip
    13.7 KB · Views: 43
Last edited:
But now the code does not do anything and without any errors
You haven't added any code to do anything :) The code sample was just to show you how to connect to the Word automation object from the browser. If you want to do Word automation I recommend you search "vb.net word automation" to get started.
Is there any Web Browser Component for VS 2003 ?
With VS 2003 you have to use the ActiveX browser (which is the underlying control for the managed WebBrowser added in .Net 2.0). Choose items for Toolbox where you find the MS Web Browser control in COM page, add it from Toolbox to form.
 
Thanks a lot

I found this code :)

CodeProject: ByPass difficult Automation and add applications "as is" in your .NET application. Free source code and programming help

It shows how to move any application's window to a button,Label or any other controls by changing it's handle

But it seemed that we have cannot control that application, But it's wonderful :)

This is the code
VB.NET:
Dim pinfo As New ProcessStartInfo("WINWORD")
Dim p As Process = System.Diagnostics.Process.Start(pinfo)
p.WaitForInputIdle()
SetParent(p.MainWindowHandle, Me.Label1.Handle)
 
Last edited:
I used this code to show a Word document into a Web Browser Control :

How to use the WebBrowser control to open Office documents in Visual C# 2005 or in Visual C# .NET

This command should load the document into the browser :

VB.NET:
axWebBrowser1.Navigate(strFileName, ref refmissing, ref refmissing, ref refmissing, ref refmissing);

But a File Download dialog is displayed when this command executes instead of openning the file :(

What's wrong ?
The C# 2008 progect is attached :)

This is what Should be done :

Press F5 to run the project. When you click Browse, the Open dialog box appears and allows you to browse to a Word document, an Excel worksheet, or a PowerPoint presentation. Select any file, and then click Open. The document opens inside the WebBrowser control, and a message box that displays the name of the Office document server appears.
 

Attachments

  • Word Integration 3.zip
    15 KB · Views: 33
Last edited:
For VB 2003 use this article, it explains in detail what I've already told you before: How to use the WebBrowser control in Visual Basic to open an Office document
But a File Download dialog is displayed when this command executes instead of openning the file
In this dialog you can choose Open or Save, to open in browser select Open. You can configure this behaviour in the dialog with the checkbox "Always ask before opening this type of file."
 
For VB 2003 use this article, it explains in detail what I've already told you before

Thanks a lot
I created my app with this article :D

But the error still exists

In this dialog you can choose Open or Save, to open in browser select Open.

But when I click the open button, the document opens in the microsoft word instead of the browser control !

I saw this in the article above :

By default, the 2007 Office programs do not open Office documents in the Web browser. This behavior also affects the WebBrowser control. We recommended that you use a custom ActiveX document container instead of the WebBrowser control when you develop applications that open 2007 Office documents. For more information about custom ActiveX document containers, click the following article number to view the article in the Microsoft Knowledge Base:
311765 Visual C++ ActiveX control for hosting Office documents in Visual Basic or HTML

But the article 311765 does not exists.
Do you know how can I create this container ?

Thanks
 
Last edited:
By default, the 2007 Office programs do not open Office documents in the Web browser.
It also says you can configure this, though "only if the situation requires it".
But the article 311765 does not exists.
MS has withdrawn this ActiveX (known as DsoFramer). The winwordcontrol referred to in CodeProject article looks to be a similar control.
 
Back
Top