2

I need to insert images in "Calc" file using Open Office. The code from the answer to this question with minor changes works fine with Open Text documents so I decided to enhance it for Calc files but did not succeed - assigning Graphic.GraphicURL causes OLE error. How to fix it?

procedure InsertImage2;
var
  OleApp, Desktop, OpenParams, SaveParams, Document, Bitmaps, Sheet: Variant;
  SearchDescriptor, TextCursor, Graphic, Found: Variant;
  cb: integer;
  sSourceFile, sDestFile, sImageFile, sImageName, sLocationWord: string;
begin
  sSourceFile := 'file:///c:/test.ods';
  sDestFile := 'file:///c:/test2.ods';
  sImageFile := 'file:///c:/stamp.png';
  sImageName := 'Image1';
  sLocationWord := '[stamp]';
  OleApp := CreateOleObject('com.sun.star.ServiceManager');
  Desktop := OleApp.CreateInstance('com.sun.star.frame.Desktop');
  OpenParams := VarArrayCreate([0, -1], varVariant);
  Document := Desktop.LoadComponentFromURL(sSourceFile, '_blank', 0, OpenParams);
  Bitmaps := Document.CreateInstance('com.sun.star.drawing.BitmapTable');
  Bitmaps.insertByName(sImageName, sImageFile);
  if Document.supportsService('com.sun.star.sheet.SpreadsheetDocument') then
  begin
    for cb := 0 to Document.Sheets.Count - 1 do
    begin
      Sheet := Document.Sheets.getByIndex(cb);
      SearchDescriptor := Sheet.createSearchDescriptor;
      SearchDescriptor.setSearchString(sLocationWord);
      Found := Sheet.findFirst(SearchDescriptor);
      While not VarIsClear(Found) do
      begin
        TextCursor := Found.GetText.createTextCursor;
        TextCursor.gotoRange(Found, False);
        Graphic := Document.CreateInstance('com.sun.star.text.GraphicObject');
        Graphic.GraphicURL := Bitmaps.getByName(sImageName); //error "Variant does not reference an automation object"
        Graphic.AnchorType := 1;
        Graphic.Width := 6000;
        Graphic.Height := 6000;
        Found.GetText.insertTextContent(TextCursor, Graphic, True);
        Found := Sheet.findNext(Found.End, SearchDescriptor);
      end;
      SearchDescriptor := Unassigned;
      TextCursor := Unassigned;
      Found := Unassigned;
      Graphic := Unassigned;
    end;
    SaveParams := VarArrayCreate([0, -1], varVariant);
    Document.StoreAsURL(sDestFile, SaveParams);
  end;
  Document.Close(True);
  Bitmaps := Unassigned;
  OpenParams := Unassigned;
  SaveParams := Unassigned;
  Document := Unassigned;
end;

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.