0

With the use of Python (win32com) I want to insert a user defined page number in a table cell of MS Word. Normally in Word if I want to have custom page number different from standard PAGE variable I press Cnrtl+F9 and insert field expression like this {={PAGE}+3}. So I want to get it in Python.

What I tried is inserting field in a table cell:

import win32com.client as win32
word = win32.Dispatch('Word.Application')
document = word.ActiveDocument

myRange = document.Paragraphs(1).Range
myTable = document.Tables.Add(myRange, 5, 5,
                          win32.constants.wdWord8TableBehavior)
cellRange = myTable.Cell(2, 2).Range    

myField = document.Fields.Add(
    cellRange,
    win32.constants.wdFieldEmpty,
    'PAGE',
    True)

In result I get this message:

" File "C:\Users\2E78~1\AppData\Local\Temp\gen_py\3.7\00020905-0000-0000-C000-000000000046x0x8x7\Fields.py", line 35, in Add , Type, Text, PreserveFormatting) pywintypes.com_error: (-2147352567, 'Error.', (0, 'Microsoft Word', 'This command is not available.', 'wdmain11.chm', 37373, -2146823683), None)"

P.S. I tried to record macro in Word to see VBA commands, but it seems to record very basic actions only.

Sib
  • 404
  • 4
  • 16
  • _it seems to record very basic actions only._ It would be more accurate to say that the macro recorder doesn't use the `Range` object, as that isn't an object that can be manipulated by the user via the UI (unlike documents, windows, cells, tables, and fields etc.). – Zev Spitz May 21 '19 at 16:43
  • Perhaps try using one of the [other Word `wdFieldType` constants](https://learn.microsoft.com/en-us/office/vba/api/word.wdfieldtype); maybe `wdFieldExpression` or `wdFieldPage`? Alternatively, you could insert the field manually; then open the VBA editor and programmatically reading the field's [`Type` property](https://learn.microsoft.com/en-us/office/vba/api/word.field.type). – Zev Spitz May 21 '19 at 16:51
  • It's probably due to using the entire cell range as the target range - that would insert the field in the table cell structure, instead of into the cell. Try preceding the Fields.Add line with `cellRange.Collapse win32.constants.wdCollapseStart` – Cindy Meister May 21 '19 at 21:01
  • Your comments helped me both, I changed my code in this way: cellRange = myTable.Cell(2, 2).Range cellRange.Collapse(win32.constants.wdCollapseStart) myField = document.Fields.Add( cellRange, win32.constants.wdFieldPage, '', False) – Sib May 22 '19 at 16:07
  • What I can't get is how to create nested field like {={PAGE}+3}. If I simply set text or code of field like in this expression, Word recognizes brackets as a simple text, not as a field. – Sib May 22 '19 at 16:11
  • This has been answered numerous times, although not for Python. See for example https://stackoverflow.com/a/35065209/3077495, https://stackoverflow.com/a/49805695/3077495, https://stackoverflow.com/a/37243083/3077495, https://stackoverflow.com/a/42144625/3077495 – Cindy Meister May 23 '19 at 15:01
  • Note that people who reply to your question in comments won't get a notification that you've interacted unless you "ping" them by preceding their name with @, for example: @SibColab – Cindy Meister May 23 '19 at 15:02

0 Answers0