CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

jobs.internet.com

internet.commerce
Partners & Affiliates
Boat Donations
Promos and Premiums
Televisions
Cell Phones
Hurricane Shutters
Find Software
Imprinted Gifts
Web Design
Career Education
Shop Online
Desktop Computers
GPS Devices
Car Donations
Disney World Tickets


RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

Home >> Columns >> Programming Insights


Create and Edit Office Documents with .NET
Rating:

Dino Esposito (view profile)
May 4, 2005

Environment:  .NET

Go to page: 1  2  Next

Microsoft Office documents not only offer an easy and effective way for users to communicate and collaborate, but they also get smarter with each new release and increase connectivity with the remainder of the software universe. In some cases, though, Office applications can't replace the flexibility of custom code. Hence, the need to create and edit Office documents (mostly Word and Excel documents) programmatically exists—particularly from .NET applications.


(continued)



Depending on your skills, this task can be approached from two diverse directions. You either use .NET to write GUI or console applications that manipulate documents, or leverage the true power of the .NET Framework to embed managed functions inside existing Office documents in a way that vaguely resembles a VBA macro. This article tackles both these techniques, beginning with the second approach.

Introducing VSTO

Thanks to the Visual Studio Tools for Office (VSTO), building a Word document or an Excel spreadsheet that is bound to managed code is relatively easy. Once you have installed VSTO, a new node appears in the Visual Studio project window to let you create a new type of output. In particular, you can choose to create Excel or Word documents and Word templates.

Using VSTO has many advantages. First off, you can use all the features of Visual Studio .NET and write Office-based applications in Visual Basic .NET or C#. Needless to say, both languages are much more powerful than VBA and they open you up to the whole .NET Framework of classes.

A VSTO application also introduces a strong security concept to the world of Office—far beyond what you can get through VBA. A VSTO solution won't execute unless all the involved assemblies are explicitly full-trusted.

Creating a project with VSTO configures the working environment properly as well, adding any needed reference to the appropriate Office-related executables. What you have to do is simply open the code-behind class that's there and hook up handlers for the document's core events such as open and close.

Basics of a VSTO Application

A VSTO application is a piece of .NET code that runs inside a particular Office document. Users typically open the document and find some UI widgets to play with: dialogs or graphics. They work, update the document in a WYSIWYG fashion, and then save the results to another document (if you choose).

When you open the Office document, you find references to external .NET assemblies, the assemblies are loaded, and the _Startup function in your custom code is called. The _Startup function is defined in the auto-generated code section that VSTO inserts in the code-behind class for the document.

The code you write handles just events raised by the Word and Excel objects (such as an application or document). It does that through the standard COM event marshaling and doesn't even interfere with VBA events. (This means that VBA-equipped documents can be further extended to .NET.)

An event handler can access any .NET subsystem (for example run a query, get some data, and create a chart or perhaps populate a Word template with data coming out of a Web service).

Creating Documents Manually

What if you have a .NET application and need to automate the creation of Word or Excel documents? You need to incorporate the Office model into your existing assemblies. The code in this section illustrates how to create a Word document programmatically from within a console application.

As the first step, you add a couple of specific references to the Office and Word (or Excel) COM object model. The interop infrastructure of .NET wraps those COM libraries in a .NET assembly for you:

using Office = Microsoft.Office.Core;
using Word   = Microsoft.Office.Interop.Word;

When working with Word or Excel object models, almost everything must be an object and you must take optional parameters into account. The .NET Framework doesn't support optional parameters—there's method overloading in .NET—but defines a value that should be used in lieu of parameters you don't want to specify when making calls to COM objects. The following code creates a new instance of Word and a new document object:

object Missing    = System.Reflection.Missing.Value;
object fileToOpen = (object) @"c:\sample.doc";
object fileToSave = (object) @"c:\sample1.doc";

Word.Application app = new Word.ApplicationClass();
Word.Document doc    = new Word.Document;

The next step is opening the document. You can proceed as follows, limiting the non-null parameters to the file name:

doc = app.Documents.Open(ref fileToOpen,
      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);

What you do next depends on what you want to obtain. For this example, add some text to existing bookmarks. A bookmark is a well-known place in the document whose content can be changed programmatically. In a Word document, you create a bookmark by selecting the placeholder text and clicking on Insert|Bookmark. Imagine your sample document contains a bookmark named LetterDate that you expect to assign with the current date. You modify the content of a bookmark through a Range object:

object bookmarkName = null;
bookmarkName        = (object) "LetterDate";
Word.Range rng      = doc.Bookmarks.get_Item(ref bookmarkName).Range;

You store the new text in the Range object and re-add the bookmark to the Bookmarks collection. If the bookmark already exists, it will be replaced:

rng.Text       = DateTime.Now.ToShortDateString();
rng.Font.Bold  = 1;
rng.Font.Color = Word.WdColor.wdColorBlue;
object oRng    = rng;
doc.Bookmarks.Add("LetterDate", ref oRng);

The current date will be added in bold and blue. Following this approach, you can create a Word template, add as many bookmarks as needed, and factor out a .NET class to let you prepare and save Word documents automatically.

Go to page: 1  2  Next

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

Work With InterSystems. Not Separate Systems. Rapidly develop and deploy connectable applications.
Flash Demo: Learn how IBM Information Server Blade is easy to manage, highly scalable and efficient.
Is it time to make your move to the multi-threaded and parallel processing world? Find out!
Generate Complete .NET Web Apps in Minutes . Download Iron Speed Designer today.
Whitepaper: XML Processing in Applications--Take the Next Step


RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
Read in the VS2005 Help Section - dglienna (11/14/2007)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES