none
can we load Microsoft.Office.Interop.Word dynamically and create Word Document in c# or Vb.net RRS feed

  • Question

  • Hi EveryOne,

    can we load Microsoft.Office.Interop.Word dynamically and create Word Document in c# or Vb.net .

    I would like to add dynamically and open word document and create word documents.

    Is it possible ? If yes please provide me the links.

    I loaded interop dynamically , but unable to create instances and open word doc.. etc.

    many Thanks


    s.giribabu

    • Moved by Mike Feng Thursday, June 6, 2013 9:29 AM
    Wednesday, June 5, 2013 12:00 PM

Answers

  • Why?  Unless you need functionality that is available only in newer versions of Office and can somehow emulate it in older versions then you should just target the minimum version that you need.  For example you can target Office 2010 and your code will work with 2013 and above as well.  If you were doing per-version coding then this is what your code would look like (not using real type names):

    IOffice2010 ifc = new Office2010();
    ifc.Some2010Method();

    IOffice2013 ifc = new Office2013();
    ifc.Some2013Method();

    You'd have to dynamically generate all of this which is both time consuming and error prone.  However since Office interfaces are backwards compatible you can just as easily add a ref to the 2010 PIA for Office and work with Office 2010 or 2013 like so:

    IOffice ifc = new Office();
    ifc.SomeMethod();

    That is one of the big benefits of COM-based interfaces. If you need to expose optional functionality in newer versions of Office then you can reference the newer PIA but then use exception handling to detect whether you can use it or not. For example:

    IOffice ifc = new Office();
    IOfficeNew ifcNew = ifc as IOfficeNew;
    if (ifcNew != null)
       //New stuff available

    If you really want to dynamically load the PIAs then you can use Assembly.Load but you cannot reference any of the interfaces or call any of the methods directly as they won't be available at compile time.  For dynamically generating code you can use various approaches including the DOM but I think it is overkill.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Thursday, June 6, 2013 1:58 PM

All replies

  • Hi EveryOne,

    can we load Microsoft.Office.Interop.Word dynamically and create Word Document in c# or Vb.net .

    I would like to add dynamically and open word document and create word documents.

     

    Is it possible ? If yes please provide me the links.

     

    I loaded interop dynamically , but unable to create instances and open word doc.. etc.

     

    many Thanks

     


    s.giribabu

    Wednesday, June 5, 2013 12:02 PM
  • You don't need to load the interop dynamically.  Just add a reference to the interop and it will get loaded if you actually try to use it.  .NET does delay loading of DLLs automatically. 

    You can then use the standard Office classes in the assembly to do whatever you want: create docs, edit, etc.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Wednesday, June 5, 2013 1:56 PM
  • At the risk of stating the obvious, the interop only works if Word is actually installed on the system, and I believe the supported way to do this is to use the proper Office PIAs, this kind of thing:

    http://www.microsoft.com/en-us/download/details.aspx?id=3508

    It sounds like you want the late-binding interface. Word has always been scriptable, and that doesn't use any type of interop at all. There are some examples here:

    http://stackoverflow.com/questions/13339282/how-can-i-open-a-word-document-by-using-createobject-in-c

    http://www.codeproject.com/Articles/280279/Word-Automation-using-Late-binding-usage-of-dynami

    and anything you find that tells you to create a reference to a Dll is incorrect if you really want late binding.


    Phil Wilson

    • Proposed as answer by Riquel I'm Wednesday, June 5, 2013 7:08 PM
    Wednesday, June 5, 2013 3:57 PM
  • You can use OpenXML SDK to create the word documents without using office automation. This means that there is no need to install Office applications in your PC. I didn't use it, just heard this from my friend. 

    Have a look at the following link for your reference: 

    http://msdn.microsoft.com/en-us/library/office/gg490656(v=office.14).aspx


    Best regards,
    Riquel

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 5, 2013 7:11 PM
  • My requirement is to add(load) Interop dynamically based on version of office installed in the machine.

    Many Thanks

    Giri


    s.giribabu

    Thursday, June 6, 2013 5:06 AM
  • Why?  Unless you need functionality that is available only in newer versions of Office and can somehow emulate it in older versions then you should just target the minimum version that you need.  For example you can target Office 2010 and your code will work with 2013 and above as well.  If you were doing per-version coding then this is what your code would look like (not using real type names):

    IOffice2010 ifc = new Office2010();
    ifc.Some2010Method();

    IOffice2013 ifc = new Office2013();
    ifc.Some2013Method();

    You'd have to dynamically generate all of this which is both time consuming and error prone.  However since Office interfaces are backwards compatible you can just as easily add a ref to the 2010 PIA for Office and work with Office 2010 or 2013 like so:

    IOffice ifc = new Office();
    ifc.SomeMethod();

    That is one of the big benefits of COM-based interfaces. If you need to expose optional functionality in newer versions of Office then you can reference the newer PIA but then use exception handling to detect whether you can use it or not. For example:

    IOffice ifc = new Office();
    IOfficeNew ifcNew = ifc as IOfficeNew;
    if (ifcNew != null)
       //New stuff available

    If you really want to dynamically load the PIAs then you can use Assembly.Load but you cannot reference any of the interfaces or call any of the methods directly as they won't be available at compile time.  For dynamically generating code you can use various approaches including the DOM but I think it is overkill.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Thursday, June 6, 2013 1:58 PM
  • Just have a look at late Binding from PhilWinson's answer. This can satisfy your requirement. 

    Best regards,
    Riquel

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Riquel I'm Thursday, June 6, 2013 4:44 PM
    Thursday, June 6, 2013 4:43 PM