Автоматизация OpenOffice Writer
От: WelfGiver  
Дата: 24.05.05 09:17
Оценка:
Прочитал статью "Автоматизация OpenOffice.org Calc при помощи Borland C++ Builder 6.0"
в RSDN Magazine #6-2004 и вдруг вспомнил, что мне нужно сделать нечто подобное, только
для OpenOffice Writer и с помощью Delphi. По указанным ссылкам для Delphi нашел пример,
но тоже только для Calc. Пришлось повозиться самому.
Вот пример, может кому надо, чтобы не копать с нуля.
Работает под Windows 2000 и OpenOffice.org 1.1.3, локализованным с запущенным QuickStart.


procedure TForm1.Button1Click(Sender: TObject);
var
  LoadParams, aLoader: Variant;
  ServiceManager, Desktop : Variant;
  CoreReflection, PropertyValue, VA: Variant;
  Document: Variant;
  TableCursor, Table, TableCell : Variant;
  obj, objText, objCursor : Variant;
  w : integer;
  Cols : Variant;
begin
  if VarIsEmpty(ServiceManager) then
    try
      ServiceManager := CreateOleObject('com.sun.star.ServiceManager');
    except
      Raise Exception.Create('Couldn''t get ServiceManager');
      Exit;
    end;

  CoreReflection := ServiceManager.createInstance('com.sun.star.reflection.CoreReflection');
  CoreReflection.forName('com.sun.star.beans.PropertyValue').createObject(PropertyValue);
  VA := VarArrayCreate([0, 0], varVariant);
  VA[0] := PropertyValue;

  Desktop := ServiceManager.createInstance('com.sun.star.frame.Desktop');
  Document := Desktop.LoadComponentFromURL( 'private:factory/swriter','_blank', 0,  VA);
  if (VarIsNull(Document) or VarIsEmpty(Document)) then
     begin
       Raise Exception.Create('Couldn''t create document');
       Exit;
     end;

  objText := Document.getText;
  //Create a cursor object
  objCursor := objText.createTextCursor;

  //Inserting some Text
  objCursor.setPropertyValue('CharHeight',12); //size
  objCursor.setPropertyValue('CharWeight',150);//bold
  objCursor.setPropertyValue('ParaAdjust',3);  //center
  objText.insertString(objCursor, 'The first line.', false);
  objText.insertControlCharacter(objCursor, 0, false ); //PARA_BREAK
  objText.insertControlCharacter(objCursor, 1, false ); //LINE_BREAK
  objText.insertControlCharacter(objCursor, 0, false ); //PARA_BREAK

  objCursor.setPropertyValue('CharHeight',20); //size
  objCursor.setPropertyValue('CharWeight',150);//bold
  objCursor.setPropertyValue('ParaAdjust',0);  //left
  objCursor.setPropertyValue('CharUnderline',1);//0-none, 1-single,...,18-BOLDWAVE
  objText.insertString(objCursor, 'The second line.', false);

  objCursor.setPropertyValue('CharHeight',12); //size
  objCursor.setPropertyValue('CharWeight',100);//normal
  objCursor.setPropertyValue('CharUnderline',0);//0-none
  objText.insertString(objCursor, 'The second line.', false);

  objText.insertControlCharacter(objCursor, 0, false ); //PARA_BREAK
  objText.insertControlCharacter(objCursor, 0, false ); //PARA_BREAK

  //Inserting some Table
  Table := Document.createInstance( 'com.sun.star.text.TextTable' );
  Table.initialize(5, 4);  //row, column
  Table.HoriOrient := 0;
  Table.LeftMargin := 200; // 2* 1/100 mm;
  Table.RightMargin := 200;

  Document.getText.insertTextContent( objCursor, Table, false );

  obj := Table.TableColumnSeparators;
  obj[0].Position := 750;
  obj[1].Position := obj[0].Position+2000;
  obj[2].Position := obj[1].Position+2000;

  Table.TableColumnSeparators := obj;

  //   Cols := Table.getColumns;
  //   w :=  Cols.getCount;

  TableCell := Table.getCellByName('A1');
  TableCell.setString('A1');
  TableCell := Table.getCellByPosition(1, 1);
  TableCell.setString('**B2**');
  TableCell := Table.getCellByPosition(2, 1);
  TableCell.setString('**C2**');

end;
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.