# InsertFormula 方法 (Word)Selection.InsertFormula method (Word)

## 语法Syntax

expression：必需。expression Required. 表示 Selection 对象的变量。A variable that represents a Selection object.

## 参数Parameters

FormulaFormula 可选Optional VariantVariant 希望 = (Formula) 域求值的数学公式。The mathematical formula you want the = (Formula) field to evaluate. 可以使用与电子表格类似的方式引用表格中的单元格。Spreadsheet-type references to table cells are valid. 例如，"=SUM(A4:C4)"指定第四行中的前三个值。For example, "=SUM(A4:C4)" specifies the first three values in the fourth row. 有关 = (Formula) 域的详细内容，请参阅域代码：= (Formula) 域。For more information about the = (Formula) field, see Field codes:= (Formula) field.
NumberFormatNumberFormat 可选Optional VariantVariant = (Formula) 域的结果的格式。A format for the result of the = (Formula) field. 有关可应用的格式类型的信息，请参阅"数字图片 (#)"域开关。For information about the types of formats you can apply, see Numeric Picture (#) field switch.

## 备注Remarks

• 如果紧邻插入点上面的单元格中含有数值，则 Word 插入 {=SUM(ABOVE)}。If the cell immediately above the insertion point contains a value, Word inserts {=SUM(ABOVE)}.

• 如果紧邻插入点上方的单元格不包含值，但紧邻插入点左边的单元格包含值，则 Word 插入 {=SUM(LEFT)}。If the cell immediately above the insertion point does not contain a value but the cell immediately to the left of the insertion point does, Word inserts {=SUM(LEFT)}.

• 如果紧邻插入点上方的单元格和下方的单元格都不包含值，则 Word 插入 {=SUM(ABOVE)}。If neither cell immediately above the insertion point nor the cell immediately below it contains a value, Word inserts {=SUM(ABOVE)}.

• 如果不指定Formula , 并且插入点上方和左边的所有单元格均为空, 则使用 = (Formula) 域将导致错误。If you don't specify Formula and all the cells above and to the left of the insertion point are empty, using the = (Formula) field causes an error.

## 示例Example

``````Set MyRange = ActiveDocument.Range(0, 0)
Set myTable = ActiveDocument.Tables.Add(MyRange, 3, 3)
With myTable
.Cell(1, 1).Range.InsertAfter "100"
.Cell(2, 1).Range.InsertAfter "50"
.Cell(3, 1).Select
End With
Selection.InsertFormula Formula:="=Average(Above)"
``````

``````Selection.Collapse Direction:=wdCollapseStart
Selection.InsertFormula Formula:= "=GrossSales-45,000.00", _
NumberFormat:="\$#,##0.00"
``````