Solved

Add Word to winforms application

Posted on 2007-11-15
9
591 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
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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

735 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