473,394 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Word automation and late/early binding

We are developing a C# application that has many interfaces to the
Microsoft suite (eg Word, Excel, Outlook, Powerpoint, etc). We need to
support Office 97, 2000, 2002 and any future versions.

Our first cut used early binding and worked fine with Office 2002. We
then ran on a machine that had Office 2000 installed and it all turned
to custard.

There appears to be a number of options to take to support multiple
versions:

(1) Use early binding on the lowest common demoninator (in our case
office 97)
(2) Use early binding for each office version (ie in our case, have
three different assemblies for 97, 2000 and 2002)
(3) Use late binding

We would prefer to go with Option (1) as this means no code changes
(not yet clarified). However, we are not convinced that this is the
correct thing to do.

What do others think?
Nov 15 '05 #1
2 6621
Mystery Man,

Technically, you should be able to go with option #1, as COM interfaces
are supposed to be immutable. However, the implementations behind each may
have changed. If you are not making that many calls to Office, then you
might want to consider #3, because you could do it all in code without any
interop assemblies.

Avoid #2 at all costs. That is just a nightmare waiting to happen.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mystery Man" <Pr************@hotmail.com> wrote in message
news:87**************************@posting.google.c om...
We are developing a C# application that has many interfaces to the
Microsoft suite (eg Word, Excel, Outlook, Powerpoint, etc). We need to
support Office 97, 2000, 2002 and any future versions.

Our first cut used early binding and worked fine with Office 2002. We
then ran on a machine that had Office 2000 installed and it all turned
to custard.

There appears to be a number of options to take to support multiple
versions:

(1) Use early binding on the lowest common demoninator (in our case
office 97)
(2) Use early binding for each office version (ie in our case, have
three different assemblies for 97, 2000 and 2002)
(3) Use late binding

We would prefer to go with Option (1) as this means no code changes
(not yet clarified). However, we are not convinced that this is the
correct thing to do.

What do others think?

Nov 15 '05 #2
I have successfully used the sample code below to control Word 2K and XP.
This was an Extensibility project so the app was started when Word started
but you should be able to use late binding on the generic "Word.Application"
to do the same thing. The project used the Primary Interop Assemblies for
XP but they were not "installed". The required interop assemblies were
placed in the same directory as the project dll.

Specific method calls require checking the version you are running on. I
used only Open and SaveAs but others include Compare, Merge, Sort and
PrintOut.

Hope this helps.

//get the version of Word we are running
WordVersion = (string)appObj.GetType().InvokeMember("Version",
BindingFlags.GetProperty , null, appObj , null);

//add event handlers via reflection
BindingFlags oBindFlags = BindingFlags.Instance | BindingFlags.Public |
BindingFlags.NonPublic;
Type oType;
EventInfo oEvent;
MethodInfo oMethod;
object result;

// add our handlers to the events based on app
if ( WordVersion == OfficeXP ) // OfficeXP is a constant defined as 10.0
{
// the Word XP version 10.0 events
oType = typeof(Word.ApplicationEvents3_Event);

//DocumentOpen
oEvent = oType.GetEvent("DocumentOpen", oBindFlags);
oMethod = oEvent.GetAddMethod();
result = oMethod.Invoke(appObj, new object [] { new
Word.ApplicationEvents3_DocumentOpenEventHandler(t his.DocOpenEventHandler) }
);
}
else if ( WordVersion == Office2K )
{
// The Word 2000 version 9.0 events
oType = typeof(Word.ApplicationEvents2_Event);

//DocumentOpen
oEvent = oType.GetEvent("DocumentOpen", oBindFlags);
oMethod = oEvent.GetAddMethod();
result = oMethod.Invoke(appObj, new object [] { new
Word.ApplicationEvents2_DocumentOpenEventHandler(t his.DocOpenEventHandler) }
);
}

if ( WordVersion == OfficeXP )
appObj.Documents.Open(ref sFile, ref No, ref No, ref No, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
else if ( WordVersion == Office2K )
appObj.Documents.Open2000(ref sFile, ref No, ref No, ref No, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref oMissing);
else if ( WordVersion == ? )
appObj.Documents.OpenOld(...); // I assume this will open a Word97
document??
else
throw new Exception("Unknown Word Version");
--
Michael R
NO********@tampabay.rr.com

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uD**************@TK2MSFTNGP11.phx.gbl...
Mystery Man,

Technically, you should be able to go with option #1, as COM interfaces are supposed to be immutable. However, the implementations behind each may have changed. If you are not making that many calls to Office, then you
might want to consider #3, because you could do it all in code without any
interop assemblies.

Avoid #2 at all costs. That is just a nightmare waiting to happen.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mystery Man" <Pr************@hotmail.com> wrote in message
news:87**************************@posting.google.c om...
We are developing a C# application that has many interfaces to the
Microsoft suite (eg Word, Excel, Outlook, Powerpoint, etc). We need to
support Office 97, 2000, 2002 and any future versions.

Our first cut used early binding and worked fine with Office 2002. We
then ran on a machine that had Office 2000 installed and it all turned
to custard.

There appears to be a number of options to take to support multiple
versions:

(1) Use early binding on the lowest common demoninator (in our case
office 97)
(2) Use early binding for each office version (ie in our case, have
three different assemblies for 97, 2000 and 2002)
(3) Use late binding

We would prefer to go with Option (1) as this means no code changes
(not yet clarified). However, we are not convinced that this is the
correct thing to do.

What do others think?


Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: The Roys | last post by:
Hi Im doing something wrong in quitting the Word.Application in my VB program. I have General Declarations Dim AppWord As Word.Application Form_Load() Set AppWord =...
12
by: Cheval | last post by:
Has anyone had any problems with inter-office automation between MS Word and MS Access in Office 2003? I have recently installed office 2003 in a new folder and have left the older office 2000...
4
by: Daniel | last post by:
Hello, i have a problem with the word automation from c#. First, i want to mention, that i don't have any dependencies from word in my c#-project, i want to use the system.reflection model to...
4
by: Max | last post by:
Hi, I would like to have a button and a combo box with options to select various versions of Microsoft Outlook: 2002, 2003. The user selects the email client and clicks the button. The only...
4
by: Bill Coan | last post by:
NOTE: This was posted earlier to vsnet.vstools.office under a different subject line but received no response. I'm having a problem automating Word's Find object from a .NET application, using...
3
by: Charles Law | last post by:
Word has a property BuiltinDocumentProperties, which (in VBA) returns a DocumentProperties collection. In VB.NET, using Word automation, it returns a _ComObject. I have tried to cast this to...
5
by: jpr | last post by:
Hello, I have a form with a cbo which get's its data from a table. This combo returns names of MS Word files in the following path: C:\shares\files\*.dot I would like to open these files...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.