René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Generating MS Word documents with Oracle

In order to use this example, COM must be installed and configured on Oracle.
set serveroutput on size 100000

declare
  word         binary_integer; 
  hr           binary_integer;
  dummy        binary_integer;
  documents    binary_integer;
  document     binary_integer;
begin
  hr := ordcom.CreateObject (
    'Word.Application',
    0,                    -- reserved
    '',                   -- remote dcom server
    word
  );

  if hr <> 0 then
    dbms_output.put_line('Could not create Word object');
    return;
  end if;

--  hr := ordcom.SetProperty (
--    word,
--    'Visible', -- name of property
--     1,
--    'I4');
--
--  if hr <> 0 then
--    dbms_output.put_line('Could not set property');
--    return;
--  end if;


  hr := ordcom.GetProperty (
    word,
    'Documents',
    0,
    documents);

  if hr <> 0 then
    dbms_output.put_line('Could not get Documents property');
    return;
  end if;

  hr:= ordcom.Invoke (
    documents,
    'Add',
    0,
    document);

  if hr <> 0 then
    dbms_output.put_line('Could not add Document to Documents');
    return;
  end if;

  ordcom.SetArg (
    'C:\oracle_generated.doc',
    'BSTR');

  --if hr <> 0 then
  --  dbms_output.put_line('Could not set arg [filename]');
  --  return;
  --end if;

  hr:= ordcom.Invoke (
    document,
    'SaveAs',
    1,
    dummy);

  if hr <> 0 then
    dbms_output.put_line('Could not save document');
    return;
  end if;

  if hr <> 0 then
    dbms_output.put_line('Could not quit word');
    return;
  end if;

  hr:= ordcom.Invoke (
    word,
    'Quit',
    0,
    dummy);

  if hr <> 0 then
    dbms_output.put_line('Could not quit word');
    return;
  end if;

  hr:= ordcom.DestroyObject (word);
  if hr <> 0 then
    dbms_output.put_line('Could not set Visibile property');
    return;
  end if;
end;
/