使用VSTO创建一个新的Word文档
时间:2020-03-06 15:00:54 来源:igfitidea点击:
如何使用Visual Studio Tools for Office编程地创建新的Word文档?
解决方案
对于VSTO应用程序级加载项,我们可以执行以下操作:
Globals.ThisAddIn.Application.Documents.Add(ref objTemplate, ref missingType, ref missingType, ref missingType);
其中objTemplate可以是文档的模板
请参阅Documents.Add方法
现在,我对此可能是错的,但是我不相信我们可以使用VSTO来制作新的Word文档。我对VSTO并不是很熟悉,所以如果我在这一点上不正确,请原谅我。
我确实知道我们可以使用Office Interop库来执行此操作。
要下载这些库,只需搜索"办公互操作程序集",其中可能包括我们想要的Office版本(例如:"办公互操作程序集2007")。
将Word Interop程序集包含到应用程序中之后(使用"添加引用"),我们可以执行以下操作:
using Word = Microsoft.Office.Interop.Word; object missing = System.Reflection.Missing.Value; Word.Application app = new Word.ApplicationClass(); Word.Document doc = app.Documents.Add(ref missing, ref missing, ref missing, ref missing); doc.Activate(); app.Selection.TypeText("This is some text in my new Word document."); app.Selection.TypeParagraph();
希望对我们有所帮助!