使用vba将格式为变量的文本从一个文件传递到另一个文件

使用vba将格式为变量的文本从一个文件传递到另一个文件,vba,powerpoint,powerpoint-2013,Vba,Powerpoint,Powerpoint 2013,我对VBA比较陌生,一般来说只有非常有限的编程经验,但非常感谢您的帮助 最终目标是将PPT中文本框中的文本作为演示之间的变量传递(格式化)。我认为将(格式化的)文本作为变量传递是很重要的,因为该变量将用于生成电子邮件的正文(这部分代码已经完成,但我正在尝试创建该变量的核心)。不幸的是,我不知道如何在VBA中传递变量。我想我已经知道了如何抓取文本,但是简单的格式(粗体、文本大小差异等)丢失了。请帮忙?:-) 暗淡的标题 标题=ActivePresentation.Slides(1).Shapes(

我对VBA比较陌生,一般来说只有非常有限的编程经验,但非常感谢您的帮助

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

暗淡的标题

标题=ActivePresentation.Slides(1).Shapes(1).TextFrame.TextRange.Text

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

e、 g


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

这将有助于您继续操作。它将包含所需所有格式属性的形状对象从一个打开的演示文稿复制到第二个打开的演示文稿。实际上,您可能会根据您试图实现的目标,使演示文稿、幻灯片和形状参考动态化,但这是一个工作示例来演示以下原则:

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

我看到的问题是变量不接受格式化文本。它们是“字符串”或未格式化的原始文本。最简单的方法是使用复制/粘贴命令piggy从剪贴板上后退,以保留所有格式。如果您在outlook中使用VBA,您只需使用代码ActivePresentation.Slides(1).Shapes(1).TextFrame.TextRange.CopyHmm在(2/2)电子邮件上执行.Paste命令即可,但如何将其从一个文件传递到另一个文件?还有一种方法可以测试它是否正确捕获文本?例如,要在文件之间传递这样的对象,您可以简单地将对象从PowerPoint幻灯片复制到Excel工作表,就像使用UI一样。或者,您可以在所选应用程序(如Excel或PowerPoint)托管的VBE中的代码中引用该对象,并在应用程序之间复制属性。我知道我需要使用代码引用该对象,但老实说,我不知道该如何进行,也不确定如何识别其他位,以便接近我想要的内容。你能给我一个链接吗?还有,我会使用什么样的对象?文本框本身是一个可以传递的对象吗?谢谢,杰米,这绝对有助于我理解你所说的!我想我现在最关心的是,我是否可以将带有格式的文本从PPT传输到Outlook?
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