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#程序设计有所帮助。