Microsoft word 11.0 Object Library 用什么关闭word程序?

孙月魂 2011-11-18 04:17:06


string message = "";
try
{

//打开文档:
string name = "新建 Microsoft Word 文档.doc";
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"E:\\新建 Microsoft Word 文档.doc ";
oDoc = oWord.Documents.Open(ref fileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

//插入图片
string PName = @"E:\\360截图20110815162932593.jpg";//图片所在路径
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = oDoc.Application.Selection.Range;
oDoc.Application.ActiveDocument.InlineShapes.AddPicture(PName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
oDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;//图片宽度
oDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//图片高度

//文件保存
object fileName2 = @"E:\\新建 Microsoft Word 文档2.doc ";
oDoc.SaveAs(ref fileName2, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
//oDoc.Quit(ref oMissing, ref oMissing, ref oMissing);
message = name + "文档生成成功,以保存到C:CNSI下";
}
catch
{
message = "文件导出异常!";
}
//return message;



这是我的代码~我用这个代码打开一个word文档,向里面插入一张图片~但是我发现图片插入后word程序不会自动关闭,只是把打开的文件关闭了~有什么办法把word程序也一起关闭吗?我试了网上教程里面的quit,但却发现没有这个方法,不知道这是为什么。。。还有我发现我插入的图片只能出现在表格的左上角,有什么办法可以让我来选择插入的位置吗?
...全文
138 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hjanjie 2011-11-28
  • 打赏
  • 举报
回复
使用书签来定位
孙月魂 2011-11-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 net_lover 的回复:]
oWord.Visible = false;
[/Quote]
多谢!但是要怎么定位了?
孟子E章 2011-11-18
  • 打赏
  • 举报
回复
oWord.Visible = false;
孙月魂 2011-11-18
  • 打赏
  • 举报
回复
还有用这个的时候,word程序总是要弹出来闪一下,有没有什么办法让word程序完全在后台运行啊?
孙月魂 2011-11-18
  • 打赏
  • 举报
回复
晕~原来是前面那个变量搞错了。。。要用oword。。。我被教程骗了。。。
但是怎么选择插入图片的位置啊~有木有人知道?
暖枫无敌 2011-11-18
  • 打赏
  • 举报
回复

oWord.GetType().InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
孙月魂 2011-11-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 net_lover 的回复:]
System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord );
[/Quote]
不行啊!还是关不掉。。。
孟子E章 2011-11-18
  • 打赏
  • 举报
回复
System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord );

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧