Results 1 to 9 of 9

Thread: Change reference at runtime

  1. #1

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Question Change reference at runtime

    Hi guys

    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

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Change reference at runtime

    Thanks JM

    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.


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Change reference at runtime

    The first tutorial is for C#. It was the title that gave it away:
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Change reference at runtime

    Thanks

    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 ?

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Change reference at runtime

    Quote Originally Posted by jmcilhinney View Post
    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.

    Also, Word specific constants used in the code should be replaced with its corresponding values. Isn't it ?


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Change reference at runtime

    My mistake

    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 ?

    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.


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width