我创建了一个c#程序,它使用MergeFields和模板打印Word文档。
当我第二次/第三次运行程序时。程序每次打印出程序的第一次迭代。
有什么想法吗?
迭代中似乎没有剩余的打印作业。
正在使用的脚本位于
之下Word._Application wordApplication = new Word.Application();
Word.Document wordDocument = new Word.Document();
try
{
Word.Selection selection = wordApplication.Selection;
foreach (Word.Field myMergeField in wordDocument.Fields)
{
Word.Range rngFieldCode = myMergeField.Code;
String fieldText = rngFieldCode.Text;
if (fieldText.StartsWith(" MERGEFIELD"))
{
Int32 endMerge = fieldText.IndexOf("\\");
Int32 fieldNameLength = fieldText.Length - endMerge;
String fieldName = fieldText.Substring(11, endMerge - 11);
fieldName = fieldName.Trim();
#region Merge fields
// Principal Contractor
if (fieldName == "strJobName")
{
myMergeField.Select();
if (summary.GetName() == String.Empty || summary.GetName() == null)
{
selection.TypeText(" ");
}
else
{
selection.TypeText(summary.GetName());
}
}
#endregion
}
}
object copies = "1";
object pages = "";
object range = Word.WdPrintOutRange.wdPrintAllDocument;
object items = Word.WdPrintOutItem.wdPrintDocumentContent;
object pageType = Word.WdPrintOutPages.wdPrintAllPages;
object oTrue = true;
object oFalse = false;
wordDocument.PrintOut(ref oTrue, ref oFalse, ref range, Type.Missing, Type.Missing, Type.Missing,
ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oTrue,
Type.Missing, ref oFalse, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
// Clean up!
if (wordDocument != null) wordDocument.Close();
wordApplication.Quit(Type.Missing, Type.Missing, Type.Missing);
}