move_to_field method

move_to_field(field, is_after)

Moves the cursor to a field in the document.

def move_to_field(self, field: aspose.words.fields.Field, is_after: bool):
    ...
ParameterTypeDescription
fieldFieldThe field to move the cursor to.
is_afterboolWhen True, moves the cursor to be after the field end. When False, moves the cursor to be before the field start.

Examples

Shows how to move a document builder’s node insertion point cursor to a specific field.

doc = aw.Document()
builder = aw.DocumentBuilder(doc=doc)
# Insert a field using the DocumentBuilder and add a run of text after it.
field = builder.insert_field(field_code=' AUTHOR "John Doe" ')
# The builder's cursor is currently at end of the document.
self.assertIsNone(builder.current_node)
# Move the cursor to the field while specifying whether to place that cursor before or after the field.
builder.move_to_field(field, move_cursor_to_after_the_field)
# Note that the cursor is outside of the field in both cases.
# This means that we cannot edit the field using the builder like this.
# To edit a field, we can use the builder's MoveTo method on a field's FieldStart
# or FieldSeparator node to place the cursor inside.
if move_cursor_to_after_the_field:
    self.assertIsNone(builder.current_node)
    builder.write(' Text immediately after the field.')
    self.assertEqual('\x13 AUTHOR "John Doe" \x14John Doe\x15 Text immediately after the field.', doc.get_text().strip())
else:
    self.assertEqual(field.start, builder.current_node)
    builder.write('Text immediately before the field. ')
    self.assertEqual('Text immediately before the field. \x13 AUTHOR "John Doe" \x14John Doe\x15', doc.get_text().strip())

See Also