1

我对 VBA 比较陌生,一般编程经验非常有限,但非常感谢一些帮助!

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

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

4

2 回答 2

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
于 2015-11-20T09:04:10.763 回答
1

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

例如

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

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

于 2015-11-12T23:23:58.633 回答