C#和Word Interlop库创建“腐败”库来自模板的文档 [英] C# and Word Interlop library creates "corrupt" documents from templates

查看:105
本文介绍了C#和Word Interlop库创建“腐败”库来自模板的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!



我正在尝试使用MSWord 2007创建报表系统,但我遇到了模板复制的问题。



我已经完成了打开模板的代码部分并创建了一个新文档,但是 文档是MESS - 边框被删除,表和标题完全被删除不同的立场,这是不可接受的。



我工作了四天,在MSDN和其他热门节目网站上找到解决方案,但我无法弄明白!



感谢您的时间。



我正在创建使用Microsoft Word 2007作为报告的应用程序工具 - 从各自的模板打印某些文档。它没有这样做。这是一段代码:

  private   void  ManagersDoc_Click( object  sender,EventArgs e)
{
Object wMissing = System.Reflection.Missing.Value;
对象 wTrue = true ;
对象 wFalse = false ;
Word.Application wordApp = new Word.Application();
Word.Document wordDoc = new Word.Document();
wordApp.Visible = true ;
对象 docPath = @ J:\\ \\Quar.dotm;
// 文档路径
模板wordDoc = wordApp.Documents.Add( ref docPath, ref wMissing, ref wTrue , ref wTrue);
...
// 与数据库PostgeSQL 9.1.2的交互。
对象 oSaveAsFile = @ J:\ \SampleDoc.doc;
wordDoc.SaveAs( ref oSaveAsFile, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing); wordApp.Quit();





我们有要打印的文档模板,它有:

1)两张桌子

2)字幕

3)标记所有都是整洁,干净,占据了大约50%的纸张。



应该做什么代码:

1)从Quar.dotm模板创建一个新的Word文档。

2)使用与模板完全相同的数据填写



它的作用:

1)创建一个新单词来自Quar.dotm模板的文档。

2)用数据填充,你必须手动将所有表格,标题和标记放入其中。



除此之外,在预览模式下(MsWord显示该文档在纸上的外观),所有内容都分为2页,2个表随机重新调整大小并分散在两个页面上。

IDE:Visual Studio 2010语言:C#3.0 C#3.0的原因 - 将在Windows XP上部署应用程序。 .Net Framework版本:3.5我在这里做错了什么?

解决方案

我看到一两个原因:

1)目标文件扩展名不正确!

和/或

2)SaveAs方法未指定FileFormat。



广告1)

替换:

 对象 oSaveAsFile =  @  J:\\SampleDoc.doc; 



with

 对象 oSaveAsFile =  @  J:\\SampleDoc.docx; 



* .doc扩展用于MS Word 97-2003文件。

* .docx是MS Word 2007文件中的默认扩展名。



Ad 2)

如果要将文件保存在以前版本的MS Word中,则需要指定 FileFormat 。了解有关 SaveAs的更多信息 [ ^ ]方法和 wdSaveFormat [ ^ ]枚举。


您实际上正在执行邮件合并。使用COM不是最好的方法,因为它依赖于版本,非健壮且不能部署在服务器端。

一些经济实惠的邮件合并第三方可以节省大量时间。

Hello!

I`m trying to create a report system by using MSWord 2007, but i have problems with "Template replication".

I have done the code part that opens template and creates a new document, but document is MESS - borders are erased, tables and captions are in completely different positions, what is unacceptable.

I`m working four days to find solution on MSDN and other popular programming sites, but i can`t figure it out!

Thank you for your time.

I`m creating application that uses Microsoft Word 2007 as a report tool - printing certain documents from their respective templates. It fails to do so. Here is a piece of code:

private void ManagersDoc_Click(object sender, EventArgs e) 
{
 Object wMissing = System.Reflection.Missing.Value;
 Object wTrue = true;
 Object wFalse = false;
 Word.Application wordApp = new Word.Application();
 Word.Document wordDoc = new Word.Document();
 wordApp.Visible = true;
 Object docPath = @"J:\Quar.dotm";
 // Path to Document
 Template wordDoc = wordApp.Documents.Add(ref docPath, ref wMissing, ref wTrue, ref wTrue);
 ... 
// Interaction with Database PostgeSQL 9.1.2.
 Object oSaveAsFile = @"J:\\SampleDoc.doc";
 wordDoc.SaveAs(ref oSaveAsFile, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing); wordApp.Quit();



We have template of document that is going to be printed, it has:
1) Two tables
2) Captions
3) Markers All is neat, clean and takes 50 roughly percent of sheet.

What code is supposed to do:
1) Create a new Word document from "Quar.dotm" Template.
2) Fill it with data exactly as it is in template

What it does:
1) Create a new word Document from "Quar.dotm" template.
2) Fill it with data in such way that you have to manually place all tables, captions and markers in it.

In addition to that, in preview mode (where MsWord shows how this document will look on paper) everything is split on 2 pages, 2 tables randomly re-sized and scattered across two pages.
IDE: Visual Studio 2010 Language: C# 3.0 Reason why C# 3.0 - app is going to be deployed on Windows XP. .Net Framework version: 3.5 What i`m doing wrong here?

解决方案

I see one or two reasons:
1) destination file extension is not correct!
and/or
2) SaveAs method doesn't specify FileFormat.

Ad 1)
Replace:

Object oSaveAsFile = @"J:\\SampleDoc.doc";


with

Object oSaveAsFile = @"J:\\SampleDoc.docx";


*.doc extension was used in MS Word 97-2003 files.
*.docx is default extension in MS Word 2007 files.

Ad 2)
If you would like to save the file in previous version of MS Word, you need to specify FileFormat. Read more about SaveAs[^] method and wdSaveFormat[^] enumeration.


You are actually performing a mail merge. Using COM is not the best possible approach since it is version dependent, non-robust and cannot be deployed on server side.
Some affordable third-party for mail-merge can you save a lot of time.


这篇关于C#和Word Interlop库创建“腐败”库来自模板的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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