Change reference at runtime
Hi guys :wave:
Is there a way to change reference at runtime ?
I am automating Word from VB.Net. I think what I have coded will be same for both Word2003 and Word2007. I used Word 12 Object Library reference(which is of Word2007).
So, I want to know how to make it compatible with a machine installed with Office2003. That is, it will check the whether the machine has 2003 installed. If so, use that reference. Otherwise use 2007.
I think the Word object has an Application.Version property which would return the version of the currently installed the Word and can be used to check the version.
I know about late binding, which is a bit tough as it has more overheads. I am currently using early binding.
Any ideas ? :)
Thanks :wave:
Re: Change reference at runtime
Referencing is done at design time, not run time. Late-binding is what you are supposed to use if you want to support multiple versions of Office applications. Just because a solution is a bit more difficult, that doesn't mean that it's not the right solution.
Re: Change reference at runtime
Thanks JM :wave:
When late binding is used, do I have to use the InovkeMember() like in this tutorial or do I have to remove only the reference and keep the rest of the code(as it is coded in the early bounded way), like in this tutorial ?
A bit confused with that. :confused:
:wave:
Re: Change reference at runtime
The first tutorial is for C#. It was the title that gave it away:
Quote:
Binding for Office automation servers with Visual C# .NET
C#, until recently, didn't support late-binding. The 'dynamic' keyword was added to C# pretty much specifically to make Office Automation easier and more like VB. You're using VB so the second tutorial is what you should be following.
Re: Change reference at runtime
Thanks :wave:
So, create the program using early binding technique. And when it is ready for distribution, remove the dependencies(references). No change in the rest of the code.
This will work regardless of 2003 or 2007 versions. Am I right ?
Re: Change reference at runtime
Yes, create the application using early binding. When you are ready to deploy, remove the reference and then make the required code changes. Those changes will involve changing all the Office-specific types to Object and then using CreateObject to create the initial Office application root object. You will obviously require Option Strict Off for late-binding to work.
This will then work with every version of Office as long as you have only used functionality supported by that version. If you use functionality specific to Office 2010 then obviously the app won't work with older versions.
I would tend to use conditional compilation to fairly easily switch between early binding and late binding. You can declare the Office variables twice, once as their specific types and once as Object. You can then switch between those two blocks by setting the the value of the compilation constant, which you would do at the same time as you add or remove the Office reference and turning Option Strict On or Off.
Re: Change reference at runtime
Quote:
Originally Posted by
jmcilhinney
I would tend to use conditional compilation to fairly easily switch between early binding and late binding. You can declare the Office variables twice, once as their specific types and once as Object. You can then switch between those two blocks by setting the the value of the compilation constant, which you would do at the same time as you add or remove the Office reference and turning Option Strict On or Off.
I didn't got this part. Can you please tell me about the unconditional complilation that you mentioned. :confused:
Also, Word specific constants used in the code should be replaced with its corresponding values. Isn't it ?
:wave:
Re: Change reference at runtime
It's conditional compilation, not UNconditional compilation. If you want to know more about it then you should look it up and read about it. If you don't understand what you find, then you can ask specific questions about that.
Re: Change reference at runtime
My mistake :D
You mean like this:
Code:
if word.version is 11 then
use all 2003 related stuff here
else if word.version is 12 then
use all 2007 related stuff here
end if
Isn't it ?
Quote:
You can declare the Office variables twice, once as their specific types and once as Object.
For late binding, we need to use Object types for all variables. But didn't got the idea of using office specific types. :confused:
:wave: