none
Why I got this excepion 'COMException' after running few times ??

    질문

  • This is my function.

    I got COMException at this line.

    application.ActiveWindow.Selection.Paste();

    When I use this function only once , there is no problem.

    But I use this function by loop(the input parameter is always different) ,I got the exception after run this function more than twice .

    The exception isn`t happened at certain tims.

    Sometimes it occurred at second times ,sometimes it occurred at third times.

    It`s seems that the application have not got the controlled permissions with the word files.

    How can I solve it ?

    Any reply would be appreciate.

    Thank you!

    private void addTxt(string from, string to)
    {
        object missing = System.Reflection.Missing.Value;
        object oTrue = true;
        object oFormat = WdOpenFormat.wdOpenFormatText;
        var application = new Application();
        var document = application.Documents.Open(from, missing, oTrue, missing, missing, missing, missing, missing, missing, oFormat);
    
        var document2 = application.Documents.Open(to);
    
        document.Content.Copy();
    
        document2.Content.Select();
    
        Object objUnit = WdUnits.wdStory;
    
        application.Selection.EndKey(ref objUnit, ref missing);
    
        application.ActiveWindow.Selection.Paste();
    
        document2.Save();
    
        document.Close();
    
        document2.Close();
    
        application.Quit();
    
    }
    

    2012년 3월 13일 화요일 오후 1:05

답변

  • Hi,

    I would try to attach the Debugger, Enable all Exceptions and then have a Look at the Inner Exception.
    The COM Exception normally has a number associated.

    Then you can try to find the "Human readable" Error Message by that Number.

    Do a try/catch and catch the System.COMException (not System.Exception)

    Then see what's in the Message Text

    http://msdn.microsoft.com/en-us/library/9ztbc5s1.aspx

    Also you can search the Internet by the COM Errornumber to find out what's causing the Problem.

    Hope that helps.

    Greets - Helmut


    Helmut Obertanner [http://www.obertanner.de] [http://www.outlooksharp.de]

    2012년 3월 16일 금요일 오전 7:06

모든 응답

  • Hi 哇哩勒,

    Thanks for posting in the MSDN forum.

    Would you please clarify your Office version?

    And please show me the loop circle's snippet for further research.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    2012년 3월 15일 목요일 오전 4:54
  • The version of the .dll is Office12.

    My program read a text file ,the file has a lot of filename in it.

    like

    A001.TXT A002.TXT A003.TXT ......

    I want read these files and paste the context to a word file.

    The snippet is as below.

    Thank you!

    string file[];//this is read from file
    
    for(int i=0;i<file.Length;++i)
        addText(file[i],file[i]+".docx");
    
    

    2012년 3월 15일 목요일 오전 5:41
  • Hi All,

    what Document is the ActiveWindow?

    Does it have the Focus? Is a Document active, do the files exists?

    In the Loop you always create a new Instance of Word.Application() ? -> create the instance outside and pass it to the AddText function.

    I would integrate Exception-Handling, do a try/catch and maybe a Debugger.Launch() statement, then you can see where the error is.

    The files are just .txt files, no docx files. Would it not be simpler to do the string contenation using a simple Text Filereader/writer ?

    Greets - Helmut


    Helmut Obertanner [http://www.obertanner.de] [http://www.outlooksharp.de]

    2012년 3월 15일 목요일 오후 1:16
  • ActiveWindow is "document2" ,and I'm sure that the file is exist .

    I change the application to outside but the problem is still happen.

    The Exception is
    An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in wordTest.dll

    Additional information: Command Fails

    The error sometimes happened at Paste() sometimes at Save()

    I use this function not only deal txt files but doc or docx files.

    And it's very slow if I use this COM object to write , copy and paste would faster.

    Thanks for the reply.

    2012년 3월 16일 금요일 오전 6:53
  • Hi,

    I would try to attach the Debugger, Enable all Exceptions and then have a Look at the Inner Exception.
    The COM Exception normally has a number associated.

    Then you can try to find the "Human readable" Error Message by that Number.

    Do a try/catch and catch the System.COMException (not System.Exception)

    Then see what's in the Message Text

    http://msdn.microsoft.com/en-us/library/9ztbc5s1.aspx

    Also you can search the Internet by the COM Errornumber to find out what's causing the Problem.

    Hope that helps.

    Greets - Helmut


    Helmut Obertanner [http://www.obertanner.de] [http://www.outlooksharp.de]

    2012년 3월 16일 금요일 오전 7:06
  • Hi 哇哩勒,

    Do you use try/catch to get more information of your issue? I will mark Helmut's reply. It's based on my experience that his suggestion will help you. If you feel it dosn't help you, please feel free to unmark it.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us


    2012년 3월 19일 월요일 오전 6:46