我如何......如何从c#中退出ms word [英] How do I...how to quit ms word from c#

查看:84
本文介绍了我如何......如何从c#中退出ms word的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hellow。

我的项目是使用c#2010发送给ms word 2010的信。



参考:

C:\Program Files(x86)\ Microsoft Visual Studio 10.0 \ Visual Studio工具

for Office \ PIA \Office12 \ Microsoft.Office.Interop.Word。 dll



当我编译程序时,我得到一个关于ms.Quit和(可能)来自system.Quit的歧义的警告

unfortunatley此警告不再出现

我的目标是关闭然后退出ms字使用

对象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。在上面的代码中退出(ref saveChanges,ref originalFormat,ref routeDocument); //< ----- Line B



(参见我在B行的评论) ,我在这个词下面有一条绿色的曲折线(我不是指ms字)退出(见B行)

但是当我把B行评论并且未通过线路A

i得到此消息



无法将类型为'Microsoft.Office.Interop.Word.ApplicationClass'的COM对象转换为接口类型
'Microsoft.Office.Interop.Word._Document'。

此操作失败,因为QueryInterface调用COM上的接口,其中IID为{0002096B-0000-的接口0000-C000-000000000046}'

由于以下错误而失败:

不支持此类接口(HRESULT异常:0x80004002



这些是我的一些行可能有用的代码

使用Word = Microsoft.Office.Interop.Word; 
使用System.Diagnostics;
namespace Wared
{
public partial class WordOrderForm:Form
{
Word.Application wrdApp;
Word._Document wrdDoc;



什么是保存代码,可以关闭并退出来自c#项目的ms字?

解决方案

你得到的错误是告诉你你正试图通过文档界面对应用程序对象进行操作,但是他可能只是前一个操作的延迟效果。



可能不是原因,但我注意到了这一点: wrdApp.Document s .Close(... ,我的意思是复数。但是Documents是一个集合,没有任何Close方法。请参阅 MSDN [ ^ ]。



这是一个LinqPad演示,按预期工作:

  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();



我添加了引用和你的命名空间别名。我也注意到了歧义,但这样你就可以解决它。


我认为我的提示文章可以帮到你。



如何释放COM互操作对象,以便被叫程序可以退出 [ ^ ]



此外,您需要在程序开头使用以下语句。

使用Microsoft.Office.Interop; 





我建议使用Word = Microsoft.Office.Interop.Word删除

 ; 


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 ?

解决方案

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:

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.


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;


这篇关于我如何......如何从c#中退出ms word的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆