473,230 Members | 4,189 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,230 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 6613
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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.