Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Delphi, OpenOffice , OLE

Posted on 2009-04-29
9
Medium Priority
?
5,403 Views
Last Modified: 2012-06-27
Hello,

     My Application supposed to Move a Grid to an Excel sheet. everything works fine with me as  for Microsoft Excel. But now I have some customers they don't use Microsoft Excel and they are using OpenOffice Calc instead.

  I don't know how to deal with OpenOffice. Actually it is an important option in my application which is moving a Grid Data to an Excel Sheet.

kindly find below my current code for Microsoft Excel, I need it to be converted to work with OpenOffice Calc.

another point, I want to make my Application check if the customer PC has Microsoft Excel or OpenOffice Calc.
var
   ExcelApp : Variant;
   Sheet    : variant;
   I, J     : Integer;
begin
  ExcelApp:= CreateOleObject('Excel.Application');
  ExcelApp.Workbooks.Add;
  Sheet := ExcelApp.Workbooks[1].WorkSheets[1];
  ExcelApp.Rows[1].RowHeight := 16;
  for i := 0 to Grid.Columns.Count-1  do
  begin
    Sheet.Cells[1,i+2] := Grid.Columns[i].Title.Caption;
    Sheet.Columns[i+2].ColumnWidth := Grid.Columns[i].Width div 7;
    Sheet.Columns[i+2].HorizontalAlignment := -4108;
    Sheet.Cells[1,i+2].Font.Bold  := True;
    Sheet.Cells[1,i+2].Font.Size  := 12;
  end;
  Sheet.Columns[1].HorizontalAlignment := -4108;
  Sheet.Cells[1,1].Font.Bold  := True;
  Sheet.Cells[1,1].Font.Size  := 12;
  Sheet.Cells[1,1] := String('#');
 
    for i := 0 to Grid.Columns.Count-1  do
    begin
     Sheet.Cells[j, i+2] := Grid.DataSource.DataSet.Fields[i].AsString;
    end;
    Sheet.Cells[j,1] := IntToStr(J-1);
 
 
  ExcelApp.Visible := True;

Open in new window

0
Comment
Question by:i7mad
  • 5
  • 4
9 Comments
 
LVL 5

Expert Comment

by:louislietaer
ID: 24258713
As Excel is not install on your client PC you will not be able to run ole on excel.

here is a download link for openoffice sdk. Install it there are some delphi examples.

http://download.openoffice.org/3.0.0/sdk.html
0
 

Author Comment

by:i7mad
ID: 24258725
louislietaer

 I Know that, I just want:

1- Check if Excel Installed on current machine.
2- if not, Check if OpenOffice Calc installed.
3- deal with OpenOffice Calc as code attached in my first post here.
0
 
LVL 5

Expert Comment

by:louislietaer
ID: 24258792
For point 1.
var
  ClassID: TCLSID;
  strOLEObject: string;
begin
  strOLEObject := 'Excel.Application';
  if (CLSIDFromProgID(PWideChar(WideString(strOLEObject)), ClassID) = S_OK)
then
  begin
    <application is installed>
  end
  else
  begin
    <application is NOT installed>
  end
end;

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:i7mad
ID: 24258819
louislietaer:


[DCC Error] MainMenu.pas(1250): E2003 Undeclared identifier: 'TCLSID'
0
 
LVL 5

Expert Comment

by:louislietaer
ID: 24259025
uses
  ComObj, ActiveX; // here an other example uses clause what missing in the first one

function IsObjectActive(ClassName: string): Boolean;
var
  ClassID: TCLSID;
  Unknown: IUnknown;
begin
  try
    ClassID := ProgIDToClassID(ClassName);
    Result  := GetActiveObject(ClassID, nil, Unknown) = S_OK;
  except
    // raise;
    Result := False;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if IsObjectActive('Word.Application') then ShowMessage('Word is running !');
  if IsObjectActive('Excel.Application') then ShowMessage('Excel is running !');
  if IsObjectActive('Outlook.Application') then ShowMessage('Outlook is running !');
  if IsObjectActive('Access.Application') then ShowMessage('Access is running !');
  if IsObjectActive('Powerpoint.Application') then ShowMessage('Powerpoint is running !');
end;
0
 

Author Comment

by:i7mad
ID: 24259344
Thanks it worked for me as for Excel.

 Now the important part, is how to OLE over OpenOffice Calc, like what I did in Excel?
0
 
LVL 5

Expert Comment

by:louislietaer
ID: 24260711
It is a bit different.

Do you want I write the code for you ?
0
 

Author Comment

by:i7mad
ID: 24260733
louislietaer

  I will be very thankfull for you if you write the code for me

same as above code, but works for OpenOffice Calc.

Thanks in Advance
0
 

Accepted Solution

by:
i7mad earned 0 total points
ID: 24323435
I did it myself.

here we go:
           if isOpenOfficeInstalled then
           begin
              objServiceManager := CreateOleObject('com.sun.star.ServiceManager');
              Stardesktop := objServiceManager.createInstance('com.sun.star.frame.Desktop');
              Document := Stardesktop.LoadComponentFromURL('private:factory/scalc','_blank', 0, VarArrayCreate([0, - 1], varVariant));
              Sheet := Document.getSheets.getByName('Sheet1');
              Document.getCurrentController.getFrame.getContainerWindow.setVisible(False);
 
              for i := 0 to Grid.Columns.Count-1  do
              begin
                Sheet.getCellByPosition(i+1, 0).CharHeight := 12;
                Sheet.getCellByPosition(i+1, 0).CharFontName := 'Arial';
                Sheet.getCellByPosition(i+1, 0).CharWeight := 150;
 
                Sheet.getCellByPosition(i+1, 0).getColumns.getByIndex(0).SetPropertyValue('Width', Grid.Columns[i].Width * 35);
                Sheet.getCellByPosition(i+1, 0).getRows.getByIndex(0).setPropertyValue('Height', 600);
                Sheet.getCellByPosition(i+1, 0).getColumns.getByIndex(0).paraAdjust := 3;
 
                Sheet.getCellByPosition(i+1, 0).String := Grid.Columns[i].Title.Caption;
              end;
 
              Sheet.getCellByPosition(0, 0).getColumns.getByIndex(0).paraAdjust := 3;
 
              Sheet.getCellByPosition(0, 0).CharHeight := 12;
              Sheet.getCellByPosition(0, 0).CharFontName := 'Arial';
              Sheet.getCellByPosition(0, 0).CharWeight := 150;
 
 
              Sheet.getCellByPosition(0, 0).String := String('#');
              Grid.DataSource.DataSet.First;
              J := 2;              
              Grid.DataSource.DataSet.First;
              While Not Grid.DataSource.DataSet.EOF do
              begin
                for i := 0 to Grid.Columns.Count-1  do
                begin
                 Sheet.getCellByPosition(i+1, j-1).CharHeight := 11;
                 Sheet.getCellByPosition(i+1, j-1).CharFontName := 'Arial';
 
                 Sheet.getCellByPosition(i+1, j-1).String := Grid.DataSource.DataSet.Fields[i].AsString;
                end;
 
                Sheet.getCellByPosition(0, j-1).CharHeight := 11;
                Sheet.getCellByPosition(0, j-1).CharFontName := 'Arial';
 
                Sheet.getCellByPosition(0, j-1).String := IntToStr(J-1);
                Sheet.getCellByPosition(0, j-1).getRows.getByIndex(0).setPropertyValue('Height', 600);
                WaitSetProg(Grid.DataSource.DataSet.RecNo, Grid.DataSource.DataSet.RecordCount);
                Grid.DataSource.DataSet.Next;
                J := J + 1;
              end;              
              Document.getCurrentController.getFrame.getContainerWindow.setVisible(True);

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

575 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question