使用vba

时间:2015-11-12 22:25:26

标签: vba powerpoint-vba powerpoint-2013

我对VBA相对较新,一般只有非常有限的编程经验,但我真的很感激一些帮助!

最终目标是将PPT中文本框中的(格式化)文本作为演示文稿之间的变量传递。我认为将(格式化的)文本作为变量传递是很重要的,因为该变量将用于生成电子邮件的正文(代码的一部分已完成,但我试图创建胆量)这个变量在这里)。不幸的是,我不知道如何在VBA中传递变量。我想我已经弄清楚如何抓取文本,但简单的格式(粗体,文本大小差异等)丢失了。请帮忙? : - )

Dim headlines headlines = ActivePresentation.Slides(1).Shapes(1).TextFrame.TextRange.Text

2 个答案:

答案 0 :(得分:1)

您可以设置TextFrame类型的对象变量,然后将其设置为您的形状的TextFrame,以便在各种应用之间传递它。

e.g。

Dim myFormattedText as TextFrame ' or TextFrame2 to access all of the newer properties
Set myFormattedText = ActivePresentation.Slides(1).Shapes(1).TextFrame ' or TextFrame2

这样你就拥有了该对象中的所有文本属性。

答案 1 :(得分:1)

这应该有助于你前进。它复制一个形状对象,其中包含所需的所有格式属性,从一个打开的演示文稿到另一个打开的演示文稿。实际上,您可以根据您要实现的目标使演示,幻灯片和形状参考动态化,但这是一个展示原则的实例:

Option Explicit

' Macro to copy the first shape from the first slide of open presentation 1 to the first slide of open presentation 2
' Requires that 2 presetnations are open and that the first has a shape on slide 1
' Wriiten by Jamie Garroch of youpresent.co.uk
Sub PassTextBoxBetweenPresentations()
  Dim oSrcShp As Shape ' Source Shape in Presentation 1
  Dim oTgtShp As Shape ' Source Shape in Presentation 2

  ' Set a reference to a shape in the first presentation, in this case, the first shape on the first slide
  Set oSrcShp = Presentations(1).Slides(1).Shapes(1)

  ' Copy the shape (with all of its properties) to the clipboard
  oSrcShp.Copy

  ' Paste the shape from the first presentation to the second presentation
  Presentations(2).Slides(1).Shapes.Paste

  ' Set a reference to the pasted shape
  Set oTgtShp = Presentations(2).Slides(1).Shapes(Presentations(2).Slides(1).Shapes.Count)

  ' Do stuff with your pasted shape object
  With oTgtShp
    Dim headlines
    If .HasTextFrame Then
      If .TextFrame.HasText Then
        headlines = .TextFrame.TextRange.Text
        Debug.Print headlines
      End If
    End If
  End With

  ' Clean up
  Set oSrcShp = Nothing: Set oTgtShp = Nothing
End Sub