VBA - PowerPoint宏 - 将文本框内容添加到大纲视图

时间:2016-06-21 11:56:53

标签: vba powerpoint

我有从软件自动生成的PowerPoints。该软件将内容(文本)放入文本框而不是占位符。我需要创建并运行一个宏,将所有文本添加到Outline视图(用于辅助功能)。

我有一个脚本,可以将文本框内容移动到占位符中,默认情况下会显示在大纲视图中。唯一的问题是它没有保留样式(带子弹的项目符号列表不起作用)。当我将一个幻灯片中的多个文本框组合到一个占位符中时,样式变得特别成问题。

有什么想法吗?

这是我当前的脚本(重要的东西):

For Each sld In ActivePresentation.Slides
 With ActivePresentation
 sld.CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(2)

 Set hypCollection = New hyperColl 'Set the collection of arrays - 1 for each shape

 Set shp = sld.Shapes(1)


For j = sld.Shapes.Count To 1 Step -1
     Set shp = sld.Shapes(j)
     bolCopy = False
     If j = 3 Then
         sld.Shapes.Placeholders.Item(1).TextFrame.TextRange = shp.TextFrame.TextRange.Characters
         sld.Shapes.Placeholders.Item(1).Visible = msoTrue
         shp.Delete

    ElseIf j > 3 And shp.Type = msoTextBox Then
      sld.Shapes.Placeholders.Item(2).TextFrame.TextRange.InsertBefore (shp.TextFrame.TextRange.TrimText) '.ParagraphFormat.Bullet.Type = shp.TextFrame.TextRange.ParagraphFormat.Bullet.Type
         If hypCollection.Exists(shp.Name) Then
              hypArray = hypCollection.GetArray(shp.Name)
              For i = LBound(hypArray) To UBound(hypArray)
                  Set hypToAdd = hypArray(i)
                 With sld.Shapes.Placeholders.Item(2).TextFrame.TextRange.Characters(hypToAdd.getchrStart, Len(hypToAdd.getHypText)).ActionSettings.Item(1)
                       .Action = ppActionHyperlink
                       .Hyperlink.Address = hypToAdd.getHypAddr
                 End With
              Next i
         End If

      shp.Delete
    End If
 Next j
 End With
 Next sld

以下是一些例子: 第一张图片就是我的开头: enter image description here

这是运行我的脚本后的样子: enter image description here

这就是我想要的样子(简单地保持格式化): enter image description here

2 个答案:

答案 0 :(得分:0)

会重置幻灯片帮助吗?

您可以添加以下行:

CommandBars.ExecuteMso(“SlideReset”)

就在之前:

下一个

这应该将文本框中的格式设置为它在主文件上的格式。

答案 1 :(得分:0)

修复是将Paste Special添加到新占位符而不替换所有内容。由于我以相反的顺序迭代文本框,我只是将每个TextBox复制,然后将特殊粘贴复制到位置0的占位符(将所有当前内容保留在那里)。

我将代码转换为C#,这是完整的解决方案:

private void FixPPTDocument()
    {
        PPT.Application pptApp = new PPT.Application();
        PPT.Shape currShp;
        PPT.Shape shp2;



        if (File.Exists((string)fileLocation))
        {
            DateTime today = DateTime.Now;
            PPT.Presentation pptDoc = pptApp.Presentations.Open(fileLocation, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse);

            foreach (PPT.Slide slide in pptDoc.Slides)
            {
                slide.CustomLayout = pptDoc.Designs[1].SlideMaster.CustomLayouts[2];
                for (int jCurr = slide.Shapes.Count; jCurr >= 1; jCurr--)
                {
                    currShp = slide.Shapes[jCurr];
                    if (jCurr == 3)
                    {
                        slide.Shapes.Placeholders[1].TextFrame.TextRange.Text = currShp.TextFrame.TextRange.Text;
                        slide.Shapes.Placeholders[1].Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
                        currShp.Delete();
                    }
                    else if (jCurr > 3 && currShp.Type == Microsoft.Office.Core.MsoShapeType.msoTextBox)
                    {
                        currShp.TextFrame.TextRange.Copy();
                        slide.Shapes.Placeholders[2].TextFrame.TextRange.Characters(0, 0).PasteSpecial();
                        currShp.Delete();
                    }
                }
            }
            pptDoc.SaveAs(fileNewLocation);
            pptDoc.Close();
            MessageBox.Show("File created!");
        }
    }