Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open a Word document

Status
Not open for further replies.

liorlankril

Programmer
Nov 5, 2005
46
IL
I want to know how to open Word application. (not a spesific word file, I want to open the word application - new document)
 
Do the following:

1. Click the Start button on your desktop
2. Navigate to All Programs --> Microsoft Office 2003
3. Click 'Microsoft Word 2003'

...whats that, you meant you wanted to open a Word file from a C# class?
 
so..... as I understand there is no option to do this???

my code is Web Part in c#
 
The First Step in manipulating Word in .NET is that you'll need to add a COM reference to your project by right clicking in the solution explorer on References->Add Reference. Click on the COM tab and look for the Microsoft Word 9.0 Object Library. Click Select and OK.
This will automatically place an assembly in your application directory that wraps COM access to Word. Now we are ready to create a word object in our code. To instantiate an instance of a Word application, you just declare the line below in your class:

private Word.ApplicationClass WordApp = new Word.ApplicationClass();

Now you can call the interesting methods and properties that Microsoft Word provides to you to manipulate documents in Word. Personally, I never want to figure the Microsoft Word code out myself, because the Word hierarchy and properties, although rich in features, has a bit of a learning curve. So in order to jump straight over the learning curve, I simple turn the Macro Recorder on in Word and let Word write the code for me. (Of course this is VBA code, but...close enough). To start the macro recorder in Word, go to Tools->Macro->Record New Macro inside Microsoft Word. Now anything you type, open, delete, or format, will get recorded in VBA, so you have a clue how to write your Word Interoperability code.

The application in this particular article is divided into two methods based on the button that is pressed in the form. The first button opens an existing Word Document and adds a copyright line to the document. The second button creates a new document, and brings up a dialog to allow you to enter a title to the document.

Below is the code for the first button event handler:

private void button1_Click(object sender, System.EventArgs e)
{
// Use the open file dialog to choose a word document
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
// set the file name from the open file dialog
object fileName = openFileDialog1.FileName;
object readOnly = false;
object isVisible = true;
// Here is the way to handle parameters you don't care about in .NET
object missing = System.Reflection.Missing.Value;
// Make word visible, so you can see what's happening
WordApp.Visible = true;
// Open the document that was chosen by the dialog
Word.Document aDoc = WordApp.Documents.Open(ref fileName, ref missing,ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible);
// Activate the document so it shows up in front
aDoc.Activate();
// Add the copyright text and a line break
WordApp.Selection.TypeText("Copyright C# Corner");
WordApp.Selection.TypeParagraph();
}
}

The code above opens a document based on the file chosen in the OpenFileDialog. Notice the parameters passed to the Document.Open method are all references. Don't ask me why it was done this way, but it seems like when Microsoft goes through a call with optional parameters, they had no choice but to make everything a variant so on the C# side of the parameter list looks like a bunch of references to objects. If you want to skip over a parameter in the call to Open, use the System.Reflection.Missing.Value and assign it to an object. Once you know this trick, the rest of the interoperability used with Word is pretty straightforward.

Below is the code for creating a document from scratch:

private void button2_Click(object sender, System.EventArgs e)
{
// Use the custom dialog to get the title of the document from the user
TitleQuery theQueryDialog = new TitleQuery();
if (theQueryDialog.ShowDialog() == DialogResult.OK)
{
// vba code generated from recorded macro to "remind me" how to do it.
// *************************************************************
// Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
// Selection.Font.Bold = wdToggle
// Selection.TypeText Text:="Creating a Title"
// Documents.Add Template:="C:\My Documents\CSharp Book Project\Normal.dot", _
// NewTemplate:=False, DocumentType:=0
// *************************************************************
// Set up all the parameters as generic objects so we can pass them in Documents.Add
object missing = System.Reflection.Missing.Value;
object fileName = "normal.dot"; // template file name
object newTemplate = false;
object docType = 0;
object isVisible = true;
// Create a new Document, by calling the Add function in the Documents collection
Word.Document aDoc = WordApp.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);
// need to see the created document, so make it visible
WordApp.Visible = true;
aDoc.Activate();
// Global Constant enumerations are members of Word and can be assigned to properties
// Set alignment to the center of the document
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
// Toggle the title to a Bold Font
WordApp.Selection.Font.Bold = (int)Word.WdConstants.wdToggle;
// Type the Text of the Title that was inputted by the user in the Custom Dialog
WordApp.Selection.TypeText(theQueryDialog.Title);
}
}


 
Can I do it in .NET (c#) or it can be done only in VB.

If yes... can i do it as a Web Part file??
 
Yes you can do it in C# as well ... Mibble's example is in C# !

Yes you can do it on web applications as well, if you meant this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top