Hello,
I modified the sample file, PowerPointAutomationSample.java, which included in the comfyj-2.9-windows-32_64-bit.zip for adding a picture to a powerpoint slide.
But I don't understand why I always got this error "Exception in thread "AWT-EventQueue-0" com.jniwrapper.win32.com.ComException: COM object method returns error code: 0x80020005; DISP_E_TYPEMISMATCH".
Please refer to the following source code. There are 3 different functions for adding a picture to a powerpoint slide. But I got errors no matter which one I use.
Please kindly tell me if there's anything wrong or something I missed.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pptautomation;
import com.jniwrapper.Parameter;
import com.jniwrapper.SingleFloat;
import com.jniwrapper.win32.automation.Automation;
import com.jniwrapper.win32.automation.OleContainer;
import com.jniwrapper.win32.automation.OleMessageLoop;
import com.jniwrapper.win32.automation.impl.IDispatchImpl;
import com.jniwrapper.win32.automation.types.BStr;
import com.jniwrapper.win32.automation.types.Variant;
import com.jniwrapper.win32.ole.OleFunctions;
import com.jniwrapper.win32.ole.types.OleVerbs;
import operations.OfficeFileOperationsHandler;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import powerpoint.office.MsoTriState;
/**
*
* @author Jeff
*/
public class PPTAutomation extends JFrame{
private static final Dimension WINDOW_SIZE = new Dimension(820, 480);
/**
* progid of presentation
*/
private static final String PRESENTATION_PROGID = "Powerpoint.Show";
private OleContainer _container;
JPanel jPanel1;
JButton jBtn1;
//===========================
IDispatchImpl presentation;
Automation presentationAutomation;
//===========================
public PPTAutomation()
{
super("JNIWrapper - Power Point Automation");
_container = new OleContainer();
_container.createObject(PRESENTATION_PROGID);
initComponent();
// Enable open / save operations
_container.setFileOperationsHandler(new OfficeFileOperationsHandler(OfficeFileOperationsHandler.TYPE_POWERPOINT));
}
private void initComponent()
{
jPanel1 = new JPanel();
jBtn1 = new JButton();
//setLayout(new BorderLayout());
jBtn1.setPreferredSize(new Dimension(80,30));
jBtn1.setText("AddPic");
jPanel1.setPreferredSize(new Dimension(100,480));
jPanel1.add(jBtn1);
getContentPane().add(jPanel1,BorderLayout.WEST);
getContentPane().add(_container, BorderLayout.CENTER);
jBtn1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBtnActionPerformed(evt);
}
});
pack();
}
private void jBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
IDispatchImpl slides = (IDispatchImpl) presentationAutomation.getProperty("Slides").getPdispVal();
Automation slidesAutomation = new Automation(slides);
IDispatchImpl slide = (IDispatchImpl)slidesAutomation.invoke("item",new Integer(1)).getPdispVal();
Automation slideAutomation = new Automation(slide);
IDispatchImpl _shapes = (IDispatchImpl)slideAutomation.getProperty("Shapes").getPdispVal();
Automation shapesAutomation = new Automation(_shapes);
shapesAutomation.invoke("addPicture", new Object[] {new BStr("D:\\web-cam_sticker.png"), new MsoTriState(0), new MsoTriState(0),new SingleFloat(100), new SingleFloat(100), new SingleFloat(30), new SingleFloat(30)}); //Error occurs here
//shapesAutomation.invoke("addPicture", new Object[] {new BStr("D:\\web-cam_sticker.png"), new MsoTriState(0), new MsoTriState(0),new SingleFloat(100), new SingleFloat(100), new Parameter[] {new SingleFloat(30),new SingleFloat(30)}});
//shapesAutomation.invoke("addPicture", new Object[] {new BStr("D:\\web-cam_sticker.png"), new MsoTriState(0), new MsoTriState(0),new SingleFloat(100), new SingleFloat(100)});
System.out.println("jBtnActionPerformed...");
}
public void modifyDocument()
{
// get current presentation from ole object
//IDispatchImpl presentation = new IDispatchImpl(_container.getOleObject());
presentation = new IDispatchImpl(_container.getOleObject());
presentation.setAutoDelete(false);
try
{
//Automation presentationAutomation = new Automation((IDispatchImpl) presentation);
presentationAutomation = new Automation((IDispatchImpl) presentation);
IDispatchImpl slides = (IDispatchImpl) presentationAutomation.getProperty("Slides").getPdispVal();
slides.setAutoDelete(false);
try
{
addSlide(slides);
}
finally
{
slides.release();
}
}
finally
{
presentation.release();
}
}
private void addSlide(IDispatchImpl slides)
{
Automation slidesAutomation = new Automation(slides);
try
{
Variant result = (Variant)slidesAutomation.invoke("Add", new Object[] { new Integer(2), new Integer(2) } );
IDispatchImpl pSlide = (IDispatchImpl)result.getPdispVal();
pSlide.setAutoDelete(false);
pSlide.release();
}
finally
{
slidesAutomation.release();
}
}
private static void createGUI()
{
final PPTAutomation app = new PPTAutomation();
app.setSize(WINDOW_SIZE);
app.setLocationRelativeTo(null);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.addWindowListener(new WindowAdapter()
{
public void windowOpened(WindowEvent e)
{
// show power point
app._container.doVerb(OleVerbs.SHOW);
// work with power point through automation
try
{
OleMessageLoop.invokeMethod(app, "modifyDocument", new Object[] {});
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public void windowClosing(WindowEvent e)
{
// close presentation on exit
app._container.destroyObject();
}
});
app.setVisible(true);
}
public static void main(String[] args)
{
// initialize OLE
OleFunctions.oleInitialize();
createGUI();
}
}