我想用c#做一个通过word的dot模板文件 通过改变dot文件里的book书签来修改数据 生成新的word文件doc。
但我写好了 创建好了我的dot文件 去执行 每次都报这个错误:
Word 无法读取文档,文档可能损坏。
请尝试下列方法:
* 打开并修复文件。
* 用文本恢复转换器打开文件。
然后我也试过横多方法 都不行。那位大侠帮帮我 教教我怎么创建dot文件 还是我其他地方有错,我在c#里也应用了那个office的Microsoft.Office.Interop.Word这个 东西。那位大侠 帮帮小弟。小弟qq:115674440
这个是我的word控制类:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Word = Microsoft.Office.Interop.Word;namespace TechnologyGuide
{
class WordControl
{
private Word.Document wDoc = null;
private Word.Application wApp = null;
public Word.Document Document
{
get { return wDoc; }
set { wDoc = value; }
}
public Word.Application Application
{
get { return wApp; }
set { wApp = value; }
} #region 从模型创建新的Word文档
/// <summary>
/// 从模型创建新的Word文档
/// </summary>
public bool CreateNewWordDocument(string templateName)
{
try
{
return CreateNewWordDocument(templateName, ref wDoc, ref wApp);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 从模型创建新的Word文档,并且返回对象Document,Application
/// <summary>
/// 从模型创建新的Word文档,
/// </summary>
public static bool CreateNewWordDocument(string templateName, ref Word.Document wDoc, ref Word.Application WApp)
{
Word.Document thisDocument = null;
Word.Application thisApplication = new Word.ApplicationClass();
thisApplication.Visible = false;
thisApplication.Caption = "";
thisApplication.Options.CheckSpellingAsYouType = false;
thisApplication.Options.CheckGrammarAsYouType = false; Object Template = templateName
Object NewTemplate = false
Object DocumentType = Word.WdNewDocumentType.wdNewBlankDocument;
Object Visible = true
try
{
Word.Document wordDoc = thisApplication.Documents.Add(ref Template, ref NewTemplate, ref DocumentType, ref Visible);
thisDocument = wordDoc;
wDoc = wordDoc;
WApp = thisApplication;
return true;
}
catch (Exception ex)
{
string err = string.Format("创建Word文档出错,错误原因:{0}", ex.Message);
throw new Exception(err, ex);
}
}
#endregion
#region 文档另存为其他文件名
/// <summary>
/// 文档另存为其他文件名
/// </summary>
public bool SaveAs(string fileName)
{
try
{
return SaveAs(fileName, wDoc);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 文档另存为其他文件名
/// <summary>
/// 文档另存为其他文件名
/// </summary>
public static bool SaveAs(string fileName, Word.Document wDoc)
{
Object FileName = fileName; // 文档的名称。默认值是当前文件夹名和文件名。如果文档在以前没有保存过,则使用默认名称(例如,Doc1.doc)。如果已经存在具有指定文件名的文档,则会在不先提示用户的情况下改写文档。
Object FileFormat = Word.WdSaveFormat.wdFormatDocument; // 文档的保存格式。可以是任何 WdSaveFormat 值。要以另一种格式保存文档,请为 SaveFormat 属性指定适当的值。
Object LockComments = false; // 如果为 true,则锁定文档以进行注释。默认值为 false。
Object Password = System.Type.Missing; // 用来打开文档的密码字符串。(请参见下面的备注。)
Object AddToRecentFiles = false; // 如果为 true,则将该文档添加到“文件”菜单上最近使用的文件列表中。默认值为 true。
Object WritePassword = System.Type.Missing; // 用来保存对文件所做更改的密码字符串。(请参见下面的备注。)
Object ReadOnlyRecommended = false; // 如果为 true,则让 Microsoft Office Word 在打开文档时建议只读状态。默认值为 false。
Object EmbedTrueTypeFonts = false; //如果为 true,则将 TrueType 字体随文档一起保存。如果省略的话,则 EmbedTrueTypeFonts 参数假定 EmbedTrueTypeFonts 属性的值。
Object SaveNativePictureFormat = true; // 如果图形是从另一个平台(例如,Macintosh)导入的,则 true 表示仅保存导入图形的 Windows 版本。
Object SaveFormsData = false; // 如果为 true,则将用户在窗体中输入的数据另存为数据记录。
Object SaveAsAOCELetter = false; // 如果文档附加了邮件程序,则 true 表示会将文档另存为 AOCE 信函(邮件程序会进行保存)。
Object Encoding = System.Type.Missing; // MsoEncoding。要用于另存为编码文本文件的文档的代码页或字符集。默认值是系统代码页。
Object InsertLineBreaks = true; // 如果文档另存为文本文件,则 true 表示在每行文本末尾插入分行符。
Object AllowSubstitutions = false; //如果文档另存为文本文件,则 true 允许 Word 将某些符号替换为外观与之类似的文本。例如,将版权符号显示为 (c)。默认值为 false。
Object LineEnding = Word.WdLineEndingType.wdCRLF;// Word 在另存为文本文件的文档中标记分行符和换段符。可以是任何 WdLineEndingType 值。
Object AddBiDiMarks = true;//如果为 true,则向输出文件添加控制字符,以便保留原始文档中文本的双向布局。
try
{
wDoc.SaveAs(ref FileName, ref FileFormat, ref LockComments, ref Password, ref AddToRecentFiles, ref WritePassword
, ref ReadOnlyRecommended, ref EmbedTrueTypeFonts, ref SaveNativePictureFormat
, ref SaveFormsData, ref SaveAsAOCELetter, ref Encoding, ref InsertLineBreaks, ref AllowSubstitutions
, ref LineEnding, ref AddBiDiMarks);
return true;
}
catch (Exception ex)
{
string err = string.Format("另存文件出错,错误原因:{0}", ex.Message);
throw new Exception(err, ex);
}
}
#endregion
#region 关闭文档
/// <summary>
/// 关闭文档
/// </summary>
public void CloseDocument()
{
CloseDocument(wDoc, wApp);
wDoc = null;
wApp = null;
}
#endregion
#region 关闭文档
/// <summary>
/// 关闭文档
/// </summary>
public static void CloseDocument(Word.Document wDoc, Word.Application WApp)
{
Object SaveChanges = Word.WdSaveOptions.wdSaveChanges;// 指定文档的保存操作。可以是下列 WdSaveOptions 值之一:wdDoNotSaveChanges、wdPromptToSaveChanges 或 wdSaveChanges。
Object OriginalFormat = Word.WdOriginalFormat.wdOriginalDocumentFormat;// 指定文档的保存格式。可以是下列 WdOriginalFormat 值之一:wdOriginalDocumentFormat、wdPromptUser 或 wdWordDocument。
Object RouteDocument = false;// 如果为 true,则将文档传送给下一个收件人。如果没有为文档附加传送名单,则忽略此参数。
try
{
if (wDoc != null) wDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
if (WApp != null) WApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 填充书签
/// <summary>
/// 填充书签
/// </summary>
public void ReplaceBookMark(string book, string value)
{
try
{
object bkObj = book;
if (wApp.ActiveDocument.Books.Exists(book) == true)
{
wApp.ActiveDocument.Books.get_Item(ref bkObj).Select();
}
else return;
wApp.Selection.TypeText(value);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}
这个是主程序调用来写doc的private void CreateWord_btn_Click(object sender, EventArgs e)
{
try
{
this.TecGuideWord.CreateNewWordDocument("D:\\123.dot");
this.TecGuideWord.ReplaceBookMark("abc", "123");
this.TecGuideWord.SaveAs("d:\\1.doc");
this.TecGuideWord.CloseDocument();
}
catch (Exception ex)
{
return;
}
}
但我写好了 创建好了我的dot文件 去执行 每次都报这个错误:
Word 无法读取文档,文档可能损坏。
请尝试下列方法:
* 打开并修复文件。
* 用文本恢复转换器打开文件。
然后我也试过横多方法 都不行。那位大侠帮帮我 教教我怎么创建dot文件 还是我其他地方有错,我在c#里也应用了那个office的Microsoft.Office.Interop.Word这个 东西。那位大侠 帮帮小弟。小弟qq:115674440
这个是我的word控制类:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Word = Microsoft.Office.Interop.Word;namespace TechnologyGuide
{
class WordControl
{
private Word.Document wDoc = null;
private Word.Application wApp = null;
public Word.Document Document
{
get { return wDoc; }
set { wDoc = value; }
}
public Word.Application Application
{
get { return wApp; }
set { wApp = value; }
} #region 从模型创建新的Word文档
/// <summary>
/// 从模型创建新的Word文档
/// </summary>
public bool CreateNewWordDocument(string templateName)
{
try
{
return CreateNewWordDocument(templateName, ref wDoc, ref wApp);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 从模型创建新的Word文档,并且返回对象Document,Application
/// <summary>
/// 从模型创建新的Word文档,
/// </summary>
public static bool CreateNewWordDocument(string templateName, ref Word.Document wDoc, ref Word.Application WApp)
{
Word.Document thisDocument = null;
Word.Application thisApplication = new Word.ApplicationClass();
thisApplication.Visible = false;
thisApplication.Caption = "";
thisApplication.Options.CheckSpellingAsYouType = false;
thisApplication.Options.CheckGrammarAsYouType = false; Object Template = templateName
Object NewTemplate = false
Object DocumentType = Word.WdNewDocumentType.wdNewBlankDocument;
Object Visible = true
try
{
Word.Document wordDoc = thisApplication.Documents.Add(ref Template, ref NewTemplate, ref DocumentType, ref Visible);
thisDocument = wordDoc;
wDoc = wordDoc;
WApp = thisApplication;
return true;
}
catch (Exception ex)
{
string err = string.Format("创建Word文档出错,错误原因:{0}", ex.Message);
throw new Exception(err, ex);
}
}
#endregion
#region 文档另存为其他文件名
/// <summary>
/// 文档另存为其他文件名
/// </summary>
public bool SaveAs(string fileName)
{
try
{
return SaveAs(fileName, wDoc);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 文档另存为其他文件名
/// <summary>
/// 文档另存为其他文件名
/// </summary>
public static bool SaveAs(string fileName, Word.Document wDoc)
{
Object FileName = fileName; // 文档的名称。默认值是当前文件夹名和文件名。如果文档在以前没有保存过,则使用默认名称(例如,Doc1.doc)。如果已经存在具有指定文件名的文档,则会在不先提示用户的情况下改写文档。
Object FileFormat = Word.WdSaveFormat.wdFormatDocument; // 文档的保存格式。可以是任何 WdSaveFormat 值。要以另一种格式保存文档,请为 SaveFormat 属性指定适当的值。
Object LockComments = false; // 如果为 true,则锁定文档以进行注释。默认值为 false。
Object Password = System.Type.Missing; // 用来打开文档的密码字符串。(请参见下面的备注。)
Object AddToRecentFiles = false; // 如果为 true,则将该文档添加到“文件”菜单上最近使用的文件列表中。默认值为 true。
Object WritePassword = System.Type.Missing; // 用来保存对文件所做更改的密码字符串。(请参见下面的备注。)
Object ReadOnlyRecommended = false; // 如果为 true,则让 Microsoft Office Word 在打开文档时建议只读状态。默认值为 false。
Object EmbedTrueTypeFonts = false; //如果为 true,则将 TrueType 字体随文档一起保存。如果省略的话,则 EmbedTrueTypeFonts 参数假定 EmbedTrueTypeFonts 属性的值。
Object SaveNativePictureFormat = true; // 如果图形是从另一个平台(例如,Macintosh)导入的,则 true 表示仅保存导入图形的 Windows 版本。
Object SaveFormsData = false; // 如果为 true,则将用户在窗体中输入的数据另存为数据记录。
Object SaveAsAOCELetter = false; // 如果文档附加了邮件程序,则 true 表示会将文档另存为 AOCE 信函(邮件程序会进行保存)。
Object Encoding = System.Type.Missing; // MsoEncoding。要用于另存为编码文本文件的文档的代码页或字符集。默认值是系统代码页。
Object InsertLineBreaks = true; // 如果文档另存为文本文件,则 true 表示在每行文本末尾插入分行符。
Object AllowSubstitutions = false; //如果文档另存为文本文件,则 true 允许 Word 将某些符号替换为外观与之类似的文本。例如,将版权符号显示为 (c)。默认值为 false。
Object LineEnding = Word.WdLineEndingType.wdCRLF;// Word 在另存为文本文件的文档中标记分行符和换段符。可以是任何 WdLineEndingType 值。
Object AddBiDiMarks = true;//如果为 true,则向输出文件添加控制字符,以便保留原始文档中文本的双向布局。
try
{
wDoc.SaveAs(ref FileName, ref FileFormat, ref LockComments, ref Password, ref AddToRecentFiles, ref WritePassword
, ref ReadOnlyRecommended, ref EmbedTrueTypeFonts, ref SaveNativePictureFormat
, ref SaveFormsData, ref SaveAsAOCELetter, ref Encoding, ref InsertLineBreaks, ref AllowSubstitutions
, ref LineEnding, ref AddBiDiMarks);
return true;
}
catch (Exception ex)
{
string err = string.Format("另存文件出错,错误原因:{0}", ex.Message);
throw new Exception(err, ex);
}
}
#endregion
#region 关闭文档
/// <summary>
/// 关闭文档
/// </summary>
public void CloseDocument()
{
CloseDocument(wDoc, wApp);
wDoc = null;
wApp = null;
}
#endregion
#region 关闭文档
/// <summary>
/// 关闭文档
/// </summary>
public static void CloseDocument(Word.Document wDoc, Word.Application WApp)
{
Object SaveChanges = Word.WdSaveOptions.wdSaveChanges;// 指定文档的保存操作。可以是下列 WdSaveOptions 值之一:wdDoNotSaveChanges、wdPromptToSaveChanges 或 wdSaveChanges。
Object OriginalFormat = Word.WdOriginalFormat.wdOriginalDocumentFormat;// 指定文档的保存格式。可以是下列 WdOriginalFormat 值之一:wdOriginalDocumentFormat、wdPromptUser 或 wdWordDocument。
Object RouteDocument = false;// 如果为 true,则将文档传送给下一个收件人。如果没有为文档附加传送名单,则忽略此参数。
try
{
if (wDoc != null) wDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
if (WApp != null) WApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 填充书签
/// <summary>
/// 填充书签
/// </summary>
public void ReplaceBookMark(string book, string value)
{
try
{
object bkObj = book;
if (wApp.ActiveDocument.Books.Exists(book) == true)
{
wApp.ActiveDocument.Books.get_Item(ref bkObj).Select();
}
else return;
wApp.Selection.TypeText(value);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}
这个是主程序调用来写doc的private void CreateWord_btn_Click(object sender, EventArgs e)
{
try
{
this.TecGuideWord.CreateNewWordDocument("D:\\123.dot");
this.TecGuideWord.ReplaceBookMark("abc", "123");
this.TecGuideWord.SaveAs("d:\\1.doc");
this.TecGuideWord.CloseDocument();
}
catch (Exception ex)
{
return;
}
}
解决方案 »
- 求教怎么切分图片?
- XML
- 【高手求救】怎样用C#实现类似飞鸽的文件夹及文件的拖放
- 为什么我在VB里可以调用VB.NE的COM DLL,而调用C#的DLL就失败呢?
- DataGridView 合图片行...
- 有没有办法能知道DataTable里的字段属于数据库中的什么数据类型? 就是找出是SqlDbType的值.
- Microsoft.mshtml 的问题,跪求
- 小问题,高手一眼就能看出来.请帮忙告诉错在哪?
- 用子线程调用excel,用尽网上所有释放excel方法...依然无解!!!
- 水晶报表打包问题
- DefaultCellStyle.Format属性后。没有效果
- qq快速登录的clientkey,有没有方法生成??
Word.Document wordDoc = thisApplication.Documents.Add(ref Template, ref NewTemplate, ref DocumentType, ref Visible);
报错 在CreateNewWordDocument里的