Solved

Add Word to winforms application

Posted on 2007-11-15
9
589 Views
Last Modified: 2013-11-26
Hi,

I have a listbox on my winforms application. This listbox loads all my available files for viewing from a specific directory. I want to add a Word document on the right side of my application (form1) and then every time a user clicks on a file in the listbox it opens the file on the right hand side in Word.

How do I do that? and is it possible?
0
Comment
Question by:ALawrence007
  • 5
  • 3
9 Comments
 
LVL 8

Expert Comment

by:Wikkard
ID: 20294023
I dont think that you can embed a word editor onto a form.
I might be wrong.
0
 
LVL 8

Assisted Solution

by:Wikkard
Wikkard earned 450 total points
ID: 20294037
You could however automate an instance of word to open the file easily enough.
0
 
LVL 8

Expert Comment

by:Wikkard
ID: 20294081
Add a reference to Microsoft.Word (COM).
Then you can create an instance of word and open your document in it.
ie.
[C#]
 Microsoft.Office.Interop.Word._Application oWord = new ApplicationClass();
            try
            {
                object filename = this.txtWordDoc.Text;
                oWord.Visible = false;
               
              //  oWord.Activate();
                oWord.Documents.Open(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
catch (Exception ex)

{

}

ill convert it to VB for you

             

0
 
LVL 10

Assisted Solution

by:vadim63
vadim63 earned 50 total points
ID: 20294192
You can use the WebBrowser control. Something like:
WebBrowser1.Navigate("c:\test.doc")
Look here:
http://support.microsoft.com/default.aspx?scid=kb%3bEN-US%3b304643
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 8

Accepted Solution

by:
Wikkard earned 450 total points
ID: 20294222
The reference is called "Microsoft Word 11.0 Object Library" the version might be different for you.



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim oWord As Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application
        Dim filename As String = "c:\oct.doc"
        Dim missing As Object = System.Reflection.Missing.Value
        oWord.Visible = True
        oWord.Documents.Open(filename, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
        oWord.Activate()

        Dim rc As MsgBoxResult = MsgBox("Do you want to close word ? ", MsgBoxStyle.YesNo)

        If rc = MsgBoxResult.Yes Then
            oWord.ActiveDocument.Close(missing, missing, missing)
            oWord.Quit()
        End If


    End Sub
0
 

Author Comment

by:ALawrence007
ID: 20294712
Wow,

These are all great suggestions. One more question: If I want to add settings i.e open in read only mode? Or something like that?

If I open a XML file, can I apply the stylesheet this way? With the settings?
0
 
LVL 8

Assisted Solution

by:Wikkard
Wikkard earned 450 total points
ID: 20295687
You definitely can do both of those thing.

Notice in the code above we use a variable called missing alot. It's just a placeholder for the missing parameters. You need to tell the open method to open read only.

Just change the line
oWord.Documents.Open(filename, missing, true, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)

Ill find out how to do XSL transform.
0
 

Author Comment

by:ALawrence007
ID: 20300473
I would appretiate the XSL transform question.

Thanks
0
 

Author Closing Comment

by:ALawrence007
ID: 31409469
Wikkard, Based on what you gave me I found my solution.
vadim63, your suggestion was also helpful.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now