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

插入包含选定内容的公式的 = (Formula) 域。Inserts an = (Formula) field that contains a formula at the selection.

语法Syntax

表达式Formula(Formula, NumberFormat)expression.Formula (Formula, NumberFormat)

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

参数Parameters

名称Name 必需/可选Required/Optional 数据类型Data type 说明Description
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

如果所选内容未折叠,则该公式将替换所选内容。The formula replaces the selection, if the selection is not collapsed.

如果正在使用电子表格应用程序(如 Microsoft Office Excel),那么在文档中嵌入全部或部分工作表比在表格中使用 = (Formula) 域更简单易行。If you are using a spreadsheet application, such as Microsoft Office Excel, embedding all or part of a worksheet in a document is often easier than using the = (Formula) field in a table.

如果所选内容位于单元格中, 且包含插入点的单元格的上方或左侧至少有一个包含值的单元格, 则 Formula 参数是可选的。The Formula argument is optional only if the selection is in a cell and there is at least one cell that contains a value above or to the left of the cell that contains the insertion point. 如果插入点上方的单元格不包含值,插入的字段是 {= SUM(ABOVE)};如果单元格左侧插入的点,包含值时,插入的字段是 sum。If the cells above the insertion point contain values, the inserted field is {=SUM(ABOVE)}; if the cells to the left of the insertion point contain values, the inserted field is {=SUM(LEFT)}. 如果插入点上方的单元格和其左侧的单元格都包含值, Microsoft Word 将使用以下规则来确定要插入的 SUM 函数:If both the cells above the insertion point and the cells to the left of it contain values, Microsoft Word uses the following rules to determine which SUM function to insert:

  • 如果紧邻插入点上面的单元格中含有数值,则 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

本示例在活动文档的开始处创建一个三行三列的表格,然后计算第一列中所有数值的平均值。This example creates a table with three rows and three columns at the beginning of the active document and then calculates the average of all the numbers in the first column.

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)"

本示例插入一个公式域, 该字段从名为 "GrossSales" 的书签所表示的值中减去。The example inserts a formula field that is subtracted from a value represented by the bookmark named "GrossSales." 结果设置为美元符号。The result is formatted with a dollar sign.

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

另请参阅See also

Selection 对象Selection Object

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.