OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Using COM for OOo with different languages
Goto page Previous  1, 2, 3, 4, 5, 6  Next
 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
pasha_golub
General User
General User


Joined: 29 Jun 2004
Posts: 24
Location: Ukraine

PostPosted: Wed Jul 07, 2004 6:45 am    Post subject: Reply with quote

Code:

procedure TForm1.Button1Click(Sender: TObject);
var ServiceManager, Desktop: variant;
begin
   ServiceManager := CreateOleObject('com.sun.star.ServiceManager');
   Desktop := ServiceManager.createInstance('com.sun.star.frame.Desktop');
   ShowMessage(''''''''); //for pause
   Desktop.Terminate;
end;



What I have in result:

Program SOFFICE ... at adress 00de:00000038
Registers:
EAX=01971364 CS=017f EIP=00000038 EFLGS=00010246
EBX=00000000 SS=0187 ESP=0123f9d4 EBP=0123f9e8
ECX=01971364 DS=0187 ESI=010244a8 FS=65e7
EDX=01971358 ES=0187 EDI=03a56df4 GS=0000
Bytes at address CS:EIP:
9a 00 e3 09 65 04 70 00 07 00 70 d0 4d f8 00 f0
Stack:
02b9dab0 01971364 00000002 036f5c78 000000c1 0123fa50 037a4386 0135000c 0129e970 00000000 036f5c70 7800db11 780332a0 ffffffff 0123fa8c 7800b2de

Then I tried your code:
Code:

procedure TForm1.Button1Click(Sender: TObject);
var ServiceManager, Desktop: variant;
begin
   ServiceManager := CreateOleObject('com.sun.star.ServiceManager');
   Desktop := ServiceManager.createInstance('com.sun.star.frame.Desktop');
   ShowMessage('''''''');
   Desktop.Terminate();
end;


In result I have OO error (or OLE, I don't know): Type missmatch. This is because Delphi insert empty variant parameter in case of empty brackets.
_________________
Nullus est in vitae sensus, ipsa vera est sensus!
Back to top
View user's profile Send private message
Cybb20
Super User
Super User


Joined: 02 Mar 2004
Posts: 1572
Location: Frankfurt, Germany

PostPosted: Wed Jul 07, 2004 6:53 am    Post subject: Reply with quote

O, I see now, thanks.
Now what happens if you run my example?
Because I have text := doc.getText() in it, which would probably throw an error then.

As to your problem I have no idea.
_________________
- Knowledge is Power -
Back to top
View user's profile Send private message Send e-mail
pasha_golub
General User
General User


Joined: 29 Jun 2004
Posts: 24
Location: Ukraine

PostPosted: Wed Jul 07, 2004 11:49 pm    Post subject: Reply with quote

Cybb20 wrote:
O, I see now, thanks.
Now what happens if you run my example?
Because I have text := doc.getText() in it, which would probably throw an error then.

As to your problem I have no idea.


Let's try your code, but there are some mistakes. I will correct them in example:
Code:

var
    objServiceManager: Variant;
    Stardesktop: Variant;
    doc: Variant;
    text: Variant;
begin
  objServiceManager := CreateOleObject('com.sun.star.ServiceManager');
  Stardesktop := objServiceManager.createInstance('com.sun.star.frame.Desktop');
  doc := Stardesktop.LoadComponentFromURL('private:factory/swriter', '_blank', 0, VarArrayCreate([0, - 1], varVariant));
  text := doc.getText; //was ...getText(), but this cause type missmatch
  text.setString('Hello World'); //semicolon missed
  //CreateTextDocument := Document; //commented, we don't need this
  StarDesktop.Terminate; //this code cause AV again, even don't know why
end;


I'll try next:
1. Change OS to WinXP
2. Install OOO from scratch
3. Try to run this code again. Then we'll see. May be Win98 causes this.

PS If Delphi is not your native language I can help with some examples. All I need is proper code in VB, for example or in other language that uses OLE bridge.
_________________
Nullus est in vitae sensus, ipsa vera est sensus!
Back to top
View user's profile Send private message
pasha_golub
General User
General User


Joined: 29 Jun 2004
Posts: 24
Location: Ukraine

PostPosted: Thu Jul 08, 2004 4:34 am    Post subject: Reply with quote

Quote:
I'll try next:
1. Change OS to WinXP
2. Install OOO from scratch
3. Try to run this code again. Then we'll see. May be Win98 causes this.


It seems to me that I found problem. This code works properly on Windows XP. Now about Win98. I reinstalled OO, and tried to run my app. All works properly. But I saw, that OO QuickStart was enabled in system tray. I decided to disabled it for experiment, so I choose menu item 'Exit from QuickStart'. Then I ran my code again and gues what? Smile Yes, in result I had AV.

For use with Win98 OLE-bridge I suggest to enable "quickstart OO " in the system tray. But, I think that, this is the bug and developers sholud fix it. [/quote]
_________________
Nullus est in vitae sensus, ipsa vera est sensus!
Back to top
View user's profile Send private message
Cybb20
Super User
Super User


Joined: 02 Mar 2004
Posts: 1572
Location: Frankfurt, Germany

PostPosted: Thu Jul 08, 2004 6:21 am    Post subject: Reply with quote

Can you please provide the full (corrected) code that worked for you?
I didn't test anything with the Delphi code, so I am glad that you encountered this thread and you contribute to it, very nice.

Thanks.
Christian
_________________
- Knowledge is Power -
Back to top
View user's profile Send private message Send e-mail
pasha_golub
General User
General User


Joined: 29 Jun 2004
Posts: 24
Location: Ukraine

PostPosted: Thu Jul 08, 2004 6:55 am    Post subject: Reply with quote

With great pleasure. This is the unit, that consists of one class TOpenOffice.

Code:

unit SampleCode;

interface

uses
   Windows, Messages, SysUtils, Classes, Graphics, Controls,
   Forms, Dialogs, StdCtrls, ComObj, Variants;

   type
   TOpenOffice = class
      function Connect: boolean;
      procedure Disconnect;
      function CreateDocument: boolean;
      function OpenDocument(const aFileUrl:string): boolean;
      procedure SaveDocument(const aFileUrl:string);
      procedure CloseDocument(const Flag:boolean);
      procedure InsertText(const aText: String);
   private
      StarOffice: Variant;
      StarDesktop: variant;
      Document: Variant;
      function MakePropertyValue(PropName:string; PropValue:variant):variant;
   end;

implementation

function TOpenOffice.MakePropertyValue(PropName:string; PropValue:variant):variant;
var Struct: variant;
begin
    Struct := StarOffice.Bridge_GetStruct('com.sun.star.beans.PropertyValue');
    Struct.Name := PropName;
    Struct.Value := PropValue;
    Result := Struct;
end;


function TOpenOffice.Connect: boolean;
begin
   if VarIsEmpty(StarOffice) then
      StarOffice := CreateOleObject('com.sun.star.ServiceManager');
   StarDesktop := StarOffice.createInstance('com.sun.star.frame.Desktop');
   Result := not (VarIsEmpty(StarOffice) or VarIsNull(StarOffice));
end;

procedure TOpenOffice.Disconnect;
begin
   StarOffice := Unassigned;
end;

function TOpenOffice.CreateDocument: boolean;
begin
   Document := StarDesktop.LoadComponentFromURL(
                  'private:factory/swriter', '_blank', 0,
                  VarArrayCreate([0, -1], varVariant));
   Result := not (VarIsEmpty(Document) or VarIsNull(Document));
end;

function TOpenOffice.OpenDocument(const aFileUrl:string): boolean;
var
   VariantArr: variant;
begin
   VariantArr := VarArrayCreate([0, 1], varVariant);
   VariantArr[0] := MakePropertyValue('FilterName', 'HTML (StarWriter)');
   VariantArr[1] := MakePropertyValue('Hidden', True);
   Document := StarDesktop.LoadComponentFromURL(
                  aFileUrl, '_blank', 0,
                  VariantArr);
   Result := not (VarIsEmpty(Document) or VarIsNull(Document));
end;

procedure TOpenOffice.SaveDocument(const aFileUrl:string);
var
   VariantArr: variant;
begin
   VariantArr := VarArrayCreate([0, 0], varVariant);
   VariantArr[0] := MakePropertyValue('FilterName', 'MS Word 97');
   Document.StoreToURL(aFileUrl, VariantArr);
end;

procedure TOpenOffice.InsertText(const aText: String);
var
   oCursor: Variant;
   oText: Variant;
begin
//get document text object
   oText := Document.GetText;
//create cursor
   oCursor := oText.CreateTextCursor;
//set some text properties
   oCursor.SetPropertyValue('CharColor', 255);
   oCursor.SetPropertyValue('CharShadowed', True);
//insert string
   oText.InsertString(oCursor, aText, false);
//insert line break character
   oText.InsertControlCharacter(oCursor, 0, false);
end;

procedure TOpenOffice.CloseDocument(const Flag:boolean);
begin
  Document.Close(Flag);
  StarDesktop.Terminate;
end;

end.


Example of use:
Code:

var OO: TOpenOffice;
begin
  OO := TOpenOffice.Create;
try
 if OO.Connect then
  begin
   OO.OpenDocument('file:///D:/example.html');
   OO.InsertText('Added by OLE automation');
   OO.SaveDocument('file:///D:/example.doc');
   OO.CloseDocument(True);
   OO.Disconnect;
  end;
finally
 OO.Free;
end;
end;


This example starts OO in invisible mode, then load .html file 'example.html', then save it as .doc file 'example.doc'. And terminate all started connections.

You can also use Delphi analog of MakePropertyValue realized through Bridge_GetStruct.
_________________
Nullus est in vitae sensus, ipsa vera est sensus!
Back to top
View user's profile Send private message
Cybb20
Super User
Super User


Joined: 02 Mar 2004
Posts: 1572
Location: Frankfurt, Germany

PostPosted: Thu Jul 08, 2004 7:42 pm    Post subject: Reply with quote

That's nice but too long, please just correct my given example and don't add any commands like you did.
I really appreciate your support, but I can't use your example cause it's too long.
I corrected my example and it would be nice if you could tell me if it works now for you, if not go ahead and post the example with the correct changes in the syntax.

Christian

Thanks again Smile
_________________
- Knowledge is Power -
Back to top
View user's profile Send private message Send e-mail
pasha_golub
General User
General User


Joined: 29 Jun 2004
Posts: 24
Location: Ukraine

PostPosted: Thu Jul 08, 2004 10:44 pm    Post subject: Reply with quote

Oh, I see. Very Happy

Now your code is looking good, but as for me there is no need to use class model, IMHO. Code is correct. May I help you in other way, i.e. with Calc or Draw examples? Very Happy
_________________
Nullus est in vitae sensus, ipsa vera est sensus!
Back to top
View user's profile Send private message
flex
OOo Enthusiast
OOo Enthusiast


Joined: 06 Jul 2004
Posts: 122
Location: Vienna | Austria

PostPosted: Thu Jul 15, 2004 12:03 am    Post subject: Re: Using COM for OOo with different languages Reply with quote

Hello!
I tried to run your code, Cybb20. But there were troubles:
The first code example (open Word) crashes, and I got an message that OOo will terminate and I should send an error report. I saw no piece of Word.
And the second code snippet produced an error message at line :
IE.Navigate("http://www.openoffice.org")
The message said:
"Runtime Error - An exception occurs - Type: com.sun.star.lang.IllegalArgumentException - Message: ."
The IE window opened but did not change to www.openoffice.org.
I am running Windows 2000, OOo 1.1.0, IE 6, MS Office 2000.
Cybb20 wrote:
B1 Starbasic:
Code:

Sub loading_MSWord( )
 Dim oleService As Object
 Dim oword As Object
 Dim odoc As Object

 oleService = createUnoService("com.sun.star.bridge.OleObjectFactory")
 oword = oleService.createInstance("Word.Application")
 oword.Visible = True
 odoc = oword.Documents.Add
 odoc.Range.Text = "Hello World!"
End Sub


B2 StarBasic:
Code:

Sub using_IE( )
 Dim oleService
 Dim IE   
 
 oleService = createUnoService("com.sun.star.bridge.OleObjectFactory")
 IE = oleService.createInstance("InternetExplorer.Application.1")
 IE.Visible = 1
 IE.Navigate("http://www.openoffice.org")
End Sub

Do you know these problems?
Back to top
View user's profile Send private message
pasha_golub
General User
General User


Joined: 29 Jun 2004
Posts: 24
Location: Ukraine

PostPosted: Thu Jul 15, 2004 12:51 am    Post subject: Reply with quote

This is StarBasic script. And you, as I think, use it from Word Visual Basic. Am I right?
_________________
Nullus est in vitae sensus, ipsa vera est sensus!
Back to top
View user's profile Send private message
flex
OOo Enthusiast
OOo Enthusiast


Joined: 06 Jul 2004
Posts: 122
Location: Vienna | Austria

PostPosted: Thu Jul 15, 2004 12:52 am    Post subject: Reply with quote

No, I run it from OOBasic.
Back to top
View user's profile Send private message
Cybb20
Super User
Super User


Joined: 02 Mar 2004
Posts: 1572
Location: Frankfurt, Germany

PostPosted: Thu Jul 15, 2004 6:14 am    Post subject: Reply with quote

Download the newest OOo version flex, I think that's the problem.
_________________
- Knowledge is Power -
Back to top
View user's profile Send private message Send e-mail
flex
OOo Enthusiast
OOo Enthusiast


Joined: 06 Jul 2004
Posts: 122
Location: Vienna | Austria

PostPosted: Mon Jul 19, 2004 6:53 am    Post subject: Reply with quote

Cybb20 wrote:
Download the newest OOo version flex, I think that's the problem.

That's not possible, since I use OO at my job and I am not admin/root.
But on the one hand it is not important whether this code works or not and on the other hand I think OO will getting updatet in the near future by our admin/root.
Back to top
View user's profile Send private message
Cybb20
Super User
Super User


Joined: 02 Mar 2004
Posts: 1572
Location: Frankfurt, Germany

PostPosted: Mon Jul 19, 2004 7:58 am    Post subject: Reply with quote

There are many things that can throw exceptions for COM usage, especially if you're in a network and you don't have all the access that is needed.
The examples itself do all work correctly, the problem is often just how Windows is set up.
I am sorry to say that I am not very skilled with that, therefore your administrator should know about the needed things, although sometimes trying to find these things can be a total headache on Windows, cause as oftentimes stated Windows can't just be configured easily, it's often just a mess dealing with these things on Windows.

Christian
_________________
- Knowledge is Power -
Back to top
View user's profile Send private message Send e-mail
flex
OOo Enthusiast
OOo Enthusiast


Joined: 06 Jul 2004
Posts: 122
Location: Vienna | Austria

PostPosted: Wed Jul 21, 2004 2:49 am    Post subject: Reply with quote

Cybb20 wrote:
Download the newest OOo version flex, I think that's the problem.

You were right. That was the problem. With v1.1.2 it works fine.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Goto page Previous  1, 2, 3, 4, 5, 6  Next
Page 2 of 6

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group