Click here to Skip to main content
11,925,307 members (56,940 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET Word
Ich versuche seit geraumer Zeit eine VB.NEt Anwendung mit Word 2007 Automatisierung von VB.NET 2008 auf VB.NET 2010 und Word 2010 umzustellen.
Alle von mir gefundenen Beispiele stammen aus älteren Versionen und erzeugen das gleiche Problem:

Das COM-Objekt des Typs "Microsoft.Office.Interop.Word.DocumentClass" kann nicht in den Klassentyp "WindowsApplication1.word.Document" umgewandelt werden. Instanzen von Typen, die COM-Komponenten repräsentieren, können nicht in andere Typen umgewandelt werden, die keine COM-Komponenten repräsentieren. Eine Umwandlung in Schnittstellen ist jedoch möglich, sofern die zugrunde liegende COM-Komponente QueryInterface-Aufrufe für die IID der Schnittstelle unterstützt.

Kann mir jemand weiterhelfen?



I have tried for some time a VB.NEt use with Word 2007 automation from VB.NET 2008 to switch to VB.NET 2010 and Word 2010. All of my examples are found in older versions and produce the same problem:

The COM object of type Microsoft.Office.Interop.Word.DocumentClass "can not be cast to the class type" WindowsApplication1.word.Document. Instances of types that represent COM components can not be converted into other types that do not represent COM components. A conversion interfaces is possible, provided that the underlying COM component QueryInterface calls for the IID of the interface supports.

Can anyone help me?
Posted 5-Apr-11 2:53am
Edited 5-Apr-11 3:24am
Eduard Keilholz 5-Apr-11 7:57am
Funny, German... but erhm... English?
Member 3945068 6-Apr-11 10:15am
Better a solution in German then no solution in English!
Thank you John for translation.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You didn't provide the code you're using, so try this:

DocumentClass m_Doc = (DocumentClass)m_App.Documents.Open(FileName: m_strTemplate);

Or in VB.NET:
Dim m_doc as documentclass = DirectCast(m_App.Documents.Open(FileName: m_strTemplate))

EDIT ===========

Corrected Dave Kreskowiak's invalid VB code (check versions to see what I mean).
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Thank you for your help,
(I use VB.NET 2010 an Office 2010)

Just now I solved the problem myself in the following way:

1. I referenced from Microsoft.Office.Interop.Word.Document from PIA Office 2010

2. then I imported Microsoft.Office.Interop.Word in the Module

3. I changed the Object at the CType - Expression from Word.Document to
Microsoft.Office.Interop.Word.Document (see the bold text)

4. now it works

Imports Microsoft.Office.Interop.Word
Public doc As Microsoft.Office.Interop.Word.Document

Public Sub Druck()

'open Document
doc = CType(GetObject("abc.doc"), Microsoft.Office.Interop.Word.Document)
MessageBox.Show("... ", "...", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try

doc.Application.Visible = False
With doc.Application
'fill the document
End Sub

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151126.1 | Last Updated 6 Apr 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100