locked
Word automation RRS feed

  • Question

  •     Hi there! I need to create some report generating engine so i decided to create a word template and "on-fly" change a template's values with a right ones. The problem is that I have Microsoft Office 2007 installed on my machine but I want to create a "cross-word" application - is there any ways to include an object library into my build? Or can you describe other ways to create such application? Thank you.
    Tuesday, December 30, 2008 6:07 AM

Answers

  • I depends on how you are accessing the component object model, which is the only way to get access to the automation server.
    If you are using inferfaces only, it should not become very complicated.

    The files you need are mso.dll and msword.olb. Just try to import a reference to msword.olb, which is always located at the installation directory of MS Office, Visual Studio will do the rest for you. Or you can use tlbimp.exe on both files.

    Then try something like the following

    private bool InitWordInstance(ref Word.Application appWord) 
        try 
        {                 
            Type comType = Type.GetTypeFromProgID("Word.Application"); 
            if (comType == nullreturn false
            appWord = Activator.CreateInstance(comType) as Word.Application; 
            return appWord != null
        }                    
        catch (System.Reflection.TargetInvocationException) 
        { 
        } 
        catch (System.Runtime.InteropServices.COMException) 
        { 
        } 
         
     
        return false


    This should not be troblesome, as long as you use common functions only.

    regards,
    franking


    • Marked as answer by Kira Qian Monday, January 5, 2009 1:56 AM
    Tuesday, December 30, 2008 5:28 PM
  •  

    Hi nKognito,

    I am not sure whether I have understood your purpose. Here is an topic talking about Word automation using C#. Please look at it. http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx

    If it is not what you want, please feel free to tell me.

    Sincerely,
    Kira Qian


    Please mark the replies as answers if they help and unmark if they don't.
    • Marked as answer by Kira Qian Monday, January 5, 2009 1:56 AM
    Friday, January 2, 2009 3:41 AM

All replies

  • I depends on how you are accessing the component object model, which is the only way to get access to the automation server.
    If you are using inferfaces only, it should not become very complicated.

    The files you need are mso.dll and msword.olb. Just try to import a reference to msword.olb, which is always located at the installation directory of MS Office, Visual Studio will do the rest for you. Or you can use tlbimp.exe on both files.

    Then try something like the following

    private bool InitWordInstance(ref Word.Application appWord) 
        try 
        {                 
            Type comType = Type.GetTypeFromProgID("Word.Application"); 
            if (comType == nullreturn false
            appWord = Activator.CreateInstance(comType) as Word.Application; 
            return appWord != null
        }                    
        catch (System.Reflection.TargetInvocationException) 
        { 
        } 
        catch (System.Runtime.InteropServices.COMException) 
        { 
        } 
         
     
        return false


    This should not be troblesome, as long as you use common functions only.

    regards,
    franking


    • Marked as answer by Kira Qian Monday, January 5, 2009 1:56 AM
    Tuesday, December 30, 2008 5:28 PM
  •  

    Hi nKognito,

    I am not sure whether I have understood your purpose. Here is an topic talking about Word automation using C#. Please look at it. http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx

    If it is not what you want, please feel free to tell me.

    Sincerely,
    Kira Qian


    Please mark the replies as answers if they help and unmark if they don't.
    • Marked as answer by Kira Qian Monday, January 5, 2009 1:56 AM
    Friday, January 2, 2009 3:41 AM
  • Hi,

    Old thread, but it might help the others.

    For .NET Word automation with no dependency on MS Word application, you can try this C# Word library.

    With its .NET Word mail merge capabilities, it is ideal for these kind of situations:

    // Load template document from a DOCX file.
    var document = DocumentModel.Load("TemplateDocument.docx", LoadOptions.DocxDefault);
    
    // Create data source for mail merge.
    var data = new { FullName = "John Doe" };
    
    // Import data to the document using mail merge.
    document.MailMerge.Execute(data);
    
    // Save document to DOCX and PDF files.
    document.Save("Document.docx");
    document.Save("Document.pdf");

    Friday, September 7, 2012 10:24 AM