Example: replace a snippet by name

Created: 2026-07-11 · Updated: 2026-07-11

This example finds all shapes with a given technical name (e.g. Kundenlogo) and replaces each hit with a snippet chosen in the dialog — optionally across all slides. Classic use: swap an old logo throughout an entire deck in one go.

It combines the @param snippet picker with the ppptools.ReplaceShape(...) helper.

Prerequisite: a technical name

For the search to match, the target shapes need an object name (e.g. Kundenlogo). Snippets set this automatically via the Technical name field when saving — so the inserted object stays findable and replaceable by name later.


The snippet

// ╔════════════════════════════════════════════════════════════════════╗
// ║  Replace snippet — finds shapes by technical name and replaces      ║
// ║  each hit with a snippet chosen in the dialog                       ║
// ║  (e.g. old customer logo → new logo).                               ║
// ╚════════════════════════════════════════════════════════════════════╝

// @help https://ppptools.help.happy-pc.ch/en/ase-example-replace-snippet
/* PARAMS */

// @param string  TechName  "Technical name"    default="Kundenlogo" tooltip="Object name of the shapes to replace (exact)"
// @param snippet Ersatz    "Replacement"       subtype="Fragment"   tooltip="What to replace with (a logo from the library)"
// @param enum    Scale     "Scale by"          options="Breite|Höhe|Einpassen|Strecken|Original" default="Breite"
// @param bool    KeepName  "Keep name"         default="true"       tooltip="Replacement keeps the same technical name (stays replaceable)"
// @param bool    AllSlides "All slides?"       default="true"
// @button Replace

string techName  = Params.GetString("TechName", "");
var    ersatz    = Params.GetSnippet("Ersatz");
string scale     = Params.GetString("Scale", "Breite");
bool   keepName  = Params.GetBool("KeepName", true);
bool   allSlides = Params.GetBool("AllSlides", true);

int replaced = 0;

// Replaces every shape on ONE slide whose Name == techName.
// Important: collect the hits first (snapshot), then replace — ReplaceShape
// mutates the Shapes collection (deletes the original, inserts the replacement).
void ProcessSlide(System.Collections.Generic.List<dynamic> shapes)
{
    var targets = new System.Collections.Generic.List<dynamic>();
    foreach (dynamic sh in shapes)
    {
        string name = "";
        try { name = (string)sh.Name; } catch { }
        if (string.Equals(name, techName, System.StringComparison.OrdinalIgnoreCase))
            targets.Add(sh);
    }

    foreach (dynamic t in targets)
        if (ppptools.ReplaceShape(t, ersatz, scale, keepName) != null)
            replaced++;
}

// Only run when a replacement snippet is chosen and a name is given.
if (ersatz != null && !string.IsNullOrWhiteSpace(techName))
{
    if (allSlides)
        for (int s = 1; s <= ppptools.SlideCount; s++)
            ProcessSlide(ppptools.GetAllShapes(ppptools.GetSlide(s)));
    else
        ProcessSlide(ppptools.GetAllShapes());
}

The enum values (Breite, Höhe, Einpassen, Strecken, Original) are language-neutral tokens read by ReplaceShape.


How it works

  1. The picker (@param snippet … subtype="Fragment") shows only Fragment snippets with a preview — you pick the new logo.
  2. The script scans the slide(s) for shapes whose object name exactly matches the technical name (case-insensitive).
  3. Each hit is replaced via ReplaceShape: the new snippet lands at the centre of the old one, is sized per Scale by, and the original is deleted.
  4. With keep name, the replacement carries Kundenlogo again — so the action can be repeated any number of times.

Example scenario

A 30-slide deck shows the logo Kundenlogo on every slide. The client rebranded:

  1. Save the new logo once as a Fragment snippet (technical name e.g. Kundenlogo).
  2. Run this snippet → in the dialog set Technical name = Kundenlogo, Replacement = new logo, Scale by = Breite, All slides = yes.
  3. Replace → all 30 logos are swapped, each scaled to logo width and positioned in the same spot.

Variants

  • Partial match instead of exact (e.g. Kundenlogo, Kundenlogo_DE, …): replace the comparison line with csharp if (name.IndexOf(techName, System.StringComparison.OrdinalIgnoreCase) >= 0)
  • Pictures only: use subtype="Picture" instead of "Fragment" in @param snippet.
  • Rotation is carried over by ReplaceShape automatically.

Groups

Only top-level shapes are searched. A logo inside a group won't be found — ungroup first, or name and replace the group itself.