Microsoft Developer Network > 포럼 홈 > .NET Base Class Library > COM reference error when working with MS Office applications using MS VS 2008
질문하기질문하기
 

답변됨COM reference error when working with MS Office applications using MS VS 2008

  • 2009년 11월 4일 수요일 오전 11:31SWClarke 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    I have been trying to write a C# program using MS VS 2008, in order to work with MS office apps (eg open/close/save Word/Excel, etc files).  I found a good site for this @ http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx and it looks fairly straightforward, but this is for using VS 2005. 

    As on the site above, I tried adding Microsoft Office 11.0 Object Library (and 10.0 and 12.0) as COM references, and this seems to work fine.  However, when I try to add Microsoft Word 12.0 Object Library as a COM reference (11.0 doesn't seem to be in the list as mentioned in the site above), it displays the error 'Could not determine the dependencies of the COM reference "Microsoft.Office.Interop.Word". Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY))'.  This is even before I try using code to access Word docs.  I have tried several variations so far, and have gone back to basics to try to get this working so the code below is fairly simple so far.  I have commented out the Word line, as if I leave it in it shows that I am missing the relevant reference, which I am trying to add as above.


    using

     

    System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Interop.Word; //added this
    using Microsoft.Office; //added this

    namespace
    WindowsFormsApplication5
    {
    class Worddoc
    {
    public static void WordClass()
    {
    //Word.Application oWord = new Word.Application();
    }}}


    I have been checking out this error, and people seem to have encountered it before (for Excel, etc), but can't see a solution that works.  Any help/support on this very much appreciated.

    Thanks.

    Stephen

답변

  • 2009년 11월 4일 수요일 오후 2:37TaylorMichaelLMVP, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    There really isn't any need to use the COM objects directly.  VS ships with the PIAs for Office already.  You can just add a reference to the PIAs via the .NET tab of Add Reference.  The Office PIAs begin with Microsoft.Office.Interop.  There should be a version of Word for v11 and v12.  If not then you might need to install the VSTO library.  I believe v11 ships with VS2008 and v12 was either an addon or added in a SP.  Can't remember.  Here's the link for the current version of VSTO: http://www.microsoft.com/downloads/details.aspx?familyid=54EB3A5A-0E52-40F9-A2D1-EECD7A092DCB&displaylang=en

    Michael Taylor - 11/4/09
    http://p3net.mvps.org
  • 2009년 11월 4일 수요일 오후 5:50TaylorMichaelLMVP, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    Add a using statement to the top of your file that includes the Microsoft.Office.Interop.Word namespace.  Then you will instantiate an instance of the ApplicationClass to interact with Word.

    Michael Taylor - 11/4/09
    http://p3net.mvps.org
  • 2009년 11월 5일 목요일 오후 6:24Marcel Roma 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨코드 있음
    Hi Stephen,

    using System;
    using Word = Microsoft.Office.Interop.Word;
    
                //[...]
    
                string saveToDirectory = @"C:\Docs";
    
                foreach(Word.Document doc in oWord.Documents)
                {
                    object targetFileName = System.IO.Path.Combine(saveToDirectory, doc.Name);
                    doc.SaveAs(ref targetFileName, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing);
                }
    
                //[...]
    
    
    I hope this helps.

    Marcel
  • 2009년 11월 9일 월요일 오전 8:46eryangMSFT, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    Hi Stephen,
    Marcel just shows his idea from above demo, you may need to do some changes on the code snippet, for instance, give different value to saveToDirectory in the foreach loop.
    For excel automation, this article may gives some help.

    Thanks,
    Eric



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

모든 응답

  • 2009년 11월 4일 수요일 오후 2:37TaylorMichaelLMVP, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    There really isn't any need to use the COM objects directly.  VS ships with the PIAs for Office already.  You can just add a reference to the PIAs via the .NET tab of Add Reference.  The Office PIAs begin with Microsoft.Office.Interop.  There should be a version of Word for v11 and v12.  If not then you might need to install the VSTO library.  I believe v11 ships with VS2008 and v12 was either an addon or added in a SP.  Can't remember.  Here's the link for the current version of VSTO: http://www.microsoft.com/downloads/details.aspx?familyid=54EB3A5A-0E52-40F9-A2D1-EECD7A092DCB&displaylang=en

    Michael Taylor - 11/4/09
    http://p3net.mvps.org
  • 2009년 11월 4일 수요일 오후 3:30SWClarke 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    Thanks for your reply Michael.  I tried doing as you suggested and started a new project, with just using the .net tab of the reference.  I saw both v11 and v12 of Microsoft.Office.Interop as you mentioned.  I then added 'using Microsoft.Office.Interop.Word;' version 12.

    However when I tried using the line 'Word.Application oWord =
    new Word.Application();' it shows the error 'The type or namespace name 'Word' could not be found (are you missing a using directive or an assembly reference?)'.

    I also saw the link that you sent to install 'VS Tools for the Office system 3.0 Runtime'.  So I checked the add/remove programs and saw a version there for VS 2005, and this 3.0 version.  So just in case I removed both and re-installed the 'VS Tools for the Office system 3.0 Runtime' from your link.  However this made no difference.

    So am I correct in using the 'Word.Application oWord =
    new Word.Application();' statement, or should this be something else to call the Word application from the .net reference?

    Stephen

  • 2009년 11월 4일 수요일 오후 5:50TaylorMichaelLMVP, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    Add a using statement to the top of your file that includes the Microsoft.Office.Interop.Word namespace.  Then you will instantiate an instance of the ApplicationClass to interact with Word.

    Michael Taylor - 11/4/09
    http://p3net.mvps.org
  • 2009년 11월 5일 목요일 오전 11:14SWClarke 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Thanks for the reply Michael.  I think that I have this fixed now with out the need to add the 'using Microsoft.Office.Interop.Word namespace'.  By the way do you mean a reference for my namespace ie I am using a namespace called WordClass, so would this be 'using Microsoft.Office.Interop.Word WordClass'?  I tried this and am getting an error 'The type or namespace name 'WordClass' does not exist in the namespace 'Microsoft.Office.Interop.Word' (are you missing an assembly reference?)'.

    At the moment, I have the code opening a new Word doc, writing some text into it, closing the file and closing Word.  The next thing that I would like to do is to identify any open Word docs (or processes) and save these open docs to a folder, so any helpful hints/ways of doign this?  I have included my C# code in VS2008 so far as below, so any help with this appreciated.

    Regards,

    Stephen


    using

     

    System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Interop.Word;
    using Microsoft.Office;
    using System.Reflection;
    namespace WordClass
    {

     

    class WordDoc
    {

     

    public static void WordClass()
    {
    //Missing Null Values
    Object oMissing = System.Reflection.Missing.Value;

     

    //True and false objects
    Object oTrue = true;
    Object oFalse = false;

     

    //Create Word and document
    Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
    Microsoft.Office.Interop.Word.
    Document oWordDoc = new Microsoft.Office.Interop.Word.Document();

     

    //Make the Word app display
    oWord.Visible = true;

     

     

    //Create a new document
    oWordDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

     

    //Insert a paragraph at the beginning of the document
    Microsoft.Office.Interop.Word.Paragraph oPara1;
    oPara1 = oWordDoc.Content.Paragraphs.Add(
    ref oMissing);
    oPara1.Range.Text =
    "Testing writing some text into the document";

     

     

    //Save file as
    Object oSaveAsFile = (Object)"C:\\SampleDoc.doc";
    oWordDoc.SaveAs(
    ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    ref oMissing, ref oMissing);

     

    //File close
    oWordDoc.Close(ref oFalse, ref oMissing, ref oMissing);

     

    //App close
    oWord.Quit(ref oMissing, ref oMissing, ref oMissing);

     

    }}}

  • 2009년 11월 5일 목요일 오전 11:33Marcel Roma 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    Use the oWord.Documents collection, loop through the collection and save each document to a folder of your choosing.

    (BTW, you could use an namespace alias to avoid typing the qualified names of Word classes.)

  • 2009년 11월 5일 목요일 오후 2:36SWClarke 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    Hi Marcel,

    Thanks for your reply. I'm not sure what you mean, so do you mean change part of my existing code or write a new section towards the bottom of the code to identify and save any open Word docs?  If a new block of code then would you mind showing how this may work.

    Yes, it is a bit akward using the fully qualified names.  I tried using '
    using Microsoft.Office.Interop.Word' as a namespace which worked but this brought other problems.  I am not too worried about using the fully qualified names foe the moment, but would like to get the first part working where it recognises open Word documents and saves them to a certain location?

    Regards,

    Stephen

  • 2009년 11월 5일 목요일 오후 6:24Marcel Roma 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨코드 있음
    Hi Stephen,

    using System;
    using Word = Microsoft.Office.Interop.Word;
    
                //[...]
    
                string saveToDirectory = @"C:\Docs";
    
                foreach(Word.Document doc in oWord.Documents)
                {
                    object targetFileName = System.IO.Path.Combine(saveToDirectory, doc.Name);
                    doc.SaveAs(ref targetFileName, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing);
                }
    
                //[...]
    
    
    I hope this helps.

    Marcel
  • 2009년 11월 6일 금요일 오후 3:00SWClarke 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Hi Marcel,

    When I include this code, it seems to be building fine, but gettign an error when I run it.  It may be something simple, so will check it out.

    Do you have similar code for saving open Excel files?

    Thanks again,

    Stephen
  • 2009년 11월 9일 월요일 오전 8:46eryangMSFT, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    Hi Stephen,
    Marcel just shows his idea from above demo, you may need to do some changes on the code snippet, for instance, give different value to saveToDirectory in the foreach loop.
    For excel automation, this article may gives some help.

    Thanks,
    Eric



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