Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Word Zeilen : Sentences Fehler System.InvalidCastException HResult 0x80004002

10.11.2018 (👁6809)

Loop durch Word Zeilen Sentences

Beim Versuch durch die SĂ€tze eines Word-Dokumentes zu durchlaufen kommt die Meldung

Fehlermeldung:

Das COM-Objekt des Typs "System.__ComObject" kann nicht in den Schnittstellentyp "Microsoft.Office.Interop.Word.Sentences" umgewandelt werden.

Fehlerhafter Code

foreach (Word.Sentences sLine in _doc.Sentences)
            {

..

Ursache man kann nicht Sentences mit einzelnen Objekten vom Typ Sentence durchlaufen.

Es gibt kein Objekt-/Klassen Typ: Sentence

Lösung:

Die AufzĂ€hlung Sentences[] gibt einzelne Objekte vom Typ: Range zurĂŒck

Beispiel der Lösung


Loop durch alle Zeilen eines Dokuments

foreach (Word.Range sLine in _doc.Sentences)

            {

                //-------< Check Sentence >------

                string sText = sLine.Text;

 

                sText = sText.Trim();

                if (sText == sHeader )

                {

                    //----< Line_found >----

                    

                    sLine.InsertAfter("\r\r");

                    sLine.Select();

                    _app.Selection.EndKey();

                    _app.Selection.Select();

                    Range newRange = _app.Selection.Range;

                    _app.ScreenRefresh();

 

                    //< init setup >

                    int Columns_Count = Settings1.Default.Columns_Count;

                    //</ init setup >

 

                    //< create new table >

                    //Word.Range newRange = sLine.MoveEnd(WdUnits.wdCharacter, -1);

                    Word.Table newTable = _doc.Tables.Add( newRange ,NumRows:1,NumColumns: Columns_Count);

                    newTable.Borders.InsideColor = WdColor.wdColorRed;

                    //</ create new table >

 

                    //< out >

                    return newTable;

                    //</ out >

                    //----</ Line_found >----

                }

                //-------</ Check Sentence >------

            }

Betrifft: Microsoft MS Word, Sentences Lines Zeilen range, vba C# interop com

Komplette Fehlermeldung:

System.InvalidCastException

  HResult=0x80004002

Schnittstelle nicht unterstĂŒtzt (Ausnahme von HRESULT: 0x80004002 (E_NOINTERFACE)).

  Source=<Cannot evaluate the exception source>

  StackTrace:

<Cannot evaluate the exception stack trace>

 

 

Message=Das COM-Objekt des Typs "System.__ComObject" kann nicht in den Schnittstellentyp "Microsoft.Office.Interop.Word.Sentences" umgewandelt werden.

Dieser Vorgang konnte nicht durchgefĂŒhrt werden, da der QueryInterface-Aufruf an die COM-Komponente fĂŒr die Schnittstelle mit der IID "xx" aufgrund des folgenden Fehlers nicht durchgefĂŒhrt werden konnte:

 

 

 

foreach (Word.Range sLine in _doc.Sentences)

            {

                //-------< Check Sentence >------

                string sText = sLine.Text;

 

                sText = sText.Trim();

                if (sText == sHeader )

                {

                    //----< Line_found >----

                    

                    sLine.InsertAfter("\r\r");

                    sLine.Select();

                    _app.Selection.EndKey();

                    _app.Selection.Select();

                    Range newRange = _app.Selection.Range;

                    _app.ScreenRefresh();

 

                    //< init setup >

                    int Columns_Count = Settings1.Default.Columns_Count;

                    //</ init setup >

 

                    //< create new table >

                    //Word.Range newRange = sLine.MoveEnd(WdUnits.wdCharacter, -1);

                    Word.Table newTable = _doc.Tables.Add( newRange ,NumRows:1,NumColumns: Columns_Count);

                    newTable.Borders.InsideColor = WdColor.wdColorRed;

                    //</ create new table >

 

                    //< out >

                    return newTable;

                    //</ out >

                    //----</ Line_found >----

                }

                //-------</ Check Sentence >------

            }