Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Automation Powerpoint example
Message
 
 
To
All
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Title:
Automation Powerpoint example
Miscellaneous
Thread ID:
00398557
Message ID:
00398557
Views:
38
This snippet of code might be helpful to some UT members. It's for automatically building a slide presentation from a database of images. It's called once for each slide to be added. The procedure is fully-functional, and is passed two parameters:
1. A caption
2. A fully-qualified name to an image file (e.g., jpeg or gif)

The procedure will invoke powerpoint if not already running, put it into the correct state for adding slides, then add a new slide with the picture optimally sized & centered (regardless of H:W proportions)with the caption above the image to serve as the slide title.

All automation is done within VFP (no macro/vba snippets necessary on the PPT side). When you read through the code, you'll see the spots to tweak things like background color, font color of the title, etc.
Param cTitleText,cPictureFile
#DEFINE ppLayoutTitleOnly 11
#DEFINE msoFalse 0
#DEFINE msoTrue -1
#DEFINE ppForeground 2
Local oPicture, oPres, oView,nSlides,oCurSLide,nTitleTextLen,nFontSize
Local SlideWidth,SlideHeight,nImgMaxHeight,nImgMaxWidth,nImgTop,nImgLeft,WtoH

if type("G_oppt")<>"O" or IsNull(G_oPPT)
  Public G_Oppt
  G_oppt = CreateObject("Powerpoint.Application")
  if type("G_oppt")<>"O" or IsNull(G_oPPT)
     Messagebox("Error connecting to PowerPoint!",48,"Powerpoint automator")
     return
  endif
endif
with G_oPPT
  if .Visible <> msoTrue
    .Visible=msoTrue
    .WindowState=1    && Normal window
  endif
  if .presentations.count = 0
    .presentations.add()
  endif
  if .activewindow.viewtype <> 1
    .activewindow.viewtype = 1
  endif
EndWith  

With G_oPPT
  oPres=G_oPPT.ActivePresentation
  oView=G_oPPT.ActiveWindow.View
Endwith
With oPres.PageSetup
  SlideWidth=.SlideWidth
  SlideHeight=.SlideHeight
endwith

With oPres
  nSlides=.slides.Count+1
  .slides.Add( nSlides, ppLayoutTitleOnly)
  oView.GotoSlide(nSlides)
  oCurSLide=oView.slide
  with oCurSLide.shapes
    .title.textframe.textrange.text=cTitleText
    nTitleTextLen=Len(cTitleText)
    nFontSize=18
    Do Case 
      Case Between(nTitleTextLen,40,60)
        nFontSize=24
      Case Between(nTitleTextLen,20,40)
        nFontSize=28
      Case nTitleTextLen < 20
        nFontSize=30
     EndCase
     with .title
      .textframe.textrange.font.size=nFontSize
      .textframe.textrange.font.Color.RGB = RGB(255, 255, 0)
      .top=-10
      .height=.height-15
      .left=5
      .width=SlideWidth-10
     endwith
  endwith
 With G_Oppt.ActiveWindow.Selection.SlideRange
    .FollowMasterBackground = msoFalse
    .DisplayMasterShapes = msoTrue
    With .Background
        .Fill.Visible = msoTrue
        .Fill.ForeColor.RGB = RGB(0, 0, 0)
        .Fill.Transparency = 0
        .Fill.Solid()
    Endwith
   Endwith
Endwith

oPicture = oCurSLide.Shapes.AddPicture(cPictureFile, ;
      msoFalse, msoTrue, 1, 1, 1, 1)
With oPicture      
  .Visible=msoFalse
  .ScaleHeight( 1, msoTrue)
  .ScaleWidth( 1, msoTrue)
EndWith  


* These can be tweaked to adjust real estate occupied by the image
nImgMaxHeight=SlideHeight-100
nImgMaxWidth=SlideWidth-100
nImgTop=50
nImgLeft=50

with oPicture
  WtoH = (1.0000 * .Width) / (1.0000 * .Height)
  nFullWidth=SlideWidth- (nImgLeft * 2)
  if .Width > .Height .and. nFullWidth / WtoH  < nImgMaxHeight
    .Left=nImgLeft
    .Width=nFullWidth
    .Top=(SlideHeight  - (nFullWidth / WtoH) ) /2  + nImgTop/2
  Else
    .Top=nImgTop + 10
    .Height=nImgMaxHeight
    .Left = (SlideWidth - (WtoH * nImgMaxHeight)) / 2.0
  endif
  .Visible=msoTrue
Endwith
Return
Reply
Map
View

Click here to load this message in the networking platform