Solved

Add Word to winforms application

Posted on 2007-11-15
9
590 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

816 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

10 Experts available now in Live!

Get 1:1 Help Now