Click here to Skip to main content
15,908,675 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hellow.
My project is to send letter to ms word 2010 using c# 2010.

the reference :
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools
for Office\PIA\Office12\Microsoft.Office.Interop.Word.dll

when i compile the program i got a warning about ambiguity between ms.Quit and (may be) from system.Quit
unfortunatley this warning does not appears any more
my goal is to close and then quit the ms word using
Object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
Object originalFormat = Type.Missing;
Object routeDocument = Type.Missing;
//((Microsoft.Office.Interop.Word._Document)wrdApp).Close(ref saveChanges, ref originalFormat, ref routeDocument);
wrdApp.Documents.Close(ref saveChanges,ref originalFormat, ref routeDocument);

//saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
//originalFormat = Type.Missing;
//routeDocument = Type.Missing;
//((Microsoft.Office.Interop.Word._Application)wrdApp).Quit(ref saveChanges,ref originalFormat, ref routeDocument);//<----- Line A
wrdApp.Quit(ref saveChanges,ref originalFormat, ref routeDocument);//<----- Line B

in the code above(see my comment in line B) ,i got a green zigzag line under the word (i do not mean ms word) Quit (see line B)
but when i made line B a comment and uncomented line A
i got this message

Unable to cast COM object of type 'Microsoft.Office.Interop.Word.ApplicationClass'
to interface type 'Microsoft.Office.Interop.Word._Document'.
this operation failed because the QueryInterface call on the COM
component for the interface with IID '{0002096B-0000-0000-C000-000000000046}'
failed due to the following error:
No such interface supported (Exception from HRESULT: 0x80004002

those are some lines in my code which might help
using Word = Microsoft.Office.Interop.Word;
using System.Diagnostics;
namespace Wared
{
    public partial class WordOrderForm : Form
    {
        Word.Application wrdApp;
        Word._Document wrdDoc;

what is the save code which can close and quit ms word from c# project ?
Posted
Updated 16-Jan-15 7:54am
v2
Comments
Zoltán Zörgő 17-Jan-15 6:51am    
Any progress?

The error you got is telling you that you are trying to act on the application object via the document interface, but his might be only a deferred effect of the previous action.

Might not be the cause, but I noticed this: wrdApp.Documents.Close(..., I mean the plural here. But Documents is a collection and does not have any Close method. Please see MSDN[^].

This is a LinqPad demo, which works as expected:
C#
var w = new Word.Application();
w.Visible = true;
w.Documents.Add();
Thread.Sleep(5000);
((Word._Document)w.ActiveDocument).Close();
Thread.Sleep(5000);
((Word._Application)w).Quit();

I have added the reference and the namespace alias like yours. I have also noiced the ambiguity, but this way you can solve it.
 
Share this answer
 
v2
I think my tip article may help you.

How to Release COM Interop Objects so the Called Program Can Exit[^]

Also, you need the following statement at the beginning of your program.
using Microsoft.Office.Interop;


I recommend removing
using Word = Microsoft.Office.Interop.Word;
 
Share this answer
 
v3
Comments
Engineer khalid 16-Jan-15 15:59pm    
To Mike Meinz
if i use
using Microsoft.Office.Interop;
instead of
using Word = Microsoft.Office.Interop.Word;
i loss the variable Word (see the above line)
from which i obtained the variable wrdApp
see also my the orignal question
namespace Wared
{
public partial class WordOrderForm : Form
{
Word.Application wrdApp;
Word._Document wrdDoc;

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