Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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?
 
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 5-Apr-11 2:53am
Edited 5-Apr-11 3:24am
v2
Comments
Eduard Keilholz at 5-Apr-11 7:57am
   
Funny, German... but erhm... English?
Member 3945068 at 6-Apr-11 10:15am
   
Better a solution in German then no solution in English!
Thank you John for translation.
Rate this: bad
good
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).
  Permalink  
v5
Rate this: bad
good
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
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
  Permalink  
v3

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

  Print Answers RSS
0 OriginalGriff 370
1 Sergey Alexandrovich Kryukov 329
2 BillWoodruff 235
3 Afzaal Ahmad Zeeshan 209
4 CPallini 195
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 6 Apr 2011
Copyright © CodeProject, 1999-2014
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