Solved

Add Word to winforms application

Posted on 2007-11-15
9
594 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

617 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