Joe K
Joe K

Reputation: 85

Pass text with formatting as variable from file to file using vba

I'm relatively new to VBA and have only very limited programming experience in general, but would really appreciate some help!

The ultimate goal is to pass (formatted) text from a text box in PPT as a variable between presentations. I believe it is important that the (formatted) text be passed as a variable, because the variable will be used to generate the body of an email (that part of the code is done, but I'm trying to create the guts of that variable here). Unfortunately, I have no idea how to pass a variable in VBA. I think I've figured out how to grab the text, but the simple formatting (bold, text size differences, etc) is lost. Help please? :-)

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

Upvotes: 1

Views: 1292

Answers (2)

Jamie Garroch - MVP
Jamie Garroch - MVP

Reputation: 2979

This should help get you going. It copies a shape object, which contains all of the formatting properties you want, from one open presentation to a second open presentation. In reality, you would make the presentation, slide and shape references dynamic based on what you're trying to achieve but this is a working example to demonstrate the principle:

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

Upvotes: 1

Jamie Garroch - MVP
Jamie Garroch - MVP

Reputation: 2979

You could set an object variable of type TextFrame and then set that to your shape's TextFrame in order to pass it between various apps.

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

That way you have all of the text properties within that object.

Upvotes: 1

Related Questions