locked
Setting a text causes "Error HRESULT E_FAIL has been returned from a call to a COM component" RRS feed

  • Question

  • Hi,

    I'm porting some source code (C/C++) formating macros from VS6 to VS2005. All in all it seemed to work fine after it was adapted to the VS2005 DTE objects. But I'm experiencing a strange error while setting a commented text to the current selection. The following sample procedure shows what happens:

     

    Test procedure

    Public Sub TestError()

    Dim selection As TextSelection

    selection = DTE.ActiveDocument.Selection

    '

    ' here I would do get the selected text into a String

    ' next I would do some operaitons on the String

    ' next I would want to replace the selected text with the processed String

    '

    Try

    selection.Text = "a, b" & vbNewLine ' ok

    selection.Text = "//a b" & vbNewLine ' ok

    selection.Text = "/* a b */" & vbNewLine ' ok

    selection.Text = "//a, b" & vbNewLine ' fails with opened project

    selection.Text = "/* a, b */" & vbNewLine ' fails with opened project

    Catch ex As System.Exception

    Call MsgBox( _

    ex.Message() & vbNewLine & _

    ex.Source() & "." & ex.TargetSite.ToString() & vbNewLine & _

    "(" & ex.ToString() & ")", _

    MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly)

    selection.Text = vbNewLine & _

    "+" & ex.Message() & vbNewLine & _

    "+" & ex.Source() & " -> " & ex.TargetSite.ToString() & vbNewLine & _

    "(" & ex.ToString() & ")"

    End Try

    End Sub

     

    When I run this procedure on a C/C++ source file (.c, .cpp, .h) with NO OPEN PROJECT (a solution is always present) it executes successfully, setting the text as expected:

    correct output

    a, b

    //a b

    /* a b */

    //a, b

    /* a, b */

     

     

    When I run this procedure on a C/C++ source file (.c, .cpp, .h) with AN OPEN PROJECT it fails with an exception "Error HRESULT E_FAIL has been returned from a call to a COM component." The file does not need to be part of the project. The inserted text looks like this:

    Exception

    a, b

    //a b

    /* a b */

    //a,

    +Error HRESULT E_FAIL has been returned from a call to a COM component.

    +EnvDTE -> Void set_Text(System.String)

    (System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component.

    at EnvDTE.TextSelection.set_Text(String pText)

    at Descr2005.Descr.TestError() in vsmacros:--file location removed--)

     

     

    This failure would occure also for /* a, b */ if the previous line was removed. Somehow a simple coma (,) within a comment makes a difference.

    When inserting the text manually (paste from the clipboard), no error occures. Also writing the text directly works ok. I also couldn't find any relation between the error and the VS2005 project or text editor setting.

     

    What is the error cause? I can't figure it out.

    Is there any solution to it?

     

    Thanks in advance.

    Mano

    Thursday, March 27, 2008 9:04 AM

Answers

  • For me, replacing Selection.Text with Selection.Insert fixed the problem.

    Somehow, I feel like Microsoft Corporation now owes me something.
    Wednesday, April 9, 2008 8:46 PM

All replies

  • For me, replacing Selection.Text with Selection.Insert fixed the problem.

    Somehow, I feel like Microsoft Corporation now owes me something.
    Wednesday, April 9, 2008 8:46 PM
  • Many thanks, ChristoP,

    replacing Selection.Text with the

    Code Snippet
    call Selection.Insert(text, vsInsertFlags.vsInsertFlagsContainNewText)

     

     

    fixed the problem and works fine for me (replaces the selected text).

    • Proposed as answer by Kaptan.GAK Sunday, January 30, 2011 5:48 AM
    Thursday, April 10, 2008 6:39 AM
  • Hi I had the same problem when I copied a SSIS solution from work to work on it at home.

    My solution was quite simple. Go into the code and remove all breakpoints. Save and re-build.

    I was then able to come back in and set breakpoints without getting the anoying error.

    I hope this helps someone in the future,

    Sunday, January 30, 2011 5:51 AM