关于winform程序中操作word的问题
我在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;
}