HOWTO: Automate PowerPoint Using Visual J++

ID: Q215484


The information in this article applies to:


SUMMARY

This article demonstrates how to Automate Microsoft PowerPoint using Visual J++. You can also apply similar code and procedures to automate other Microsoft Office applications.


MORE INFORMATION

Follow these steps to build and run the Visual J++ example.

  1. Start Visual J++ 6.0. Create a new Console Application project and name it JavaPpt.


  2. In the Project-Explorer window, open your Project tree and double-click the Class1.java file created for you.


  3. From the Project menu, click Add COM Wrapper, click Microsoft PowerPoint 8.0 Object Library, and click OK. This adds Java COM Wrappers that are derived from the PowerPoint type library msppt8.olb to your project.


  4. At the top of your Class1.Java file, add the following import statements:
    
    import msppt8.*; // PowerPoint support.
    import com.ms.com.*; // Variant & exception support. 
    import java.lang.InterruptedException; // Needed for Thread.sleep(). 


  5. 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);
    	// 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; 


  6. From the Debug menu, click Start (or, press F5) to build and run the example.



REFERENCES

For additional information about Visual J++ and Automation, please see the following articles in the Microsoft Knowledge Base:

Q169173 INFO: Frequently Ask Questions for Visual J++
Q169796 HOWTO: Automate Excel from Java
Q169802 SAMPLE: Jword8.exe Demonstrates Automating Word 8.0 from Java

© Microsoft Corporation 1999, All Rights Reserved.
Contributions by Joe Crump, Microsoft Corporation

Additional query words: jactivex


Keywords          : kbAutomation kbVJ kbVJ600 kbPowerPt kbGrpDSO 
Version           : WINDOWS:6.0,97
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: January 30, 1999