Can't add a picture to a powerpoint slide. COM object method returns error code: 0x80020005;

66 views
Skip to first unread message

jeffe...@gmail.com

unread,
Nov 13, 2012, 2:30:08 AM11/13/12
to comfyj...@teamdev.com
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.
 
 
Thank you.
Jeff
 
/*
 * 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();
    }
}
PPTAutomation.java

jeffe...@gmail.com

unread,
Nov 13, 2012, 2:53:41 AM11/13/12
to comfyj...@teamdev.com, jeffe...@gmail.com
Hello,
I forgot to say...that I tried adding a picture to a powerpoint 2003 slide and a powerpoint 2007 slide. And I got the same error.
 
Your earlier reply will be very appriciated.
Thank you.
Jeff

jeffe...@gmail.com於 2012年11月13日星期二UTC+8下午3時30分08秒寫道:

Sergei Piletsky

unread,
Nov 13, 2012, 5:19:55 AM11/13/12
to jeffe...@gmail.com, comfyj...@teamdev.com
Hi Jeff,

In order to fix this issue you just need to inform 'shapesAutomation' object that it has to work with a DispInterface object (_shapes in your case), so it should use an appropriate order of function call parameters.

The code snippet below demonstrates the solution:

        Automation shapesAutomation = new Automation(_shapes);
        shapesAutomation.setDispInterface(true);
        try {

            shapesAutomation.invoke("addPicture", new Object[]{
                    new BStr("d:\\screenshot1.png"),
                    new MsoTriState(MsoTriState.msoFalse),
                    new MsoTriState(MsoTriState.msoTrue),
                    new SingleFloat(0),
                    new SingleFloat(0),
                    new SingleFloat(300),
                    new SingleFloat(500)});
        } catch (AutomationException e) {
            e.printStackTrace();
            ExcepInfo exceptionInformation = e.getExceptionInformation();
            String bstrDescription = exceptionInformation.getBstrDescription();
            System.err.println("bstrDescription = " + bstrDescription);
        }
        shapesAutomation.release();

You may also notice that I also added handling of AutomationException which should intercept such DISP_E_TYPEMISMATCH COM exceptions and provide the ability to get the extended exception information (ExcepInfo) when such error occurs.

All automation objects (shapesAutomation) should also be released using the .release() method to handle the references to COM object properly.

Please let me know if you have any further questions.

Sincerely,
Serge


--
You are receiving this email because you have joined the "ComfyJ Forum" group or have been added by request. You may choose not to receive emails from this group or unsubscribe completely on "Edit my membership" page.
Go to http://links.teamdev.com/comfyj-forum, choose "Edit my membership" link on the right. Specify your email preferences in suggested options, or click "Unsubscribe" button.

jeffe...@gmail.com

unread,
Nov 13, 2012, 8:57:52 PM11/13/12
to comfyj...@teamdev.com, jeffe...@gmail.com
Hi Sergei,
I can successfully add a picture to a powerpoint slide now. Thank you very much for your help.
 
Thank you,
Jeff
 

Sergei Piletsky於 2012年11月13日星期二UTC+8下午6時19分56秒寫道:

jeffe...@gmail.com

unread,
Nov 14, 2012, 3:14:47 AM11/14/12
to comfyj...@teamdev.com, jeffe...@gmail.com
 
Hi Sergei,
Here I have two more questions
1) Is it possible to monitor what users did in the PowerPoint such as selet a shape, change a shape's size, change a shape's
position,......etc. via comfyj ?
2) I tried the PowerPointEventsHandling sample also, it works. But I don't know why there are error messages also. Those error
messages are listed below:
[Main message loop] ERROR com.jniwrapper.win32.e -
java.lang.NoSuchFieldException: INTERFACE_IDENTIFIER
at java.lang.Class.getDeclaredField(Class.java:1882)
at com.jniwrapper.win32.e.b(SourceFile:229)
at com.jniwrapper.win32.e.registerInterface(SourceFile:145)
at com.jniwrapper.win32.e.a(SourceFile:96)
at com.jniwrapper.win32.e.<init>(SourceFile:45)
at com.jniwrapper.win32.com.server.IClassFactoryServer.<init>(SourceFile:46)
at eventhandling.EventHandling$3.run(EventHandling.java:99)
at com.jniwrapper.win32.MessageLoopThread$ThreadSynchronizedAction.run(MessageLoopThread.java:584)
at com.jniwrapper.win32.MessageLoopThread$LoopThread.run(MessageLoopThread.java:526)
890 [Main message loop] ERROR com.jniwrapper.win32.e - The eventhandling.EventHandling$PowerPointMappedEvents interface was not registered for eventhandling.EventHandling$PowerPointEventHandler, because eventhandling.server.EventHandling$PowerPointMappedEventsVTBL cannot be instantiated.
java.lang.RuntimeException: INTERFACE_IDENTIFIER
at com.jniwrapper.win32.e.registerInterface(SourceFile:153)
at com.jniwrapper.win32.e.a(SourceFile:96)
at com.jniwrapper.win32.e.<init>(SourceFile:45)
at com.jniwrapper.win32.com.server.IClassFactoryServer.<init>(SourceFile:46)
at eventhandling.EventHandling$3.run(EventHandling.java:99)
at com.jniwrapper.win32.MessageLoopThread$ThreadSynchronizedAction.run(MessageLoopThread.java:584)
at com.jniwrapper.win32.MessageLoopThread$LoopThread.run(MessageLoopThread.java:526)
Looking forward to have your reply soon.
Thank you,
Jeff
 
 

jeffe...@gmail.com於 2012年11月14日星期三UTC+8上午9時57分52秒寫道:
EventHandling.java

Sergei Piletsky

unread,
Nov 14, 2012, 5:10:27 AM11/14/12
to Jeff yang, ComfyJ Forum
Hi

1) ComfyJ library is COM bridge. So, it can be used for monitoring those actions only if a COM library itself provides the corresponding API for handling those actions.

2) Those messages are actually debug output messages, and we already changed the logging level for them in new version of ComfyJ. So, taking into account that PowerPointEventsHandling example works for you even if it throws those errors, you can omit those messages.

Sincerely,
Serge



Reply all
Reply to author
Forward
0 new messages