获取与幻灯片形状对应的版面形状 [英] Get Layout Shape Corresponding to Slide Shape

查看:45
本文介绍了获取与幻灯片形状对应的版面形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PowerPoint 2007 和使用 VBA 中,如何获得幻灯片母版布局上的占位符形状,它是幻灯片上占位符形状的母版"?

In PowerPoint 2007 and using VBA, how can I get the placeholder shape on a Slide Master layout that is the "master" for a placeholder shape on the slide?

我目前正在使用循环将幻灯片占位符的位置和大小与幻灯片布局中每个占位符形状的位置和形状进行比较,但这并不是万无一失的.例如,如果占位符形状在幻灯片上移动,其位置可能不再与幻灯片布局中任何占位符形状的位置匹配.我可以重新应用幻灯片的布局以将占位符重新定位,但这不是我想要做的.

I am currently using a loop to compare the position and size of the slide placeholder with the position and shape of each placeholder shape in the slide's layout, but this isn't fool-proof. For example, if the placeholder shape is moved on the slide, its position may no longer match the position of any placeholder shapes in the slide's layout. I could reapply the slide's layout to snap placeholders back into position, but that's not what I want to do.

对象模型中像 Shape.Master 这样的东西是理想的,但当然,它不存在.

Something in the object model like Shape.Master would be ideal but, of course, that doesn't exist.

推荐答案

看到这里或其他地方仍然没有答案,我不妨发布我的代码.

Seeing as there is still no answer to this here or elsewhere I might as well post my code.

例如,如果占位符形状在幻灯片上移动,

For example, if the placeholder shape is moved on the slide,

这是我想出的处理方法:

Here's what I came up with to handle that:

  • 存储所有形状的位置
  • 重置幻灯片布局
  • 匹配幻灯片形状和母版幻灯片形状
  • 恢复所有形状的位置.

这是执行此操作并返回 mastershapename - shapename 映射的函数.

This is the function that does that and returns a mastershapename - shapename mapping.

private Dictionary<string, string> GetShapeMasters(Powerpoint.Slide s)
{
    Dictionary<string, string> shapeMasters = new Dictionary<string, string>();
    List<ShapeLocation> shapeLocations = new List<ShapeLocation>();

    //store locations
    foreach (Powerpoint.Shape sh in s.Shapes)
    {
        shapeLocations.Add(new ShapeLocation()
        {
            Name = sh.Name,
            Location = new System.Drawing.RectangleF(sh.Left, sh.Top, sh.Width, sh.Height)
        });
    }

    //have powerpoint reset the slide
    //ISSUE: this changes the names of placeholders without content.
    s.CustomLayout = s.CustomLayout;

    //compare slide and master
    foreach (Powerpoint.Shape sh in s.Shapes)
    {
        foreach (Powerpoint.Shape msh in s.CustomLayout.Shapes)
        {
            if (IsShapeMaster(sh, msh))
            {
                shapeMasters[msh.Name] = sh.Name;
            }
        }
    }

    //restore locations
    //TODO: might be replaced by undo
    foreach (var shm in shapeLocations)
    {
        Powerpoint.Shape sh = null;
        try
        {
            sh = s.Shapes[shm.Name];
        }
        catch 
        {
            //Fails for renamed placeholder shapes.
            //Have yet to find a decent way to check if a shape name exists.
        }

        //placeholders do not need to be restored anyway.
        if (sh != null)
        {
            sh.Left = shm.Location.Left;
            sh.Top = shm.Location.Top;
            sh.Width = shm.Location.Width;
            sh.Height = shm.Location.Height;
        }
    }

    return shapeMasters;
}

有了这个你就可以了

Dictionary<string, string> shapeMasters = GetShapeMasters(theSlide);
if(shapeMasters.ContainsKey("KnownPlaceholderName"))
    Powerpoint.Shape KnownShape = theSlide[shapeMasters["KnownPlaceholderName"]];

这是比较函数,它采用两个形状并检查它们是否相等".可以扩展以使其更精确.

And here is the comparison function that takes two shapes and checks if they are "equal". Could be extended to make it more precise.

private bool IsShapeMaster(Powerpoint.Shape sh, Powerpoint.Shape msh)
{
    return
        sh.Left == msh.Left
        && sh.Top == msh.Top
        && sh.Width == msh.Width
        && sh.Height == msh.Height
        && sh.Type == msh.Type
        && sh.PlaceholderFormat.Type == msh.PlaceholderFormat.Type;
}

存储原始形状位置的小类

Little class that stores original shape location

class ShapeLocation
{
    public string Name;
    public System.Drawing.RectangleF Location;
}

这是来自 C# VSTO 插件的代码,但我认为它与 VB 或其他 PPT 自动化类型没有什么不同.

This is code from a C# VSTO Add-in but I suppose it is not that different from VB or other PPT automation types.

这篇关于获取与幻灯片形状对应的版面形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆