A list of products available on this site
Documentation for XtraMania's xtras
Prices and links to the online store
Xtras, PDFs, samples
Have a question? Ask us!
Contacts
Logo. www.xtramaina.com  
Home Search E-mail
VbScriptXtra Samples/PowerPoint

ActiveCompanionSet xtras for Macromedia Director

Automating Microsoft PowerPoint

This sample makes a slide in a new PowerPoint presentation from the current Director frame.

vb = xtra("VbScriptXtra")
-- Create a new instance of PowerPoint
ppt = vb.CreateObject("PowerPoint.Application")

-- Create new presentation
p = ppt.Presentations.Add()

-- Add new slide to the newly created presentation
s = p.Slides.Add( 1, #ppLayoutBlank )

-- Scanning Director's current frame for texts and linked pictures
repeat with i = 1 to the lastChannel
  if sprite( i ).type <> 0 then
    mem = sprite(i).member
    r = sprite(i).rect

    case mem.type of
      #text:
        -- Add text box for a text member
        sh = s.shapes.AddTextbox( 1, r[1], r[2], r.width, r.height )
        sh.RTF = mem.rtf

      #bitmap:
        -- if bitmap member is linked
        if mem.fileName <> "" then
          -- Add picture shape for a bitmap member
          sh = s.shapes.AddPicture( mem.fileName, #true, #false, \
                      r[1], r[2], r.width, r.height )
        end if
    end case
  end if
end repeat

-- Now make PowerPoint visible
ppt.visible = #true

To find out how to save resulting presentation use ObjectBrowser xtra. Right after you get the instance of the Presentation object, call Interface() method.

-- Create new presentation
p = ppt.Presentations.Add()
p.Inteface()

If ObjectBrowser xtra is placed in Director's xtras folder, it will show up with the description of methods and properties available for Presentation object.
"Method: SaveAs
Arguments:
  IN      String FileName
  IN      PpSaveAsFileType FileFormat, Optional
  IN      MsoTriState EmbedTrueTypeFonts, Optional
Returns: VOID
Call syntax:
SaveAs(FileName, FileFormat, EmbedTrueTypeFonts)"

So, since FileFormat and EmbedTrueTypeFonts are optional you may simply specify the new file name to save the presentation with default format.
p.SaveAs( the moviePath & "p.ppt" )
-- or
p.SaveAs( the moviePath & "p.ppt", #ppSaveAsPresentation, #false )

To quit PowerPoint set to VOID all references to PowerPoint objects. Call Quit() method of PowerPoint application object and set it to VOID too.
s = VOID
p = VOID
ppt.Quit()
ppt = VOID

Note: In Macromedia Director (version 7, 8, 8.5) 'quit' was reserved and did not passed to the xtra at all. To workaround this issue VbScriptXtra uses 'underscore handling'. Add one underscore '_' at the beginning of the Quit. VbScriptXtra will automatically remove it and will call real Quit method of the wrapped object.
-- Underscore will be removed by VbScriptXtra internally
ppt._Quit()

Using OLE Embedded PowerPoint Presentation

This sample uses both OLExtra and VbScriptXtra from ActiveCompanionSet.

OLE xtra allows tight integration of Microsoft Office applications and Macromedia Director. For example, it is possible to place Microsoft PowerPoint movie right on stage as Director sprite, make it running and fully control it with scripting support provided by VbScriptXtra.

Simply let OLE xtra handle PowerPoint presentation OLE object. Presentation should be configured to allow playing in Window without displaying scroll bars (otherwise they will appear to the right of the sprite). Presentation may be adjusted for auto advancing by time, effects, transitions etc. If presentation is marked as read only, PowerPoint won't let user changing it, since within Director sprite is actually running the PowerPoint with all its capabilities.

Below is the simple behavior attached to the OLE xtra's actor sprite, that activates the embedded OLE object on begin sprite:

property spriteNum
property pView

on beginSprite me
  sprite( spriteNum ).Activate()

  mem = sprite( spriteNum ).member
  if mem.Failed then
    alert mem.LastError
    exit
  end if

  -- Get the scripting interface to the Presentation
  p = mem.GetObject()
  if mem.Failed then
    put mem.LastError
    exit
  end if

  -- Getting the running slide show view
  pView = p.SlideShowWindow.View
end

The method below shows how to browse presentation slides by Lingo:
on GoToSlide me, index
  -- Moving to the requested slide
  pView.GoToSlide( index )
end

on GoNext me -- Moving to the requested slide
  pView.Next()
end

In this sample pView is usual VBScriptXtra wrapper. Refer to the VbScriptXtra's documentation for more details. More sophisticated applications can use event handling for controlling operations that user performs with the activated OLE document.

Sample Director movie is available to download. It can load and play PowerPoint presentations.
Note: To play presentation on stage you should adjust its play settings to allow playing in window without showing scrollbars. This is a presentation setting.

Automating Microsoft PowerPoint Viewer

This sample shows how to play PowerPoint presentation with PowerPoint Viewer. it uses
the smart technique (Thanks to Steven Hite) of binding the viewer window to the Stage by making it a child of the Stage with BuddyApi xtra. It solves the problem of the player window hiding behind the Projector's stage window.

global oPPt, oShow, hwndPPtWindow, miawWindow

on OpenPresentation me, theFile, nSlide, theRect
  
  vb = xtra("VbScriptXtra")

  -- Create a new instance of PowerPoint Viewer application
  oPPt = vb.CreateObject("PowerPointViewer.Application")
  -- Use put oPPt.Interface() to see what else you can do with PowerPoint Viewer

  -- Load the specified file
  oShow = oPPt.NewShow( theFile, 1, 1 )

  -- Find the presentation window handle for windows operations
  me.GetPPtWindow()
  me.HidePPt()
  me.SetRect(theRect)

  -- Bind the presentation window to the stage to prevent it from going behind the stage
  me.AttachToStage()
  
  nTotalSlides = me.GetTotalSlides()
  
  if voidp(nSlide) then nSlide = 1
  me.GotoSlide(nSlide)
  
  me.ShowPPT()
  return me.GetCurrentSlide()
End

on GotoSlide me, nSlide
  if not objectp(oShow) then return 0
  res = oShow.GotoSlide(nSlide,0)
  return oShow.SlideNumber
End

on GetCurrentSlide me
  if not objectp(oShow) then return 0
  return oShow.SlideNumber
end

on GetTotalSlides me
  if not objectp(oShow) then return 0
  return oShow.SlidesCount()
end

on ClipWindow me
  baClipWindow( hwndPPtWindow , 10 , 8 , 10 , 8 , true, true )
end

on hidePPT me
  baSetWindowState(hwndPPtWindow, "Hidden" )
end

on attachtostage me
  baSetParent(hwndPPtWindow, baStageHandle() )
end

on showPPT me
  baSetWindowState(hwndPPtWindow, "Restored" )
end

on SetRect me, theRect
  theRect = value(theRect)
  if objectp(miawWindow) then
    stageWidth = the stage.rect.width*1.0
    width = miawWindow.rect.width*1.0
    if stageWidth = 0 then
      return
    end if
    ratio = width/stageWidth
    theRect = theRect * ratio
  end if

  l = integer(theRect.left)
  t = integer(theRect.top)
  w = integer(theRect.width)
  h = integer(theRect.height)
  baMoveWindow(hwndPPtWindow, l , t , w , h , true )
  baSetWindowState(hwndPPtWindow, "StayOnTop" )
end d

on GetPPtWindow me
  WndList = baWindowList( "" , "PowerPoint Viewer -" , false )
  if WndList <> [] then
    hwndPPtWindow = WndList[1]
    return hwndPPtWindow
    --
  else
    return []
  end if
End

on ClosePresentation me
  if objectp(oShow) then
    oShow.exit()
    oShow = 0
  end if
  if objectp(oPPt) then
    oPPt.quit()
    oPPt = 0
  end if
  put baWindowList( "" , "Microsoft PowerPoint Viewer" , false ) into thePPViewerWin
  if thePPViewerWin = [] then return
  put thePPViewerWin[1] into thisWin
  if thisWin.ilk = #integer and not voidp(thisWin) and thisWin > 0 and thisWin <> [] then
    res = baCloseWindow(thisWin)
    res = baCloseApp(thisWin)
  else
    nothing
  end if

end

Export PowerPoint Slides

This sample shows how to export PowerPoint slides to Jpeg images on disk. Steven Hite's sample also uses BuddyApi xtra for user interaction.

--Handler to export a specified slide from a PowerPoint Presentation
on exportCurrentSlide me, PptFile, theSlide

  vb=xtra"VbScriptXtra"

  currentFolder = baSysFolder("current")
  exportFolder = baGetFolder(currentFolder, "Please select a folder to save to",3, "Select a folder", -1, 0 )
  if exportFolder = "" then return

  --set the current folder for successive saves to make it easier for the user.
  Result = baSetCurrentDir(exportFolder)

  --create the PowerPoint object
  ppt = vb.CreateObject("PowerPoint.Application")

  open presentation
  doc = ppt.Presentations.Open( PptFile, false, false, false)

  if theSlide < 1 then theSlide = 1
  outputpath = exportFolder & "Slide_" & theSlide &".jpg"

  _player.cursor(4)
  doc.slides[theSlide].Export(outputpath,"JPG",1024,768)
  doc.Close()
  doc = VOID
  ppt.Quit()
  ppt = VOID
  _player.cursor(-1)

  Answer = baMsgBoxEx( "Choose an Option", "Export Option" , "Open File", "Open Folder", "Cancel", "Question" , 1 , "center", "Arial", 12, 4, -1, -1 )

  if answer = "Cancel" then return

  if answer = "Open File" then
    baOpenFile(outputpath,"Normal")
  end if

  if answer = "open folder" then
    baShell("open", "explorer.exe", exportFolder, "", "normal")
  end if

end

--Handler to export ALL slides from a PowerPoint Presentation
on exportAllSlides me, pptFile, theSlide

  currentFolder = baSysFolder("current")
  exportFolder = baGetFolder(currentFolder, "Please select a folder to save to:",3, "Select a folder", -1, 0 )
  if exportFolder = "" then return

  --set the current folder for successive saves to make it easier for the user.
  Result = baSetCurrentDir(exportFolder)

  --create the PowerPoint object
  ppt = vb.CreateObject("PowerPoint.Application")

  open presentation
  doc = ppt.Presentations.Open( PptFile, false, false, false)

  _player.cursor(4)
  doc.Export(exportFolder,"JPG",1024,768)
  doc.Close()
  doc = VOID
  ppt.Quit()
  ppt = VOID
  _player.cursor(-1)

  Answer = baMsgBox( "View folder contents in explorer?", "View Folder Contents?" , "YesNo", "Question" , 1 )--it exists so we need to overwrite it
  if answer = "No" then
    return 0
  else
    baShell("open", "explorer.exe",exportFolder, "", "normal")
  end if

end

Where to find more info about PPt?

Microsoft PowerPoint contains rather large help system, which contains just everything about PowerPoint. Look at Visual Basic for PowerPoint topics, which describes PowerPoint data model, objects, methods and properties you can use.
Make sure you have installed this part of the help system, since it may not be included in typical installation.

VbScriptXtra provides autodocumentation feature allowing you to see what you can do with particular object instance. Just create a PowerPoint object and type in Messages window:
put interface(ppt)

ObjectBrowser xtra - autodocumentation companion for VbScriptXtra - will show all available PPt interfaces, methods, properties and enumerations.

ObjectBrowser xtra is a part of ActiveCompanionSet. It is available to Download.

Site homeSearchContact author © Eugene Shoustrov. www.xtramania.com