• 0

[C#]Creating Word Docs


Question

Yes, I've tried google, but none of them seem to work. I'm relatively a noob at programming so some help would be appreciated.

VS2008

MOffice2010 BETA

in case it matters.

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

ok, i dont have visual studio (or word for that matter) installed on this pc, so mostly from memory in c#, something like...

using Microsoft.Office
using system.reflection

private void button_click(object sender, system.eventargs e)
{
word.application wordapp = new word.application();
word.document worddoc = new word.document();

wordapp.visible = true;
worddoc = wordapp.documents.add(missing.value, missing.value, missing.value, missing.value);
}

i believe that should launch a new word instance and create anew document, job done! :D

obviously you will need to add the correct references in to your application for this to work (as well as a button called 'button')

i would do some googling for "c# word automation", theres lots on the net, good luck

P.S. .net is case sensitive, and the code i have written above is certainly not correct.. copying and pasting will not work.

Link to comment
Share on other sites

  • 0
i believe that should launch a new word instance and create anew document, job done!
Yuck, don't do OLE automation, that's soooo 1990's (NOTE: there are instances where automation is required: creating PDF files, for example).

Office 2007 and 2010 use OpenXML files, which are simply a bunch of XML files wrapped into a ZIP container. They've also made available the OpenXML SDK, which allows you to create Office files in .NET without ever opening an Office application. It's super-fast and relatively easy to do. And the computer creating OpenXML files doesn't even need to have Office installed since you're directly creating the files.

Get started here.

Here's a Hello World example using the OpenXML SDK (taken from the blog of Brian Jones, an Office developer at Microsoft):

public void HelloWorld(string docName)

{
  // Create a Wordprocessing document.
  using (WordprocessingDocument package = WordprocessingDocument.Create(docName, WordprocessingDocumentType.Document))
  {
	// Add a new main document part.
	package.AddMainDocumentPart();

	// Create the Document DOM.
	package.MainDocumentPart.Document =
	  new Document( 
		new Body( 
		  new Paragraph( 
			new Run( 
			  new Text("Hello World!")))));

	// Save changes to the main document part.
	package.MainDocumentPart.Document.Save();
  }
}

Edited by boogerjones
Link to comment
Share on other sites

  • 0

im not really sure but cant you just create normal text files and end them with .docx instead of .txt?

EDIT: nevermind this doesnt work

Edited by shooter468
Link to comment
Share on other sites

  • 0
Yuck, don't do OLE automation, that's soooo 1990's (NOTE: there are instances where automation is required: creating PDF files, for example).

Office 2007 and 2010 use OpenXML files, which are simply a bunch of XML files wrapped into a ZIP container. They've also made available the OpenXML SDK, which allows you to create Office files in .NET without ever opening an Office application. It's super-fast and relatively easy to do. And the computer creating OpenXML files doesn't even need to have Office installed since you're directly creating the files.

Get started here.

Here's a Hello World example using the OpenXML SDK (taken from the blog of Brian Jones, an Office developer at Microsoft):

public void HelloWorld(string docName)

{
  // Create a Wordprocessing document.
  using (WordprocessingDocument package = WordprocessingDocument.Create(docName, WordprocessingDocumentType.Document))
  {
	// Add a new main document part.
	package.AddMainDocumentPart();

	// Create the Document DOM.
	package.MainDocumentPart.Document =
	  new Document( 
		new Body( 
		  new Paragraph( 
			new Run( 
			  new Text("Hello World!")))));

	// Save changes to the main document part.
	package.MainDocumentPart.Document.Save();
  }
}

good stuff, i must say i have never worked with 2007 or 2010 in a brand new solution/project so not had experience with open xml.

now i know for the future, ta! :D

Link to comment
Share on other sites

  • 0
good stuff, i must say i have never worked with 2007 or 2010 in a brand new solution/project so not had experience with open xml.

now i know for the future, ta! :D

Yeah, it's pretty sweet, but a couple of things to note:

A) The OpenXML 2.0 SDK is not licensed for production use and will not be until the release of Office 2010. That probably doesn't matter for you but does if you distribute any software.

B) Although the example I posted demonstrates creating a file from scratch, it's almost always better to create a template file with the Office application, save it, and then use the OpenXML SDK to manipulate the template file. That way you don't have to create all the styles, themes, fonts, views, sheets, properties, etc every time you programmatically create a file. Most of the blog and MSDN examples will use this tactic.

C) Learn LINQ, specifically LINQ to XML, as it will be invaluable for OpenXML development.

D) The new SDK includes the "Open XML SDK 2.0 Productivity Tool." Use it; it saves tons of time by spitting out the code required to reconstruct a particular Office file. It's included with the "OpenXMLSDKTool.msi" file that you can download with the SDK.

Link to comment
Share on other sites

This topic is now closed to further replies.