This Question is Answered

1 "correct" answer available (4 pts) 2 "helpful" answers available (2 pts)
6 Replies Last post: Dec 12, 2007 2:18 AM by Peter Whittaker  
Peter Whittaker   16 posts since
Oct 4, 2007
Currently Being Moderated

Nov 8, 2007 11:13 AM

Remove my Native Shapes and VB macros

First of all can I say how pleased I have been with all the support from Team Dev. This support really is what makes the difference in de-risking projects for us developers. Thank you.

 

Having managed now to create native Dropdown lists with associated VBA macros acting as listeners, I need to be able to search for all Dropdowns on a worksheet and remove them in order to prevent an accumulation of such objects over multiple saves of the Excel workbook.

 

Ive looked at uiWorksheet.getPeer().getShapes() but the return class is T which I presume is an obsfucated class.

 

Alternaitvely if I could search for a named dropdown list returning a Shape, I presume I could then delete it. However Im unsure how to find a named Shape.

 

 

Many thanks in advance for your help

 

 

 

Pete

Igor Novikov TeamDev Ltd. 225 posts since
Apr 24, 2006
Currently Being Moderated
1. Nov 8, 2007 7:44 PM in response to: Peter Whittaker
Re: Remove my Native Shapes and VB macros

Hi Peter,

 

To avoid obfuscation issue you can try using jexcel library with complete MS Excel stubs:

 

ftp://ftp.teamdev.com/updates/jexcel-1.1-full.jar

 

To remove all shapes on worksheet try using following code:

 


Shapes shapes = uiWorksheet.getPeer().getShapes()
int num = (int) shapes.getCount().getValue();
for (int i = 0; i < num; i++) 
{
     shapes.item(new Variant(i + 1)).delete();
}

 

To check shape name, getName() method can be used:

 


shapes.item(new Variant(i + 1)).getName();

 

I hope this helps.

 

Sincerely,

 

Igor Novikov

Serge Piletsky TeamDev Ltd. 670 posts since
Apr 24, 2006
Currently Being Moderated
3. Nov 30, 2007 5:48 PM in response to: Peter Whittaker
Re: Remove my Native Shapes and VB macros

Hi Peter,

 

You just need to invoke this code in OleMessageLoop of the JExcel Application, Worksheet or Workbook object, for example:

 


   object.getOleMessageLoop().doInvokeAndWait(new Runnable() {
      public void run() {
         // your code
      }
   }

 

-Serge

Igor Novikov TeamDev Ltd. 225 posts since
Apr 24, 2006
Currently Being Moderated
5. Dec 6, 2007 6:13 PM in response to: Peter Whittaker
Re: Remove my Native Shapes and VB macros

Hi Peter,

 

We have slightly modified your code sample. It works like a sharm :

 


import com.jniwrapper.win32.jexcel.Worksheet;
import com.jniwrapper.win32.jexcel.ui.JWorkbook;
import com.jniwrapper.win32.excel.Shape;
import com.jniwrapper.win32.excel.Shapes;
import com.jniwrapper.win32.automation.types.Variant;
import javax.swing.*;
import java.io.File;

public class ShapesNameSample {
       public static void main(String[] args) throws Exception
       {
        final JWorkbook _workbook = new JWorkbook(new File("c://combo.xls"));
        final Worksheet sheet = _workbook.getWorksheet("test");

        final JFrame frame = new JFrame();
        frame.setContentPane(_workbook);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800,800);
        frame.setVisible(true);


        sheet.getApplication().getOleMessageLoop().doInvokeLater(new Runnable()
        {
            public void run()
            {
                Shapes shapes = sheet.getPeer().getShapes();
                Shape shape = null;
                String name = null;
                int num = (int) shapes.getCount().getValue();
                for(int i = 0; i < num; i++)
                {
                     shape = shapes.item(new Variant(i+1));
                     if(shape!=null)
                         try
                         {
                             name = shape.getName().toString();
                             System.out.println("CtXL.findListControl() shape" + i + ": " + name);

                         }catch(Exception e){}
                }
            }
        });
       }
}

 

combo.xls file you can find in attachment. Console output of this sample will be following:

 


CtXL.findListControl() shape0: MyCombo
CtXL.findListControl() shape1: AnotherMyComboBox
CtXL.findListControl() shape2: ComboBox1
CtXL.findListControl() shape3: ComboBox2
CtXL.findListControl() shape4: ComboBox3
CtXL.findListControl() shape5: LastComboBox4

 

I hope this helps.

 

Sincerely,

 

Igor Novikov

Attachments:

More Like This

  • Retrieving data ...