Automating Sub-Shape Labeling in Visio: A Handy VBA Solution!

Started by Yacine, July 17, 2025, 11:34:28 AM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Yacine

Hello guys

I've been working on a cool feature for labeled shapes in Visio, and I thought I'd share it with you all. Imagine this: you've got a group shape, and you want to add a sub-shape that:

  • Displays the value of a field from its parent shape.
  • Dynamically positions itself near the parent, keeping its body away but its center aligned.
  • Maintains its angle towards the page, regardless of the parent shape's rotation, ensuring the text is always readable and aligned.

Nice smartshape, but reusing it can be a real hassle. So why not let a VBA module build the shape for you automatically?

What Does the Code Do?
This VBA module simplifies the creation of labeled sub-shapes for any group shape. Here's what it handles:

Orientation Details
  • Positioning (PinX/PinY): The sub-shape's center (PinX and PinY) is dynamically aligned with the parent shape's control point. This ensures the sub-shape stays close to the parent while keeping its body away.
  • Text Orientation: The text alignment (left/right/center and bottom/center/top) is managed to ensure readability. The sub-shape adjusts its alignment based on its position relative to the parent.

    Rotation with SetAtRef
    Using SetAtRef and SetAtRefExpr, the sub-shape can rotate independently of the parent. However, when the parent shape is rotated, the sub-shape retains its new angle, ensuring consistent orientation.

    Field Display or Free Text
  • If a field is provided, the sub-shape displays the value of that field from the parent.
  • If no field is provided, you can freely type in the parent shape, and the sub-shape will reflect the edited text.

Auto-Width and Reset Action
  • The sub-shape has an auto-width feature that adjusts its size based on the text content.
  • An action is available to reset the auto-size if you overwrite it manually.

Formatting Protections
  • Line Pattern: The line pattern of the sub-shape is protected, allowing you to format the parent shape without affecting the sub-shape.
  • Fill Pattern: The fill pattern is left unguarded, giving you the flexibility to style the sub-shape as you prefer.

    How to Use It?
    1. Add the provided VBA code to your Visio project.
    2. Call the addLabel  subroutine, passing your parent shape and the desired field name.
    3. Watch as the sub-shape is created, positioned, and linked to the parent field—all automatically!
    4. Afterward you may add insertFieldTextIntoSubshape to link the sub-shape's text to a field of the parent.

    Why Is This Useful?
    This solution is perfect for anyone working with complex group shapes in Visio. Whether you're creating process diagrams, organizational charts, or technical schematics, this code saves you time and ensures consistency.

    The code
    I'll put the code in a separate post to overcome the size limit of this post.

    Try It Out!
    Feel free to grab the code and give it a try. I'd love to hear your feedback or see how you're using it in your projects. Let's make Visio even more powerful together!

    You cannot view this attachment.



Yacine

Yacine

Yacine

Browser ID: smf (is_webkit)
Templates: 4: index (default), Display (default), GenericControls (default), GenericControls (default).
Sub templates: 6: init, html_above, body_above, main, body_below, html_below.
Language files: 4: index+Modifications.english (default), Post.english (default), Editor.english (default), Drafts.english (default).
Style sheets: 4: index.css, attachments.css, jquery.sceditor.css, responsive.css.
Hooks called: 247 (show)
Files included: 32 - 1218KB. (show)
Memory used: 1055KB.
Tokens: post-login.
Cache hits: 14: 0.07386s for 26,583 bytes (show)
Cache misses: 3: (show)
Queries used: 19.

[Show Queries]