Stale object reference after ConvertToCurves
After converting or deleting, re-fetch doc.Pages.Item(i) and ActiveLayer, or the next Create call fails with an invalid reference.
Symptom
Stale object reference after ConvertToCurves
Notes
After converting or deleting, re-fetch
doc.Pages.Item(i) and ActiveLayer, or the next Create call fails with an invalid reference.Python example (runnable)
Requires
pywin32. The example assumes app / doc / page / layer are already connected.
See Connection & InitPython
import time
page_index = 2
doc.Pages.Item(page_index).Activate()
shape = doc.ActivePage.Shapes.Item(1)
shape.ConvertToCurves() # the old reference may go stale after converting
time.sleep(0.3)
# re-fetch page and layer so the next Create does not fail on a stale reference
page = doc.Pages.Item(page_index)
layer = page.ActiveLayer
rect = layer.CreateRectangle2(0, 0, 20/25.4, 20/25.4)
print("Created fine after re-fetching the references")
About this section: Pitfalls
Pitfalls — errors that fail without raising.
- This section has 12 entries; this is one of them.
- See the full section index: Pitfalls
Other entries in this section
- Wrong font name fails silentlyNo exception, no font change, no warning. You have to read it back to
- Text.FontProperties swallows the font sizeSetting Size through it does nothing. In batch code you have to go thr
- Collection indexes start at 1Pages.Item(1) is the first page. In Python that means range(1, n+1).
- Objects inside containers have no Left/TopReading Left/Top on a shape inside a PowerClip raises. GetBoundingBox(
- ConvertToCurves is irreversibleAfter ConvertToCurves every text property is gone, and a wrong font is
- Do not read a shape after DeleteAfter Delete, even reading StaticID raises. Wrap traversal bodies in t
- HexValue casing is inconsistentNormalize with .upper() before comparing, or your color filter quietly
- len(doc.Pages) may not workUse doc.Pages.Count instead of len(doc.Pages).