Click to Rate and Give Feedback
Related Articles
Here the author introduces SQL Server Data Services, which exposes its functionality over standard Web service interfaces.

By David Robinson (July 2008)
Here the author answers questions regarding the Entity Framework and provides an understanding of how and why it was developed.

By Elisa Flasko (July 2008)
Here we present techniques for programmatic and declarative data binding and display with Windows Presentation Foundation.

By Josh Smith (July 2008)
Systems that handle failure without losing data are elusive. Learn how to achieve systems that are both scalable and robust.

By Udi Dahan (July 2008)
More ...
Articles by this Author
Presented here is the LINQ Enumerable class, which allows you to manipulate data in any class that implements IEnumerable(Of T).

By Ken Getz (July 2008)
LINQ to XML and the Microsoft SDK for Open XML Formats simplify access to the parts of a 2007 Office system Open XML document when retrieving or modifying data, resulting in shorter, less complex code.

By Ken Getz (March 2008)
This month Advanced Basics flaunts the power of generics and reflection and shows how you get more flexible and efficient development by combining the two.

By Ken Getz (January 2008)
Ken Getz prepares Visual Basic developers to use RibbonX.

By Ken Getz (June 2007)
This month Ken Getz writes a demo-creation system for Windows-based applications, which he calls a switchboard.

By Ken Getz (December 2006)
The System.Array and System.Collections.Generic.List classes provide methods that let you avoid writing code to loop through every element of an array or list to find the items you’re looking for. Ken Getz explains.

By Ken Getz (September 2006)
The last time I wrote this column (March 2006), I shared an application that allows you to update all the Microsoft® Word documents in a folder and its subfolders. Each time the application finds a document in the specified path, it updates the document properties to match those you specified in the application.

By Ken Getz (June 2006)
At the beginning of another lovely day of writing courseware in mad pursuit of unrealistic deadlines, I received a frantic call from a business partner. He was at the end of a long consulting project and had several hundred Microsoft® Word documents, all of which required their document properties to be set identically, except the Title property of the document, which was to be based on the document file name, minus the .

By Ken Getz (March 2006)
More ...
Popular Articles
See how to build a document-level Visual Studio Tools for Office customization and integrate it with a content type in SharePoint.

By Steve Fox (May 2008)
Learn how you can peer-enable business applications by allowing them to share state in a serverless peer network.

By Kevin Hoffman (July 2008)
Mike Volodarsky demonstrates the IIS 7.0 extensibility model by extending the Response Modification into a configurable Web server module and a custom management page for IIS Manager.

By Mike Volodarsky (Launch 2008)
Performance problems can creep into your Web app as it scales up, and when they do, you need to find the causes and the best strategies to address them.

By Richard Campbell and Kent Alstad (April 2008)
More ...
Read the Blog
There are many things called threat modeling. Rather than argue about which is "the one true way," a good practice is to consider your needs and what your skills, abilities, and schedules are, and then work with a method that's best for you. In the July 2008 issue of MSDN Magazine, ...
Read more!
Want to develop games for Xbox Live? Want to get paid for it, too? Click on over to the XNA Team Blog to learn more about their initial rollout of the XNA Creators Club for XNA Game Studio. ...
Read more!
The Microsoft Entity Data Model (EDM), based on Dr. Peter Chen's Entity Relationship (ER) model, is the driving force behind the ADO.NET Entity Framework. The EDM is also the feature that most significantly differentiates the Entity Framework from other ORM-style technologies in the marketplace. In the July 2008 issue of MSDN ...
Read more!
System.IO.File is a handy helper class for reading and writing data, but its methods support only synchronous operation. Is there an easy way to provide File’s functionality for asynchronous file I/O? In the July 2008 issue of MSDN Magazine, Stephen Toub walks through several ...
Read more!
Remember .NET Terrarium, the interactive game meant to introduce .NET development techniques? Well, the Windows SDK team has released the source code for .NET Terrarium 2.0 on CodePlex. You can read more about this release on the Windows SDK blog and at Microsoft ...
Read more!
The Enumerable class plays an important role in every LINQ query you create. Because the Enumerable class's extension methods can process many other classes—including Array and List—you can use methods of the Enumerable class not only to create LINQ queries, but also to manipulate the behavior of arrays and other data structures. In the July 2008 issue of MSDN ...
Read more!
More ...
{ End Bracket }
C# and VBA: Like Oil and Water
Ken Getz


Some things just don't mix as well as you would like. Take C# and Microsoft® Excel 2003 or Word 2003, for example. Not only are these applications huge productivity tools, but they both also provide access to large object models that you can program against from your own applications. The problem is none of the documentation for the object models includes code samples for developers writing managed code, and writing Visual Basic® for Applications (VBA) code is quite unlike writing C# code.
I spent much of summer 2004 creating managed code samples exercising many of the methods of both Word and Excel, providing samples in both C# and Visual Basic .NET. The task seemed simple—400 code snippets, all converted from Visual Basic .NET to C#—but was much harder than you might imagine.
It's not that it's difficult to convert code from Visual Basic .NET to C#, or to simply create code that automates Word or Excel in C#. What caused me pain in this particular project was that when you attempt to communicate with objects designed to be consumed by VBA clients, you're living in a world that didn't take C# and its design goals into account when it was being created. One thing is for sure: because of its VBA lineage, it's a lot easier accomplishing Office automation from Visual Basic .NET than it is from C#. I can't dig into the details here, so I'll just point out a few delectable tidbits, with links to more info later on.
From my perspective, the least pleasant issue is that of passing parameters to Word members. Before taking Word and its design to task, remember that it started its career including Word Basic rather than VBA, back in the Pleistocene era of computing. In those days, it was typical for scripting languages to accept and expect all parameters to be passed by reference, such that incoming parameters were simply full-featured aliases of the original variables, and this habit remains unbroken in Word to this day (a lot of design decisions are also a relic of designing for COM).
For the most part, all parameters must be passed by reference when calling a method in Word, so code that looks like the following Visual Basic .NET snippet needs to be converted for use in C#:
' Visual Basic .NET
' wdApp refers to the Word Application object.
wdApp.Windows.Arrange(Word.WdArrangeStyle.wdTiled)

// C#
object value = Word.WdArrangeStyle.wdTiled;
wdApp.Windows.Arrange(ref value);
It's not a terrible burden, but it does force you to create a variable each and every time you want to pass a parameter.
Optional parameters provide separate challenges when working with both Excel and Word. When programming Excel, you simply have to accept the fact that although VBA welcomes optional parameters, C# doesn't handle them in the same way (for an explanation of why this is the case, you can take a look at Anders Hejlsberg's TechEd 2004 discussion of the subject at Whiteboard with Anders Hejlsberg). Many Excel methods take advantage of optional parameters to ease the burden for VBA developers, and you must supply a value for each of the missing parameters. Luckily, the Type.Missing value indicates to the Primary Interop Assembly (PIA) that your code intends to accept the default value for the optional parameter.
Passing optional parameters to Word adds an extra challenge because they're passed by reference. Luckily, the PIA takes care of marshaling the values, so you needn't worry about "by reference" side effects passing a single variable to handle all the optional parameters. The following snippets demonstrate a technique for handling optional parameters when programming against the Word object model:
' Visual Basic .NET
' wdApp refers to the Word Application object.
ThisApplication.Documents.Open("C:\Test\MyNewDocument")

// C#
object filename = @"C:\Test\MyNewDocument";
object missing = Type.Missing;
wdApp.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);
Of course, there are more issues and many subtleties. For more information, check out a longer article I wrote previously on the topic (Programming Office Applications Using Microsoft Visual C#), and for the real scoop, you should take a look at Eric Carter's blog (Type.Missing, C#, and Word). He's the expert on this particular subject.

Ken Getz is a senior consultant with MCW Technologies. He is coauthor of ASP .NET Developers Jumpstart (Addison-Wesley, 2002), Access Developer's Handbook (Sybex, 2001), and VBA Developer's Handbook, 2nd Edition (Sybex, 2001). Reach him at keng@mcwtech.com.

Page view tracker