Microsoft Office Automation in C# with Early Binding and Late Binding
Thursday, June 30, 2011 11:07If you want to automate tasks for Microsoft Office applications, for example in Microsoft Word or Microsoft Excel, you can do so from C#.
You will have to choose though, if you want use Early Binding or Late Binding.
This sample shows you how you can use both Early- and Late Binding for Office Automation by using Word and Excel.
Early Binding
With Early Binding, you create references to Office automation DLL’s in your Visual Studio Project. These references are hard; if you have Office 2010 installed on your development machine, and you want to run your program on a machine that has Office 2007 installed, the program won’t work. It will fail with a not-found-exception, because the required (referenced) DLL’s cannot be found.
Although Early Binding is fast, your program is directly linked to the specific Office version you had installed on the development machine.
In order to use Office Automation by using Early Binding, follow these steps;
First, create a reference to the proper Office Automation components. In my case, I want to use Excel and Word automation, and I have to create references to these components; (If you’re not using Office 2010, your versions will differ. Mine are ‘14.0’.)
- Microsoft Office 14.0 Object Library
- Microsoft Excel 14.0 Object Library
- Microsoft Word 14.0 Object Library
This will look like this in your project;
Now add the required ‘using’ statements in your code;
using Word = Microsoft.Office.Interop.Word; using Excel = Microsoft.Office.Interop.Excel;
Now you can start using Word and Excel from your code.
Here’s an example on how to open a Word and an Excel file. It starts the application, loads the document, closes the document and closes the application.
For Word:
object oFileName = @"C:\Test.docx"; object oMissing = System.Reflection.Missing.Value;Word.Application oWord = new Word.Application(); Word.Document oWordDoc = oWord.Documents.Open(ref oFileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); oWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);oWord.Quit(ref oMissing, ref oMissing, ref oMissing); oWord = null;
For Excel:
object oFileName = @"C:\Test.xlsx"; object oMissing = System.Reflection.Missing.Value; Excel.Application oExcel = new Excel.Application(); Excel.Workbook oWorkbook = oExcel.Workbooks.Open(oFileName, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);oWorkbook.Close(false, oMissing, oMissing); oExcel.Quit(); oExcel = null;
Now that we’ve seen Early Binding, lets check out Late Binding.
Late Binding
If you want to make sure that your program works with ‘all’ supported versions Office, you are required to use Late Binding. With Late Binding, you are not creating references to Office Automation DLL’s in your Visual Studio project. In stead, in run-time, you are using the machine’s registry on order to find the proper components. This means that if Office isn’t installed on the machine running your program, the program will generate a run-time error.
Furthermore, Late Binding is slower then Early Binding and harder to use, because you cannot use intelli-type. (So you have to know your objects, methods and properties in order to be able to use it.)
In order to use Office Automation by using Late Binding, follow these steps;
(You don’t have to create ANY references in you project. You could add a ‘using’ statement for System.Reflection.)
Here’s how to use Late Binding in Office Automation;
For Word:
object oFileName = @"C:\Test.docx"; object oWord = Activator.CreateInstance(Type.GetTypeFromProgID("Word.Application")); oWord.GetType().InvokeMember("Visible", System.Reflection.BindingFlags.SetProperty, null, oWord, new object[] { false }); object oDocs = oWord.GetType().InvokeMember("Documents", System.Reflection.BindingFlags.GetProperty, null, oWord, null); object oDoc = oDocs.GetType().InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, oDocs, new Object[] { oFileName }); oDoc.GetType().InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, oDoc, null); oWord.GetType().InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, oWord, null); oWord = null;
For Excel:
// The Globalization stuff is to work around an Office 2003 bug. object oFileName = @"C:\Test.docx"; object oMissing = System.Reflection.Missing.Value;System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US"); object oExcel = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application")); oExcel.GetType().InvokeMember("Visible", BindingFlags.SetProperty, null, oExcel, new object[] { false }, ci); object oBooks = oExcel.GetType().InvokeMember("Workbooks", BindingFlags.GetProperty, null, oExcel, null, ci); object oBook = oBooks.GetType().InvokeMember("Open", BindingFlags.InvokeMethod, null, oBooks, new object[] { oFileName, oMissing, true }, ci); oBook.GetType().InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, oBook, new object[] { false, oMissing, oMissing }, ci); oExcel.GetType().InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, oExcel, null, ci);oExcel = null;
Conclusion
Both Late Binding and Early Binding work. These appear to be the pro’s and con’s:
Early Binding
Pro’s:
- Fast
- Easy to use (intelli-type)
Con’s:
- Confined to one specific Office version
Late Binding
Pro’s:
- Works with supported Office versions
Con’s:
- Slow (because of reflection)
- Hard to use (no intelli-type)
Now you choose…
Thanks for reading!
Leave a Reply
You must be logged in to post a comment.
