如何从模板编程方式创建Word文档 [英] How to create word docs programmatically from a template

查看:117
本文介绍了如何从模板编程方式创建Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建在Microsoft Office Word约600报告。这些文件都填充了一个本地驱动器上找到的数据库中的数据和图像。
我想通了,我也许可以在Visual Studio 2010中一个Word模板项目和程序模板,这样,当你输入一个值(ID号码),它会自动填写整个文档。

I am trying to create about 600 reports in microsoft office Word. The documents are populated with data from a database, and images found on a local drive. I have figured out, that I might create a Word Template project in visual studio 2010, and program the template, so that when you enter a single value (id-number), it automatically fills out the entire document.

我确信这是可能的。唯一的问题是。我如何通过数据库中的所有项循环,打开基于模板的新文档,设置id值?

I am quite confident that this is possible. the only problem is. How do I loop through all entries in the database, open a new document based on the template and set the id-value?

for(int i = 0; i < idnumbers.Count(); i++)
{
     Word.Application app = new Word.Application();
     Word.Document doc = app.Documents.Add(@"C:\..\WordGenerator\bin\Debug\WordTemplate.dotx");
     //input the id-number below: HOW??

     doc.SaveAs(FileName: @"c:\temp\test.docx"); 
}

应用程序应该只运行一次,生成报告,并没有关系'T得要快。它只是具有易于开发。

The application is supposed to run only once, generating the reports, and it doesn´t have to be fast. It just has to be easy to develop.

这里的问题是,它似乎文档基对象不是道项目之外访问。替代Microsoft.Office.Interop.Word.Document没有像SelectContentControlsByTitle功能,可以让我找到并设置我的ContentControls。而这正是我需要做的..

The problem here is, that it seems that the DocumentBase object is not accessible outside the Word project. The substitute Microsoft.Office.Interop.Word.Document does not have functionality like SelectContentControlsByTitle that allows me to find and set my ContentControls. And that is exactly what I need to do..

这是我的代码是什么样子,现在文本插入我的领域:
Word.Application应用=新Word.Application();

This is what my code looks like now to insert the text into my field: Word.Application app = new Word.Application();

        Word.Document doc = app.Documents.Add(@"C:\..\test.dotx");

        foreach (Word.ContentControl cc in doc.SelectContentControlsByTitle("MyCCTitle"))
        {
            cc.Range.Text += "1234";
        }

        doc.SaveAs(FileName: @"c:\temp\test.docx");



然后在我的BeforeSave模板中的事件处理程序填写根据MyCCTitle题为对象中的文本文档

Then an eventhandler on my template on BeforeSave fills out the document based on the text in MyCCTitle-titled object.

推荐答案

也许你应该看Microsoft.Office.Tools.Word.Document?

Maybe you should be looking at Microsoft.Office.Tools.Word.Document?

Document.SelectContentControlsByTitle

这篇关于如何从模板编程方式创建Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆