Microsoft KB Archive/215484

= How to automate PowerPoint by using Visual J++ 6.0 =

Article ID: 215484

Article Last Modified on 1/24/2007

-

APPLIES TO


 * Microsoft PowerPoint 2002 Standard Edition
 * Microsoft PowerPoint 2000 Standard Edition
 * Microsoft PowerPoint 97 Standard Edition
 * Microsoft Visual J++ 6.0 Standard Edition

-



This article was previously published under Q215484



SUMMARY
This article describes how to automate Microsoft PowerPoint by using Microsoft Visual J++ 6.0. You can also apply similar code and similar procedures to automate other Microsoft Office applications.



MORE INFORMATION
Follow these steps to build and run the Visual J++ example.  Start Visual J++ 6.0. Create a new Console Application project and name it JavaPpt. In the Project-Explorer window, open your Project tree and double-click the Class1.java file created for you. From the Project menu, click Add COM Wrapper, click Microsoft PowerPoint 8.0 Object Library (or 9.0 for PowerPoint 2000, or 10.0 for PowerPoint 2002.), and then click OK. This adds Java COM Wrappers that are derived from the PowerPoint type library msppt8.olb to your project. The type library for PowerPoint 2000 is named msppt9.0lb. The type library for PowerPoint 2002 is msppt.olb.  At the top of your Class1.Java file, add the following import statements:

import msppt8.*; // PowerPoint support. Use msppt9.* for 2000, and msppt.* for 2002 import com.ms.com.*; // Variant & exception support. import java.lang.InterruptedException; // Needed for Thread.sleep.   In your main function, add the following code:

// Force COM objects to be created on the current thread. // Otherwise, older VMs might not release all references // and PowerPoint might continue to run after you shutdown. ComLib.declareMessagePumpThread; // Launch PowerPoint. Application app = new Application; // Add a presentation. Presentation pres = app.getPresentations.Add(1);

// Add a slide with text layout. Slide slide1 = pres.getSlides.Add(1, PpSlideLayout.ppLayoutText); // Add text to slide. slide1.getShapes.Item(new Variant(1)).getTextFrame.getTextRange.setText("My first slide"); slide1.getShapes.Item(new Variant(2)).getTextFrame.getTextRange.setText("Automating PowerPoint is easy!\r\nUsing VJ++ is fun.");

// Add another slide, but with text and chart. Slide slide2 = pres.getSlides.Add(2, PpSlideLayout.ppLayoutTextAndChart); // Add text to slide. slide2.getShapes.Item(new Variant(1)).getTextFrame.getTextRange.setText("Slide 2's topic"); slide2.getShapes.Item(new Variant(2)).getTextFrame.getTextRange.setText("You can create and use charts in your PowerPoint slides!");

// Add chart where default chart is. {   Variant index = new Variant(3); float top = slide2.getShapes.Item(index).getTop; float width = slide2.getShapes.Item(index).getWidth; float height = slide2.getShapes.Item(index).getHeight; float left = slide2.getShapes.Item(index).getLeft; slide2.getShapes.AddOLEObject(left, top, width, height, "MSGraph.Chart", "", 0, "", 0, "", 0); // Remove old/default chart. slide2.getShapes.Item(index).Delete; }

// Add another slide, but with text and org-chart. Slide slide3 = pres.getSlides.Add(3, PpSlideLayout.ppLayoutOrgchart); // Add title for this slide. slide3.getShapes.Item(new Variant(1)).getTextFrame.getTextRange.setText("The rest is only limited by your Imagination"); // Add a new org chart where existing one is. {   Variant index = new Variant(2); float top = slide3.getShapes.Item(index).getTop; float width = slide3.getShapes.Item(index).getWidth; float height = slide3.getShapes.Item(index).getHeight; float left = slide3.getShapes.Item(index).getLeft; slide3.getShapes.AddOLEObject(left, top, width, height, "OrgPlusWOPX.4", "", 0, "", 0, "", 0); // OrgPlusWOPX.4 is provided by the Microsoft Organization Chart application. // Remove old/default org-chart. slide3.getShapes.Item(index).Delete; } // Setup slide-show properties. {   Variant varOpt = new Variant; varOpt.noParam; SlideShowTransition sst; sst = pres.getSlides.Range(varOpt).getSlideShowTransition; sst.setEntryEffect(PpEntryEffect.ppEffectRandom); sst.setAdvanceOnTime(1); sst.setAdvanceTime(5); // 5 seconds per slide sst = null; } {   SlideShowSettings sss; sss = pres.getSlideShowSettings; sss.setShowType(PpSlideShowType.ppShowTypeKiosk); sss.setLoopUntilStopped(1); sss.setRangeType(PpSlideShowRangeType.ppShowAll); sss.setAdvanceMode(PpSlideShowAdvanceMode.ppSlideShowUseSlideTimings); // Run slide show... sss.Run; sss = null; }

// Sleep for a while so user can watch slide show. try { Thread.sleep(15000); } catch(InterruptedException e) {}

// Stop slide show. try { pres.getSlideShowWindow.getView.Exit; } catch(ComFailException e) {} // Clean up. pres.Close; app.Quit; slide3 = null; slide2 = null; slide1 = null; pres = null; app = null;  From the Debug menu, click Start (or, press F5) to build and run the example.</ol>

<div class="references_section">