如何使用vsto在word中插入公式?

时间:2013-05-20 06:05:52

标签: c# ms-office vsto

我想在我的word文档中使用公式{= {PAGE} + 1},尝试下面的示例但是没有用

document.Fields.Add(sel.Selection.Range, Word.WdFieldType.wdFieldExpression, @"{PAGE}+1", true);

2 个答案:

答案 0 :(得分:1)

这应该有帮助

_wordApplication.ActiveWindow.ActivePane.View.ShowFieldCodes = true;
try
{
    _wordApplication.Selection.InsertFormula("=1");                      
    _wordApplication.Selection.MoveLeft(WdUnits.wdCharacter, 1);
    _wordApplication.Selection.TypeText("+");
    var field =_wordApplication.ActiveDocument.Fields.Add(_wordApplication.Selection.Range,   Microsoft.Office.Interop.Word.WdFieldType.wdFieldEmpty, "PAGE", true);
    field.Update();

}           
finally
{
  _wordApplication.ActiveWindow.ActivePane.View.ShowFieldCodes = false;
}

答案 1 :(得分:0)

最后我创造了这个:)

 Microsoft.Office.Interop.Word.Application _wordApplication = Globals.ThisAddIn.Application;

_wordApplication.ActiveWindow.ActivePane.View.ShowFieldCodes = true;
try
{
    _wordApplication.Selection.InsertFormula("");
    _wordApplication.Selection.MoveLeft(WdUnits.wdCharacter, 1);
    _wordApplication.Selection.TypeText("=");
    var field = _wordApplication.ActiveDocument.Fields.Add(_wordApplication.Selection.Range, Microsoft.Office.Interop.Word.WdFieldType.wdFieldEmpty, "PAGE", false);

    _wordApplication.Selection.MoveLeft(WdUnits.wdItem, 1);
    _wordApplication.Selection.TypeText("+1");
    field.Update();

}
finally
{
    _wordApplication.ActiveWindow.ActivePane.View.ShowFieldCodes = false;
}