使用ConvertTextToSmartArt将文本转换为SmartArt
【e800编译】此示例演示在Microsoft PowerPoint 2010中如何以编程方式将文本转换成不同的内置SmartArt布局,以及如何给索引值和smart art名称创建列表。
此代码段是Office 2010的101项VBA代码示例中的一部分。与其它示例一样,这些将可以直接写入您的代码中。
每块示例代码包含约5至50行的代码,分别演示了一个独特的功能或功能集,在VBA 或VB以及C#中(在Visual Studio 2010中创建)。每个示例之中都会包含代码以及相应注释,这样您就可以直接运行获取预期的结果,或者是根据代码注释提示来调整环境,运行示例代码。
Microsoft Office 2010提供了你所需要的工具来创建功能强大的应用程序。Microsoft Visual Basic Application(VBA)代码示例可以帮助你创建自己的应用程序,以执行特定功能或者以此为出发点实现更为复杂的功能。
实例代码
PowerPoint 2010中支持将文本转换成多种不同的内置SmartArt布局之一。把你的光标放在CreateSmartArtList进程,按F5进入调试窗口,它将列出所有的索引值和smart art名称。从调试窗口将列表复制到一个文本文件,用它作为参考。然后,在功能区中,你可以通过名称定位smart art布局,并把它转换到您刚刚创建的列表中与其匹配的索引。
此示例中,先会创建一个新幻灯片,然后将文本插入形状,最后将文本转换为SmartArt 布局。
Sub ConvertToSmartArtLayoutTest()
Dim sld As Slide
Set sld=ActivePresentation.Slides.Add(1,ppLayoutText)
Dim shp As Shape
Set shp=sld.Shapes(2)
' Create text with Main at the top level, and
' NW, NE, SW, SE as the four child paragraphs.
Dim sampleText As String
sampleText="Main.NW.NE.SW.SE"
sampleText=Replace(text,".",vbCrLf)
' Insert the text into the shape:
With shp.TextFrame.TextRange.Paragraphs
.Text=sampleText
.Lines(3).IndentLevel=2
.Lines(4).IndentLevel=2
.Lines(5).IndentLevel=2
EndWith
' Now that the shape includes text that is appropriate
' for the smart art layout you'll use, convert it to
' the appropriate layout (121, Titled Matrix):
shp.ConvertTextToSmartArtApplication.SmartArtLayouts(121) EndSub
Sub CreateSmartArtList()
Dim i As Integer
For i=1To Application.SmartArtLayouts.Count
Debug.Printi,Application.SmartArtLayouts(i).Name
Next i
EndSub