我在vs中操作Word,最后要关闭Word文件,在vs2003中正确,最近升级到2005,去报错,代码如下:
Word.Application myApp=null;
Word.Document myDoc=null;
myApp=new Word.ApplicationClass();
object ob=(object)p_FileName;
myDoc=myApp.Documents.Open(ref ob,ref Nothing,ref Nothing,ref Nothing,ref Nothing,
ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,
ref Nothing,ref Nothing);
myApp.Visible=true;关闭的代码如下:
myDoc.Close(ref bTrue,ref Nothing,ref Nothing);
编译关闭代码时,出现如下警告:警告 1 方法“Word._Document.Close(ref object, ref object, ref object)”和非方法“Word.DocumentEvents_Event.Close”之间存在二义性。将使用方法组。 F:\开发工程\公用工具\Lqjt\frmCommDataPro.cs 2362 10 Lqjt应该如何处理?
Word.Application myApp=null;
Word.Document myDoc=null;
myApp=new Word.ApplicationClass();
object ob=(object)p_FileName;
myDoc=myApp.Documents.Open(ref ob,ref Nothing,ref Nothing,ref Nothing,ref Nothing,
ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,
ref Nothing,ref Nothing);
myApp.Visible=true;关闭的代码如下:
myDoc.Close(ref bTrue,ref Nothing,ref Nothing);
编译关闭代码时,出现如下警告:警告 1 方法“Word._Document.Close(ref object, ref object, ref object)”和非方法“Word.DocumentEvents_Event.Close”之间存在二义性。将使用方法组。 F:\开发工程\公用工具\Lqjt\frmCommDataPro.cs 2362 10 Lqjt应该如何处理?
只不过我是把所有操作Word的代码全部封装成方法,放到一个DLL里的.这个DLL是用VS2003编译的.很方便.
本来我也是准备用2003的,但是考虑到版本问题也就放弃了.
因为用2003的组件,生成 的Word文档在word2003以下的版本里打不开.
Word.ApplicationClass WordApp
代替题中的Word.Application myApp
用Word.DocumentClass WordDoc
代替题中的Word.Document WordDoc
在打开文件时用强制转换一下:
myDoc=(Word.DocumentClass )myApp.Documents.Open(ref ob,ref Nothing,ref Nothing,ref Nothing,ref Nothing,
ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,
ref Nothing,ref Nothing);
myDoc=(Word.DocumentClass )……,是强制转换的,代码就没有问题了
private Word.ApplicationClass oWordApplic;
private Word.Document oDoc;
public void Open(string strFileName)
{
object fileName = strFileName;
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value; oDoc = oWordApplic.Documents.Open(ref fileName, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing); oDoc.Activate();
} public void OpenFromTemplate(string strTemplate)
{
object missing = System.Reflection.Missing.Value;
object oTemplate = strTemplate;
object oVisible = true;
oDoc = oWordApplic.Documents.Add(ref oTemplate, ref missing, ref missing, ref missing); oDoc.Activate();
}public void Quit( )
{
object missing = System.Reflection.Missing.Value;
oWordApplic.Quit(ref missing, ref missing, ref missing);
oWordApplic=null;
GC.Collect();
GC.WaitForPendingFinalizers();
}