Skip to content

NewDocumentFromTemplate: create the drawing from the template, reject stencils (#229) - #235

Merged
saveenr merged 1 commit into
masterfrom
fix-229-new-document-from-template
Oct 1, 2026
Merged

saveenr merged 1 commit into
masterfrom
fix-229-new-document-from-template

Conversation

@saveenr

@saveenr saveenr commented Oct 1, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes #229. Client.Document.NewDocumentFromTemplate (and so New-VisioDocument -Template and the directed graph XML <documentoptions template="..."/>) did not create the drawing from the template. It created a blank drawing and then opened the template as a separate docked stencil (AddEx with visAddStencil + visOpenDocked, flags copied from the 2012 NewStencil helper), which left an empty stencil open in Visio 2013 and later.

It now calls AddEx with no flags, so Visio creates the drawing from the template: its page setup, styles and settings apply, and the stencils in the template's workspace open and dock.

  • null, empty or whitespace template: a blank drawing (as before for null).
  • a stencil file (.vss / .vssx): ArgumentException ("... is a stencil, not a template ... use -Stencil") before anything is created. Before, it failed with an opaque COM error after leaving a blank drawing open.
  • a template that does not exist: throws, with no stray blank drawing.

Evidence (live Visio 16, BASFLO_U.VSTX)

Call Result
old: blank Add("") + AddEx(template, 0, 516, 0) Blank 8.5 x 11 drawing with an empty Template, plus an empty "Stencil" document.
AddEx(template, 0, 0, 0) or Documents.Add(template) Drawing whose Template is set, landscape 11 x 8.5, with BASFLO_U.vssx (15 masters) and XFUNC_U.vssx (7 masters) opened and docked.
AddEx(template, 0, 256, 0) (visAddNoWorkspace) Drawing based on the template, no stencils opened.
AddEx("basflo_u.vssx", 0, 516, 0) Throws. So no caller could have relied on "stencil as template".

Microsoft's Documents.AddEx documentation describes the template form the same way (and visAddStencil as "Adds a new stencil file"). I could not test Visio 2010 itself.

Behavior change

Anyone who passed a template used to get a blank drawing and now gets one based on the template, which is what the docs promised. Anyone who passed a stencil got an error and still does, but a clear one. No internal caller passes a template or a stencil (the developer commands pass null).

Testing

  • 7 new tests; 4 of them failed against the old code (real template, empty/whitespace template, stencil file, missing template). They are in VTest.Scripting, VTest.Models (the XML template option now checks the drawing is based on the template) and VTest.PowerShell (New-VisioDocument -Template, and the stencil error).
  • Debug build, local Visio: VTest.Models 104/104, VTest.Scripting 48/48, VTest.PowerShell 84/84 (VTest, 108 tests, not touched).

Also

  • NuGet/CHANGELOG.md and VisioPowerShell/CHANGELOG.md [Unreleased]: Fixed entries. The caveat on the XML option's Added entry is removed.
  • docs/TESTING.md, CLAUDE.md and the Models docs backlog entry updated.
  • Companion docs PRs in saveenr/VisioAutomation_GitBook_Docs (XML format page) and saveenr/VisioPowerShellDocs (New-VisioDocument page).

Closes #229

🤖 Generated with Claude Code

… stencils

NewDocumentFromTemplate created a blank drawing and then opened the template
as a docked stencil (AddEx with visAddStencil + visOpenDocked, flags copied
from the 2012 NewStencil helper), so the drawing was never based on the
template and, in Visio 2013 and later, an empty stencil was left open.
It now calls AddEx with no flags, which gives a drawing with the template's
page setup, styles and settings and opens the stencils in its workspace.

- null, empty or whitespace template: a blank drawing (as before for null)
- a stencil file (.vss/.vssx): ArgumentException before anything is created
  (it used to fail with an opaque COM error after leaving a blank drawing)
- a template that does not exist: throws, with no stray blank drawing

Verified against a live Visio 16: Documents.Add(template) and AddEx(...,0,0)
open BASFLO_U.vssx and XFUNC_U.vssx and keep the template's landscape page;
the old call left an empty Stencil2. 7 new tests (4 failed before the change)
across VTest.Scripting, VTest.Models (XML template option) and
VTest.PowerShell (New-VisioDocument -Template).

Closes #229

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@saveenr
saveenr enabled auto-merge October 1, 2026 16:37
@saveenr
saveenr merged commit aa6c876 into master Oct 1, 2026
2 checks passed
@TheSevenPens
TheSevenPens deleted the fix-229-new-document-from-template branch October 1, 2026 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NewDocumentFromTemplate creates a blank document and opens the template as a stencil, instead of basing the document on it

2 participants