Click here to Skip to main content
15,918,404 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
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?

Matthias

TRANSLATION:

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
Updated 5-Apr-11 2:24am
v2
Comments
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.

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

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


Or in VB.NET:
VB
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).
 
Share this answer
 
v5
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
Try
doc = CType(GetObject("abc.doc"), Microsoft.Office.Interop.Word.Document)
Catch
MessageBox.Show("... ", "...", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try

doc.Application.Visible = False
doc.ActiveWindow.Activate()
With doc.Application
'fill the document
...
End Sub
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900