How to control PowerPoint from Word
Article contributed by Dave Rado
The following code sample allows you to insert upside-down or rotated “text” in a Word document. It creates a text box in PowerPoint, puts text in it, formats the text, flips or rotates it (your choice), copies the text box and pastes it into Word as a picture.
You can get the syntax for most things you might want to do within PowerPoint with the aid of PowerPoint's macro recorder.
The code uses Early Binding. It checks to see if PowerPoint is running. If it is, the code uses the existing instance of PowerPoint; if not, the code creates a new instance.
First set a reference to PowerPoint (in the VB Editor, select Tools + References).
Option Explicit
Sub InsertUpsideDownText()
Dim oDoc As Document,
MyRange As Range
Dim oPPT As
PowerPoint.Application
Dim PPTWasNotRunning As
Boolean
Dim oPres As
PowerPoint.Presentation
Dim oSlide As
PowerPoint.Slide, oShape As PowerPoint.Shape
Set oDoc = ActiveDocument
System.Cursor = wdCursorWait
StatusBar = "Starting PowerPoint ..."
'If PPT is running, get a handle on it; otherwise start
a new instance
On Error Resume Next
Set oPPT = GetObject(, "PowerPoint.Application")
If Err Then
PPTWasNotRunning = True
Set oPPT =
New
PowerPoint.Application
End If
On Error GoTo Err_Handler
With oPPT
'Create a new presentation and a new
slide
Set oPres = .Presentations.Add(WithWindow:=msoTrue)
Set oSlide =
oPres.Slides.Add(1, ppLayoutBlank)
'Create a text box and set its
poperties
Set oShape =
oSlide.Shapes.AddTextbox _
(msoTextOrientationHorizontal,
100, 100, 10, 10)
With oShape.TextFrame
.WordWrap = msoFalse
.MarginLeft = 0#
.MarginRight = 0#
.MarginTop = 0#
.MarginBottom = 0#
'Put some
text in it
With
.TextRange
.Text = Chr$(147)
& "Nobody," & Chr$(148) & _
" he said, as he slid down the banister, " _
& vbCr & Chr$(147) & "Nobody," & Chr$(148) & _
" he said, as he landed on his head, " _
& vbCr & Chr$(147) & "Nobody, but nobody could call me a fussy man " _
& Chr$(150) & vbCr & Chr$(147) & "But I do like a little bit of butter on my
bread!" _
& Chr$(148)
'Format the texr
.Font.Name
= "Times New Roman"
.Font.Size
= 12
.Paragraphs(3).Words(5).Font.Italic = msoTrue
.Paragraphs(4).Words(4).Font.Italic = msoTrue
End With
'For upside
down text, flip it vertically
oShape.Flip msoFlipVertical
'Or if you
want to rotate the text, use something like the following instead
'oShape.Rotation = -45
End With
'Copy the text box
oShape.Copy
End With
'Back to Word
Set MyRange =
oDoc.Windows(1).Selection.Range
MyRange.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile
oDoc.Shapes(oDoc.Shapes.Count).ConvertToInlineShape
'Close the presentation
oPres.Close
If PPTWasNotRunning Then
oPPT.Quit
End If
'Make sure you release object references.
Set oPPT = Nothing
Set oDoc = Nothing
System.Cursor = wdCursorNormal
StatusBar = ""
'Quit if no errors
Exit Sub
Err_Handler:
MsgBox Err.Description, vbCritical, "Error: " & Err.Number
If PPTWasNotRunning
Then
oPPT.Quit
End If
System.Cursor = wdCursorNormal
StatusBar = ""
End Sub