sábado, 6 de junho de 2009

Como usar recursos do word?

Uses
ComObj, ActiveX;

var
OldWord, NewWord: Variant;

function GetOrCreateObject (const ClassName: string): IDispatch;
var
ClassID: TGUID;
Unknown: IUnknown;
begin
ClassID := ProgIDToClassID (ClassName);
if Succeeded (GetActiveObject (ClassID, nil, Unknown)) then
OleCheck (Unknown.QueryInterface (IDispatch, Result))
else
Result := CreateOleObject (ClassName);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
OldWord := GetOrCreateObject ('Word.Basic');
NewWord := GetOrCreateObject ('Word.Application');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
OldWord.FileNew;//criar um arquivo novo
OldWord.AppShow; //chamar a aplicação
OldWord.Insert ('Mastering Delphi by Pacal Votan'); //inserir o texto
end;

procedure TForm1.Button2Click(Sender: TObject);
var
Document, Range: Variant;
begin
NewWord.Visible := True;
NewWord.Documents.Add;
// inserir texto
NewWord.Documents.Item(1).Range.
Text := 'Mastering Delphi by Pacal Votan';
// get document and add paragraph
Document := NewWord.Documents.Item(1);
Document.Paragraphs.Add;
Document.Paragraphs.Add;
// selecionar o terceiro parqagrafo
Range := Document.Paragraphs.Item(3).Range;
Range.Text := 'Hello, Delphi';
Range.Bold := 1;
Range.Font.Size := 30;
// salvar o arquivo gerado
if SaveDialog1.Execute then
Document.SaveAs (WideString (SaveDialog1.Filename), 0);
Document.SaveAs (FileName := WideString (SaveDialog1.Filename),
FileFormat := 0, //formato nativo do word
SaveNativePictureFormat := 1); // true
end;

Nenhum comentário:

Postar um comentário