C#創(chuàng)建一個(gè)Word并打開的方法
更新時(shí)間:2015年04月14日 09:37:38 作者:Regina
這篇文章主要介紹了C#創(chuàng)建一個(gè)Word并打開的方法,實(shí)例分析了C#操作word的常用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了C#創(chuàng)建一個(gè)Word并打開的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
復(fù)制代碼 代碼如下:
private static string _createNewWord(string allTnC)
{
Microsoft.Office.Interop.Word.Document wordDocument = null;
Microsoft.Office.Interop.Word.Application wordApplication = null;
string dateTimeNow = DateTime.Now.ToString();
string wordPath = Path.GetTempFileName();
wordApplication = new Word.ApplicationClass();
object nothing = Missing.Value;
wordDocument = wordApplication.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);
wordDocument.Paragraphs.Last.Range.Text = allTnC;
object format = Word.WdSaveFormat.wdFormatDocumentDefault;
wordDocument.SaveAs(wordPath, ref format, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing,
ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing);
wordDocument.Application.Documents.Close(ref nothing, ref nothing, ref nothing);
((Word.ApplicationClass)wordApplication).Quit(ref nothing, ref nothing, ref nothing);
return wordPath;
}
private static void _importTnCToActiveDocument(string wordPath)
{
Word.Application wordApplication = new Word.Application();
Word.Document wordDocument = new Word.Document();
Object nothing = System.Reflection.Missing.Value;
Object filePath = wordPath;
wordApplication.Documents.Open(ref filePath, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing);
wordDocument = wordApplication.ActiveDocument;
wordApplication.Visible = true;
}
{
Microsoft.Office.Interop.Word.Document wordDocument = null;
Microsoft.Office.Interop.Word.Application wordApplication = null;
string dateTimeNow = DateTime.Now.ToString();
string wordPath = Path.GetTempFileName();
wordApplication = new Word.ApplicationClass();
object nothing = Missing.Value;
wordDocument = wordApplication.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);
wordDocument.Paragraphs.Last.Range.Text = allTnC;
object format = Word.WdSaveFormat.wdFormatDocumentDefault;
wordDocument.SaveAs(wordPath, ref format, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing,
ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing);
wordDocument.Application.Documents.Close(ref nothing, ref nothing, ref nothing);
((Word.ApplicationClass)wordApplication).Quit(ref nothing, ref nothing, ref nothing);
return wordPath;
}
private static void _importTnCToActiveDocument(string wordPath)
{
Word.Application wordApplication = new Word.Application();
Word.Document wordDocument = new Word.Document();
Object nothing = System.Reflection.Missing.Value;
Object filePath = wordPath;
wordApplication.Documents.Open(ref filePath, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing);
wordDocument = wordApplication.ActiveDocument;
wordApplication.Visible = true;
}
vs10-office項(xiàng)目中創(chuàng)建ThisAddIn按鈕實(shí)現(xiàn)某些功能當(dāng)打開多個(gè)word時(shí)便獲取不到當(dāng)前word文檔對(duì)象(如需要獲取打開的第一個(gè)文檔中的bookmark)
可以在 Startup 中加入:
ViteRibbon viteRibbon = new ViteRibbon(this.Application);
構(gòu)造函數(shù)傳遞該參數(shù)并賦值:
currentDoucment = wordApp.ActiveDocument;
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
unity 如何判斷鼠標(biāo)是否在哪個(gè)UI上(兩種方法)
這篇文章主要介紹了unity 判斷鼠標(biāo)是否在哪個(gè)UI上的兩種實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
C#動(dòng)態(tài)加載dll擴(kuò)展系統(tǒng)功能的方法
這篇文章主要介紹了C#動(dòng)態(tài)加載dll擴(kuò)展系統(tǒng)功能的方法,涉及C#動(dòng)態(tài)加載dll擴(kuò)展的相關(guān)技巧,需要的朋友可以參考下2015-04-04
C#設(shè)計(jì)模式之觀察者模式實(shí)例講解
這篇文章主要介紹了C#設(shè)計(jì)模式之觀察者模式實(shí)例講解,本文詳細(xì)講解了觀察者模式的定義、優(yōu)缺點(diǎn)、代碼實(shí)例等,需要的朋友可以參考下2014-10-10
分享WCF文件傳輸實(shí)現(xiàn)方法---WCFFileTransfer
這篇文章主要介紹了分享WCF文件傳輸實(shí)現(xiàn)方法---WCFFileTransfer,需要的朋友可以參考下2015-11-11

