关于winform程序中操作word的问题

zty0527 2006-12-22 01:34:13
我在winform中调用word 2000
word2000 已经添加到工程中Interop.Word.dll,不能替换模板中的变量
我需要动态生成多个word文件,个数从ds中读取的
代码如下

public ArrayList swd(string strFName, string strFind)
{
ArrayList textsFound = new ArrayList(); // matched texts
object missingValue = Type.Missing;
object missing = Type.Missing;
Word.ApplicationClass wdApp = null; // Word Application object

try
{
object fName = strFName as object;
wdApp = new ApplicationClass(); // create a Word application object
Word.Document wdDoc = wdApp.Documents.Open(ref fName, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue); // open a Word object

// the Word object has paragraphs or not
if (wdDoc.Paragraphs != null && wdDoc.Paragraphs.Count > 0)
{
int count = wdDoc.Paragraphs.Count; // the number of doc paragraphs
Word.Range rng; // Word.Range object
Word.Find fnd; // Word.Find object

Console.WriteLine("There are {0} paragraphs in document '{1}'.", count, strFName);



object replaceAll = Word.WdReplace.wdReplaceAll;


wdApp.Selection.Find.ClearFormatting();
wdApp.Selection.Find.Text = "#xjh#";

wdApp.Selection.Find.Replacement.ClearFormatting();
wdApp.Selection.Find.Replacement.Text = "123";

wdApp.Selection.Find.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref replaceAll, ref missing, ref missing, ref missing, ref missing);

string tt = wdDoc.Versions.ToString();
int aa = wdApp.Version.Length;
for (int i = 0; i < aa; i++)
{
tt = wdApp.Version[i].ToString();
}
object comment = "My first save.";
this.VERSIONS.Save(ref comment);

foreach (Word.Version version in this.VERSIONS)
{
tt = version.Index.ToString();
}
//for (int i = 1; i <= count; ++i) // search key words in every paragraphs
//{
// rng = wdDoc.Paragraphs[i].Range;

// fnd = rng.Find;
// fnd.ClearFormatting();
// fnd.Text = strFind;

// if (fnd.Execute(ref missingValue, ref missingValue,
// ref missingValue, ref missingValue, ref missingValue,
// ref missingValue, ref missingValue, ref missingValue,
// ref missingValue, ref missingValue, ref missingValue,
// ref missingValue, ref missingValue, ref missingValue,
// ref missingValue))
// {
// // if find the matched paragrahps, add it into the textsFound ArrayList.
// wdDoc.Paragraphs[i].Range.Text = wdDoc.Paragraphs[i].Range.Text.Replace(strFind, "ttttt");
// textsFound.Add("[" + i.ToString() + "] " + wdDoc.Paragraphs[i].Range.Text.Trim());
// }
//}
//wdDoc.Save();
//wdDoc.Close();
}
}
catch (NullReferenceException e)
{
Console.WriteLine(e.ToString());
wdApp.Quit(ref missingValue, ref missingValue, ref missingValue);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
wdApp.Quit(ref missingValue, ref missingValue, ref missingValue);
}

// release the Word application object
object SaveOption, OriginalFormat, RouteDocument;

SaveOption = Word.WdSaveOptions.wdSaveChanges;

OriginalFormat = Word.WdOriginalFormat.wdPromptUser;

RouteDocument = missingValue;
wdApp.Quit(ref SaveOption,ref OriginalFormat,ref RouteDocument);

//wdApp.Quit(ref missingValue, ref missingValue, ref missingValue);

return textsFound;
}
...全文
211 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zty0527 2006-12-22
  • 打赏
  • 举报
回复
谢谢

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧