DetailPage-MSS-KB

Microsoft small business knowledge base

Article ID: 215484 - Last Review: October 24, 2013 - Revision: 2.0

This article was previously published under Q215484
This article has been archived. It is offered "as is" and will no longer be updated.

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.
  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 (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.
  4. 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().
    						
  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);
             // 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;
    						
  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:
169173  (http://support.microsoft.com/kb/169173/EN-US/ ) INFO: Frequently Ask Questions for Visual J++
Note This is a "FAST PUBLISH" article created directly from within the Microsoft support organization. The information contained herein is provided as-is in response to emerging issues. As a result of the speed in making it available, the materials may include typographical errors and may be revised at any time without notice. See Terms of Use (http://go.microsoft.com/fwlink/?LinkId=151500) for other considerations.

Applies to
  • Microsoft PowerPoint 2002 Standard Edition
  • Microsoft PowerPoint 2000 Standard Edition
  • Microsoft PowerPoint 97 Standard Edition
  • Microsoft Visual J++ 6.0 Standard Edition
Keywords: 
kbnosurvey kbarchive kbautomation kbhowto KB215484
Share
Additional support options
Ask The Microsoft Small Business Support Community
Contact Microsoft Small Business Support
Find Microsoft Small Business Support Certified Partner
Find a Microsoft Store For In-Person Small Business Support