wxAutomation and Excel application Topic is solved

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
pujari
Experienced Solver
Experienced Solver
Posts: 52
Joined: Fri Sep 07, 2007 11:58 am

wxAutomation and Excel application

Post by pujari »

Hi Everyone,

Can anyone please tell me how can i inseert some images in my excel application created using the wxautomation object.
I have gone through number of sites but i couldnt get correct information.

i have got a VBA code for this but i coudnt translte it to widgets.

Here is the code:

Sub TestInsertPictureInRange()
InsertPictureInRange "C:\FolderName\PictureFileName.gif", _
Range("B5:D10")
End Sub

Sub InsertPictureInRange(PictureFileName As String, TargetCells As Range)
' inserts a picture and resizes it to fit the TargetCells range
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
' determine positions
With TargetCells
t = .Top
l = .Left
w = .Offset(0, .Columns.Count).Left - .Left
h = .Offset(.Rows.Count, 0).Top - .Top
End With
' position picture
With p
.Top = t
.Left = l
.Width = w
.Height = h
End With
Set p = Nothing
End Sub



Can anyone pls help me out in porting it to wxwidgets.
Thank you.
timg
Earned some good credits
Earned some good credits
Posts: 148
Joined: Mon Jan 23, 2006 6:52 pm

Post by timg »

Code: Select all

	wxAutomationObject *objExcel=new wxAutomationObject; 
	wxAutomationObject xlbook;
	wxAutomationObject xltmp1;
	wxAutomationObject xltmp3;
	wxVariant appo, rng[4]; 
	bool	res;
	
	if ( objExcel->CreateInstance("Excel.Application") ) 
	{ 
		res = objExcel->GetObject(xlbook,"Workbooks.Add");
		res = xlbook.GetObject(xltmp3,"ActiveSheet.Pictures");

		rng[0] = wxVariant("c:\\picture.bmp");
		appo=xltmp3.GetObject(xltmp1, "Insert", 1, rng);
		appo=xltmp1.PutProperty("Top","10");
		appo=objExcel->PutProperty("visible", true); 
This should get you started.
pujari
Experienced Solver
Experienced Solver
Posts: 52
Joined: Fri Sep 07, 2007 11:58 am

Post by pujari »

Thank you very much for your help.i could get the images on the excel.

Still i have few questions.can you pls clarify.

How can i add a new worksheet to the excel??As soon as we open an excel we will be having 3sheets by default.How to add another sheet to my excel??

How can i go to other worksheets in excel?By default we will be in "Sheet1".Now how can v go to "Sheet2" and add some images in that sheet???

I will be very thankful if you could give me this information.

Thank You,
Waiting for ur reply.
timg
Earned some good credits
Earned some good credits
Posts: 148
Joined: Mon Jan 23, 2006 6:52 pm

Post by timg »

Code: Select all

                res = xlbook.GetObject(xltmp3,"WorkSheets.Add");

This would get you a new sheet. You could set the name like this:

Code: Select all

		appo=xltmp3.PutProperty("Name", "My Sheet"); 
Or add pictures like this:

Code: Select all

		res = xltmp3.GetObject(xltmp1,"Pictures");
		rng[0] = wxVariant("c:\\cutback.bmp");
		appo=xltmp1.GetObject(xltmp4, "Insert", 1, rng);
Good luck.
pujari
Experienced Solver
Experienced Solver
Posts: 52
Joined: Fri Sep 07, 2007 11:58 am

Post by pujari »

Thank You for the reply.

DO we need to pass some extra parameters to add a worksheet in our specified location??

res = xlbook.GetObject(xltmp3,"WorkSheets.Add");
appo=xltmp3.PutProperty("Name", "My Sheet");

With the above code,I could able to add a sheet with name "My Sheet" at the first position (Before the deafault Sheets "Sheet1" "Sheet2" and "Sheet3")
how can i add "My Sheet" in second position (In between Sheet1 and Sheet2)?
Can i rename the default Sheet Names?? (Can i change the name of "Sheet1" "Sheet2" and "Sheet3" to any other names of my choice) ???

Thank You
Thanks for ur time.
timg
Earned some good credits
Earned some good credits
Posts: 148
Joined: Mon Jan 23, 2006 6:52 pm

Post by timg »

This is the easiest way to add sheets. In VBA, you can specify some other parameters to tell it where to add the sheet, but I have not tried to do that with wxAutomation. (I'm not sure how to specify them from wxAutomation actually)

By default, the sheet will be added before the active sheet, so you could get some control over where they are added by activating the sheet after where you want the new sheet to go.

As for renaming the default sheets, yes you can do that. You just get the sheet object and change its name:

Code: Select all

		rng[0] = wxVariant("Sheet2");
		res = objExcel->GetObject(xltmp1,"WorkSheets", 1, rng);
		appo=xltmp1.PutProperty("Name", "My Other Sheet"); 
pujari
Experienced Solver
Experienced Solver
Posts: 52
Joined: Fri Sep 07, 2007 11:58 am

Post by pujari »

Thank You very much for the reply.I am really very thankful to you for your immediate response.

Can you please just do me one more favor.Again i need your help in writing some data and inserting some pictures on a PPT file (Power Point Presentation).Somewhere i have read that VBA commands are similar for PPT but I could able to create an instance of PPT.i tried with few of VBA commands but i couldnt proceed further.

I just need to create a new slide and write some text to it and create another slide and insert some pictures on it.

I have some VBA sample code.Just have a look.


Sub AddSlides()
Dim Pre As Presentation
Dim Sld As Slide

Set Pre = ActivePresentation
Set Sld = Pre.Slides.Add(Index:=Pre.Slides.Count + 1, Layout:=ppLayoutText)
Sld.Shapes(1).TextFrame.TextRange = "Title of Slide"
Sld.Shapes(2).TextFrame.TextRange = "Line 1" & vbNewLine & _
"Line 2" & vbNewLine & _
"Line 3" & vbNewLine & _
"Line 4" & vbNewLine

End Sub


Sub InsertGraphicOnSlide()

Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim ppShape As PowerPoint.Shape
Dim ppCurrentSlide As PowerPoint.Slide

Set ppApp = CreateObject("PowerPoint.Application")
ppApp.Visible = True

Set ppPres = ppApp.Presentations.Add(msoTrue)
Set ppCurrentSlide = ppPres.Slides.Add(Index:=1,Layout:=ppLayoutBlank)

With ppCurrentSlide.Shapes
' Adds a picture to slide 1 in the active presentation.
Set oPicture = .AddPicture("D:\test.jpg", _
msoFalse, msoTrue, 0, 0, 1, 1)
' Now scale the image
oPicture.ScaleHeight 1, msoTrue
oPicture.ScaleWidth 1, msoTrue

End With
End Sub


Can you pls help me out in porting it to wxwidgets.

Thank you Very much once again.
Thank you for your time.
timg
Earned some good credits
Earned some good credits
Posts: 148
Joined: Mon Jan 23, 2006 6:52 pm

Post by timg »

I have not done anything with trying to automate Powerpoint.

Maybe you can look at the code I already posted for Excel and figure out how to translate your VB code. I would think the processes would be similar.
pujari
Experienced Solver
Experienced Solver
Posts: 52
Joined: Fri Sep 07, 2007 11:58 am

Post by pujari »

Thank you very much.

If you have any idea regarding this issue,please let me know.

Thank you.
Post Reply