asp.net里操作word.关于改写模式?(在线等待,菜鸟跪求)
(在代码里启动了改写模式.为什么运行后没效果.依然是普通的插入模式)
private Microsoft.Office.Interop.Word.Application appword = new Microsoft.Office.Interop.Word.Application();
#region ------------生成合同--------------
public void GetWord()
{
try
{
string TemplateFile = @"D:\WorkSpace\text\合同模板.doc";
//生成的具有模板样式的新文件
string FileName = @"D:\WorkSpace\text\" + BookMarkhtbianhao + ".doc";
File.Copy(TemplateFile, FileName);
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
object Obj_FileName = FileName;
object Visible = false;
object ReadOnly = false;
object missing = System.Reflection.Missing.Value;
appword.Options.Overtype = true;
//打开文件
doc = appword.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref Visible,
ref missing, ref missing, ref missing,
ref missing);
foreach (Bookmark bank in doc.Bookmarks)
{
switch (bank.Name)
{
case "BookMarkcgrdlr":
bank.Select();
SelectionInsertText(BookMarkcgrdlr.Trim());
break;
case "BookMarkcgrdw":
bank.Select();
SelectionInsertText(BookMarkcgrdw.Trim());
break;
case "BookMarkcgrfrdb":
bank.Select();
SelectionInsertText(BookMarkcgrfrdb.Trim());
break;
case "BookMarkcgryhang":
bank.Select();
bank.Range.Text = "";
break;
case "BookMarkcgryouzhengbh":
bank.Select();
bank.Range.Text = "";
break;
case "BookMarkcgrzhanghao":
bank.Select();
bank.Range.Text = "";
break;
default:
bank.Select();
bank.Range.Text = "";
break;
}
}
object IsSave = true;
doc.Close(ref IsSave, ref missing, ref missing);
//杀掉进程
killAllProcessDan();
}
catch (Exception ex)
{
}
finally
{
//杀掉进程
killAllProcessDan();
}
}
#endregion
#region -----------------在当前光标位置插入内容----------------
/// <summary>
/// 在当前光标位置插入内容
/// </summary>
/// <param name="text">内容</param>
public void SelectionInsertText(string text)
{
Microsoft.Office.Interop.Word.Selection currentSelection = appword.Selection;
// Store the user's current Overtype selection
bool userOvertype = appword.Options.Overtype;
// Make sure Overtype is turned off.
//if (!appword.Options.Overtype)
//{
// appword.Options.Overtype = true;
//}
appword.Options.Overtype = true;
// Test to see if selection is an insertion point.
if (currentSelection.Type == Microsoft.Office.Interop.Word.WdSelectionType.wdSelectionIP)
{
//currentSelection.TypeText("Inserting at insertion point. ");
currentSelection.TypeText(text);
//currentSelection.TypeParagraph();
}
else
{
if (currentSelection.Type == Microsoft.Office.Interop.Word.WdSelectionType.wdSelectionNormal)
{
// Move to start of selection.
if (appword.Options.ReplaceSelection)
{
object direction = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseStart;
currentSelection.Collapse(ref direction);
}
//currentSelection.TypeText("Inserting before a text block. ");
currentSelection.TypeText(text);
//currentSelection.TypeParagraph();
}
else
{
// Do nothing.
}
}
// Restore the user's Overtype selection
appword.Options.Overtype = true;
}