C#创建一个Word并打开的方法
本文实例讲述了C#创建一个Word并打开的方法。分享给大家供大家参考。具体实现方法如下:
privatestaticstring_createNewWord(stringallTnC)
{
Microsoft.Office.Interop.Word.DocumentwordDocument=null;
Microsoft.Office.Interop.Word.ApplicationwordApplication=null;
stringdateTimeNow=DateTime.Now.ToString();
stringwordPath=Path.GetTempFileName();
wordApplication=newWord.ApplicationClass();
objectnothing=Missing.Value;
wordDocument=wordApplication.Documents.Add(refnothing,refnothing,refnothing,refnothing);
wordDocument.Paragraphs.Last.Range.Text=allTnC;
objectformat=Word.WdSaveFormat.wdFormatDocumentDefault;
wordDocument.SaveAs(wordPath,refformat,refnothing,refnothing,refnothing,refnothing,refnothing,refnothing,
refnothing,refnothing,refnothing,refnothing,refnothing,refnothing,refnothing,refnothing);
wordDocument.Application.Documents.Close(refnothing,refnothing,refnothing);
((Word.ApplicationClass)wordApplication).Quit(refnothing,refnothing,refnothing);
returnwordPath;
}
privatestaticvoid_importTnCToActiveDocument(stringwordPath)
{
Word.ApplicationwordApplication=newWord.Application();
Word.DocumentwordDocument=newWord.Document();
Objectnothing=System.Reflection.Missing.Value;
ObjectfilePath=wordPath;
wordApplication.Documents.Open(reffilePath,refnothing,refnothing,refnothing,refnothing,refnothing,refnothing,refnothing,refnothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing);
wordDocument=wordApplication.ActiveDocument;
wordApplication.Visible=true;
}vs10-office项目中创建ThisAddIn按钮实现某些功能当打开多个word时便获取不到当前word文档对象(如需要获取打开的第一个文档中的bookmark)
可以在Startup中加入:
ViteRibbonviteRibbon=newViteRibbon(this.Application);
构造函数传递该参数并赋值:
currentDoucment=wordApp.ActiveDocument;
希望本文所述对大家的C#程序设计有所帮助。
热门推荐
10 喜得孙子祝福语简短唯美
11 婚礼祝福语简短朴实大众
12 员工周年祝福语大全简短
13 家长回复节日祝福语简短
14 简短生日祝福语老同学
15 夜雨祝福语简短10字
16 蛋糕祝福语简短闺蜜
17 部门过年祝福语简短独特
18 简短精炼的结婚祝福语