Word Desde Delphi

19
El Rinconcito de Delphi Manejo de Word desde Delphi Néstor Freire nestorfreire@elrinconcito.com El presente artículo ha sido cedido para su publicación en El Rinconcito de Delphi. Es ilegal cualquier actuación que vulnere los derechos del autor.

Transcript of Word Desde Delphi

Page 1: Word Desde Delphi

El Rinconcito de Delphi

Manejo de Word desde Delphi

Néstor Freire [email protected]

El presente artículo ha sido cedido para su publicación en El Rinconcito de Delphi. Es ilegal cualquier actuación que vulnere los derechos del autor.

Page 2: Word Desde Delphi

��������������������� ��������������� ��� ���� ����� �������� � � ������ ��� ������� ���� ��� ������ ��� ������������������������������������ �������������������������������������� ������� ������������ ������������������������ ����� ����������������������������������������� ����������������������������!������������"��� �� ����� !� ��� ������������ #��$�� �������� !� ���� ���%������ ������������� �� ����� �� ����� ��������������� �������� ��� �������&���� ��'�� �� ������������������ ����(��!����)***��#����+������'������������������ ������������,-��&�������� ���'�� ������������������ ����� �����%����,-������������������ �������� ��� ���� ��.��������� ���������������������'�� �������� ����/�������� �!���� ��������������������� �����������������!�����������������������

uses Windows, Messages, ... , Word97; ������������� ����������� �����������

�0� ����������&��������������� �� ������10� ��������2�������������'������������"������������ ����� ��������� ��!�� ������������#������������&������������������������������������������������!����������������3���������������������+�������������� �����%������ ���� ����������4� ��� ����� ����������� 5�67��8�������9� �� �� ���������"������:����� ��� ����� ������ ����� ���� ���%�� ���������� ��� ��� ����� %��� ��������������� ���� ����� ����&��� ���� ������������ �����������;��

procedure TForm1.Button1Click(Sender: TObject); begin WordDocument1.Activate; WordDocument1.Range.Text := 'Hola, mundo'; WordDocument1.Application.Visible := True;

end;��<� !�� ����� ������� =�� ����������� ������ �������� ���������� ���� ����� �� ������>������ ��� ��+� ����� ������ .��� �� ����� �� ��� ��� ������ ����� ������� %���������������� �� �� %���� $ � ����%�� ��� ���������� �������� ������� ���� ���%��� &�� ������������ '��������������� ���������� �����������5?����9��&�������������������������� ������� &�� ����� ������ ����� ���� ��� ��!� ����� �� ����������� � � ����� ��� ����� � ������������ =�� ����+����� ����� ��� ��� ���������� !�� �������� �� ���'����� ���������������� ��� ������������ ������������������ ������������ ���� ����� ���������������� �������������������%���� ���������������� ���� �������

Page 3: Word Desde Delphi

"������������������������ ���� ����� ���������� ���������������������������� �������� ���%�� ����� ���������� ��� ������������&���� ��� ��'� �%�������� � ��������%���� ������%����3�������� '����� �!����������!�� ��%���� ���� ������@������������%�������;������������������������ ��������������������������������������������>������������ �������������� ��������&�������������� ������������& ����������������� ����%���������������"����������������A����B����������!��������������������������A$="88�CDB��& ���������������������������� �� '����������������������� ����� ��������� �� ��� ������ �������� A� � �E���� D4� � � E ����B�� �� �� ����� ���%��A� �������������+���� ������B���"��� � � � ����� ������� ������ ������ ��� ������ ���� ����� � � ����������� !�� �����������������������������������E����������������$������������������������& ����%�� ������ ������ �������� ������ �� � ������ �������� !�� ���� �������� � � �+�����18�����������F����2����>������� ������� �������������������������+���� �� ������� �0� ����������

procedure TForm1.Button1Click(Sender: TObject); begin ... WordDocument1.Range.InsertParagraphBefore; WordDocument1.Paragraphs.First.Range.Text := 'Párrafo insertado antes' + #13;

end; �&�� �������� '������ �1?����2��� ������ ������� � � �����������!�����������!�������� �����������&���������������������G��������� ���������������� ������������&�� ����������� � � �������� ���� ������ ������ �'� �� ����������� � � ����� ��� �������� &�� ����������� �������� ��������� � � ����� ������ �� � �����������& �1?����2� ����� �������������+�����!�������������������������������������&�� ����� �� 1?����2� ��� ��� ���� ���� ������� ���� ����� �� ����� �� � ������������ ����������� &�� � � ����� � ������ ��� �� ������������� ������ ��� �� ���� 1?����2������������������ ������������&��+��������� ��� �������� '���������+������������������� ������������������������ ������������������������� ��� �����������������G�������� ��+��������������������;�=� ����������������!�������������+ ������������������������������� �������������������� ���������������� ����������������������� �� ������������ �������������������������������� ���� �����������18����2��0�!��������������������������� ������ ��������� �����+���������������� � �� ������ ������ ������ ��������&���������������������� '�������� ������������������������������������WordDocument1.Paragraphs.Item(6).Range.Text := ‘Párrafo sexto’;

���� ���������� ��� ������� �3���� ������� ������ !� �����+�� ������ ����� �� ����������AInsertParagraphBefore � InsertParagraphAfter���������%������B��"�������������������������!���������� ����������������� ��� ������ ��� ������� �������������������� ������ �������������3����� �$�����CD��=���������3������� ��������� ���������������������'�����

Page 4: Word Desde Delphi

� � ������ ������ �� ����������� � � ���������� @��� �� ��� �� ��� �3���� ���� ������������������������� ���� �� ��������������������� ������������������� �������;��

procedure TForm1.Button1Click(Sender: TObject); begin ... WordDocument1.Range.InsertParagraphBefore; WordDocument1.Range.InsertParagraphBefore; WordDocument1.Paragraphs.First.Range.Text := 'Párrafo insertado antes’;

end; �����������������+��������������������������������� ������������� �;��WordDocument1.Paragraphs.First.Range.Text := ‘’;

�.�������������%���������� �� ���� ��������������� �� ������� ������ � ��� ����������� &���� �� ���� �� ������ ��!�� ��� ����� ��� ����������� ���� !�� ���+�� �������� !� ������������ ���������������������� ���� ����������� ����� ��������� ��������������� � ���� ��������� �� �� ���� �� ����� ���� ����� ��� �� �������� � � ������ ��� ������� !������+�������������� ��E���������������������� ��������������������

procedure TForm1.Button2Click(Sender: TObject); var S : OleVariant;

Begin … S := 2; WD.Range.Paragraphs.Last.Previous (S).Range.Text := 'hhh'+CHR (13); //Párrafo

previo (2 antes) …

End; �&�� � � ����� ��� �� ��������!�������� � ��������� � ����������������������������� � ����� �� ���� � %���� � � ��������� ������� ����� ���� ��� ������� �������� ��������� ����� ����������������'������� �����������;��

procedure TForm1.Button2Click(Sender: TObject); var S,D : OleVariant;

Begin … S := 2; D := 1; WD.Range.Paragraphs.Last.Previous (S).Next (D).Next (D).Range.Text :=

'hhh'+CHR (13); …

End; �&������������ ������� ��������� ������������� ������������� �)H��������� �E ������&������;��� ������������ �E ������&����������������������������������� �����������������%��� ������������������%������� �����������������������#����+���������������%������ ��������������������������%���� ����!�+����������� ������@ �>�������#����� ��� ������������� ��� ������ ����� ��������+����������������� ������ ������� A����� ���'�� ��� #�������B�� #����+�� ��� ��������������� ������������ ���� ���� ��� %��'��� A1&���!����2B�� &�� ����� ������ ������� ��� %� �� ������������7���������� ��������������������������������

Page 5: Word Desde Delphi

"������������� ����.���%����������� ������������������������������ ��� �E������������������������!������� ��������� ���������������������������������������%���� ��������� �������� �������!���'������������� ��������������� �����$����������� ������%��� ����� ������������������&������������ ���� ����������������� �������������������� �������� ��*��� �������� Var Rango : Range; S,D : OleVariant;

Begin S := 1; D := 3; Rango := WD.Range (S,D)

end; �$��'� ��� �� �������� ��� �����)��D�!�I� �� � ��������������� �� �������� ����� ������������ ��������� ������ ��� ������ ���� �� %���� �� ?������ !� �����+�� ��� �� ������� � ����������!�� ����� ���� ���� ���� ����

Var Rango : Range;

Begin … Rango := WordDocument1.Range; Rango.Start := WordDocument1.Paragraphs.Last.Range.Start+1; Rango.End_ := Rango.Start+3;

end; �=�� ������ ��� ���� 1?�����=���2� !� 1?�����&��J2� ���� ������� ��� ����������� �� ��������$��'������ ��������� ��� �����)��D�!�I��� �E �������������.���%����� ����������� ����������������%���������������#����������� �������� ������������������ ������%�������� ����������������� ������� ��;��Rango.Bold := wdUnderlineSingle;

����� ��� ���� ��� � �� ��!� ���� %� ���� ;� 1C2� !� 1*2�� $���%����� !� �������%�������������%�������� ��� �� ������ ����� ��� ���� 18�� ��2� A"���%�B�� K��� ����!�����A1.��� ���2B��������������������C*�%� �����������������������%����� ����� ��� ����� �������

=�������!���� ��.��� ���7����=���!�������� �� ��.��� ���=��� ��

=���!��������� ����� ��.��� �������=���!������� �� ��.��� ������� ����������� ��.��� ����������

=���!���������� ��.��� ���#���6�?�!���� ��.��� ��������

?�!��!������� ��.��� �����������?�!�������������� ��.��� ��������������

@��� ���� ��.��� ��� �%!�

Page 6: Word Desde Delphi

�&���� ������ ����������������� ���CL��������������!����4������������������ ����� ���������� ����+������@���� ���������������>F�� �'�������� ������� �� ��� CL��&���������+�������������������������!�������������� ���������������������� ��������������������������$�������������������������������������������������������������������� ������������������������$ ��������� ����������������������������������� ������E��� ���������� ��=� �������� ������@������������!���������� ��� ������ ����� �����������&����� ����������������������������������� �������������'�������������WordDocument1.Range.Paragraphs.Last.Range.Paragraphs.Alignment :=

wdAlignParagraphJustify;

�=�� �� �������������� � ����������A����������C�?����B����� ���'�� � ��� �E ����������� A���������K����?����B� !� ��� �� ��� �� � ������ �� A���������$ �������� ;M���$ ����������N�����!B��K���%� ������������� ������;���

�& � ������������ ��� ���� ���� �� ��� � ������� ��������� !� �������� ��� � � ���� ��� �������������� �������������������������� ��������#����+�������������������� �����3����� ���������� ������������ �����������1O���2��=����������������������������������������� ����� ����� ������������ �����WordDocument1.Range.Paragraphs.Last.Range.Font.Size := 12; Rango.Font.Name := ‘Arial’; �G����� ���� ��� ������������������� ������������� �����3�� !�� ������ ��� ����� =�� ����������� � � ������ ��� �� �������� �� ��������� ���� ��� �� ��������� &�� ����;� ��� ���������������� ���������������� ����������� ����!���������������� �������������� ������������������%������������� ���������� ������������'������+����������������� ��������������1F� �2��18�� ��2�!�1.��� ���2��������������� ��� ���������1"� �8����2��������������������������� ���C-��� ������������������ ����"����� �����������������������������

����� �������

$ ���������������� ��$ ����������K����$ �������������� ��$ ����������?�����$ ������������� ��$ ����������"������

$ ������������������� ��$ ����������N�����!�

Page 7: Word Desde Delphi

��������������������������� ������ �� �� ��� ������ �� ��������� 10�����2������ ��� ��� � ���������������������������������������������������� ���� ���������� ������ ���= �������������%� ����;�1*2�A%���� �B�!�1C2�A��� ��B��$ ����� �����1��������2�!�1���������2�� ������ �������������� �����������������������'�����������������'���������$������� ������� ��� ������ ��� ������ �� ���������� ��� ������ ���� 18����$���2� ��18����F����2������3�������������������+�������������� ��� ������������Rango. InsertAfter (‘Texto’);

�"��� �� ��������� 1O������������2� ��� ������ ���� ���� � � ������ ������ ����� �� �������������&������������ ���������� ��� ������������ ��������������������E����������%���������������������%�;��WD.Range.Paragraphs.Last.Range.Font.Position := 3;

�.����������$��������!�������������������� �������%����������������� ����� ������� ����<������������� �������%�����������������;�

WordDocument1.Activate;

�<������%���� �� ���� ����� �;��WordDocument1.Application.Visible := True;

�=����������������E ����������+�� ������'������������ ��������#��$�� ���������&���� ������#������������������������#��$�� ���������K���������'������������� ��%���� ��������������������E ������8��� �������������'������������������������';�� WordApplication1.Visible := True;

���� � �������

$����������A����B� ��$����7���� ��F ��6�$�� � ��F ���

#������� ��#��������>������ ����� ��F����P����

O������ �����6�?���� ��?���

$��� �� ��<� ���F ����� �������

$�� ������� ����6F ���>������� ���� ��#�� �

>���� ��P����>�� ���� ��>�� ���

?���������� ����6?���$��� �������� ����6<� ���P���(*Q� ��P�!(*�P���)(Q� ��P�!)(�

Page 8: Word Desde Delphi

�&�������%��� ������������������������������ ����������� ���� ��������� �������� ���� ��� �� ��� ���� � � �+����� 1" ���2�� ���� ������ ������ ������������������������������������������& ������������������� �������������������� ������������ ������� ��� ���������� ������������ =�� ��� ��� �������� ������������ � ����������

var C : OleVariant;

begin … WordDocument1.Close; … C := 1; WordDocument1.Close (C);

end; �&���������������������%� ���;�*�A7������B�!�C�A����B������������� ������������������ ���� ��+��������1=�%�2�!�1=�%�$�2��K�������������� ���������������� �������!� ������������� ������� ��E �����%������������� ��=��� �����������������%����������� ������������� ������������� �����������������&@ �&���������������������������� �����K������������������������E���������������!� ���� ������ �� ������ ��� ���������� ���� ������� ��� ���������� �� ��� � ��������������������K�����������1=�%��2����� ����������������� ���������������������������G������� �� ��������������������1=�%�$�2;��

� ��� �� ���� ������� �� ��������

O� ������ 7�����!������� ����������� ���C�����O� ������� #�����������������A%�����������B� *�

K��6"�������� 8�������3��������������� O� ����������� "������3�������������������� R787P.7$R�

$��#�?�����O� ��� $3���� � � ���������� �� �� ����� �������%������������

#���

����������� "������3���������������� �����%�� R787P.7$R�?���@� !?����������� ?���������� �� ��� ������� ��� �����

=� �RK������O� ���

&����#��#!��O����� P���� �������� #��� #!��� ���� � �����������

#���

=�%�7���%�������O����� P���� ��%��� �� ��� ������� ��� �������%��� �������� A��� ��!� %�����%�������B�

#���

=�%�O�������� P���� ������������ ������� �������� �������� �����%��

O� ���

=�%�$�$@"&K����� =����!����� �����������������������������$@"&�

O� ���

�&������������������������� ������� ������������������������� �����A��������� ���&���!����B�������� ��� ��������������!� �������������=����������� ����������������� �DH����!���������������� ������������C�)�!�D��.������� ����������� �;��

Page 9: Word Desde Delphi

Var Si, No, Blanco, Nombre, Clave, Tipo : OleVariant;

Begin Tipo := 0; Si := 1; No := 0; Blanco := ‘’; Clave := ‘Contraseña’; Nombre := ‘C:\Documento.doc’; WordDocument1.SaveAs (Nombre, Tipo, EmptyParam, Clave, Si, Blanco, Si, Si,

Si, No, No); End; �& � �������� 1=�%�$�2� ����� � � ����%�� ���� ������� � � �������� =�� � � ����%�� !������������ �����������'������������K����������������������������!����;����� � �����

*� ������������� �����������A����B�C� � ���� ���������������A����B�)� #������ ����A����B�D� #����������� ������� '�����A����B�I� #������������� �������=R�@=�A����B�(� #������������� �������=R�@=������� ������� '�����A����B�/� $���%����������������������A���B�-� #�������������A����B�

�.���%����������������+�����%������������� ������������������ ��+�����1@���2��G����� � � �������� ���� ��� ���������� �������� ��� �� � ���� #��$�� ��������� =������������ �����+�� ���� ������� ���� ������������ � � ������� ��������� ��� ��� ��� ����������������� ������1=�%�$�2;��

� ��� �� ���� ������� �� ��������

O� ������ 7������� �����%�� R@FK8P$#@?8@R�"�����"��%������� 8������ ��� ����'�� ������ � � ������ ���

��� ����"��%��������%������������������������� ���������������%��

O� ���

?���@� !� ���� ���� � � ����%�� ��� ����� =� �RK������

O� ���

$��#�?�����O� ��� $3���� � � ���������� �� �� ����� �������%������������

#���

���������������� "������3����������� ����������� R787P.7$R��������#��� ���� "������3�� ���� ���� �� � ���� �� ���

��������������+��������%�������������R787P.7$R�

?�%��� =��� �����������!�����������������������������'�� ���� ��� ����������� ���������������������������

O� ���

������������������� "������3������ ������ ��� �������� � �����������

R787P.7$R�

����������#��� ���� "������3������������ ������������� ��� ���� ��

R787P.7$R�

O����� & � ������� �� � ����%�� A%�� O� �������� *�

Page 10: Word Desde Delphi

���=�%�$�B��.������� ���������;��

Var Si, No, Blanco, Nombre, Clave, Tipo : OleVariant;

Begin Tipo := 0; Si := 1; No := 0; Blanco := ''; Clave := 'Contraseña'; Nombre := 'C:\Documento.doc'; WordApplication1.Documents.Open (Nombre, No, No, No, Clave, Blanco, Si,

Blanco, Blanco, Tipo); WordApplication1.Visible := True;

end; �

� �������������������� �������������������� �!��� ����� ����� ������������"# �

����$�� ������� �$���$� � ����#����%� &��� �����$�'�!����������������$ ��"# ����# (��

( ���������� �( ��%�)*��(���� �+),���� '-�

�K������������������������!��������� ������������������ ��!�������������������������� �� ��������������������������7������������������������������������������������&������������������������������������������RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR���"�������������������%������������� ;��

Var No, Blanco : OleVariant;

Begin No := 0; Blanco := ''; WordApplication1.Documents.Add (Blanco, No); WordApplication1.Visible := True;

end; �& ���������������������� ���������� ��� ���� ���������%���������&��� �������������������������& ��������������������� �������������������� ���� ��������������������7@#$� ;� $ ������ ��� ��� ��������� ��� ��������� ���� �� ����� !� ������ �� ��� �������%��������������������������� ��������������������� ����� ���!������� ������������������������ ������������ ��������������������"�������� ������������������ ������ ������������ ����������1�����������2���� �� ���� ��� �'�� �� �� � �������� ��������� ������ �� ����� ���������� � &���� ���������������� �����������1��$�� ��������"������2������ ���� ������ ����� ������ �%������� ���� �������� ��� ���� ��� ������� �� ����� ���&����� ���;� @�S����� ������� ��� ����� �� �� ����� �� A�� � � E ����� ���������B4�@���������"���������������������� ���������������%����� �� �������������������A���������������������%��������������B4�!�@�=���.���������������������=�� ������������������������������������������ �������������������������������������

Page 11: Word Desde Delphi

����������������"�������������������������������������������� ����������&������%�����������+�� ������������������� ������� ������������#��$�� ����������

type TForm1 = class(TForm) … procedure Quit (Sender : TObject); …

end; implementation

procedure TForm1.Quit (Sender : TObject); begin ShowMessage ('Cerrado');

end;

procedure TForm1.Button1Click(Sender: TObject); begin … WordApplication1.OnQuit := Quit; …

end; �&�� ��� ����������� ����%���� ��� �������� ��� �%������ 1@�" ���2�� 1@�7��2� !�1@�@���2����8����� ���K�� ������ �� ��� ����������� ��� ������ ����� ��� ���� �����;� ������ � � ���������$�� �������������������������� ������������������������������������������������������� ��� ������ ��& ������������������ �����1����@��2��!������������������������������� �������� ������ ������ ������������� ����18�����2��&��������� �����������;��� ��� �� ���� �������

F��6������ 8������������������������������������ �������� ���#�����������������E���������������������

$������ =��� ���������������������������������%���+��������������������3��� ��� ����� �����%��������������� ��A#���M�$3���B�

?����� &���� ������+������������%�������������>�� ����� �������������@�����O� �7���� 7�����!������� �����%�������� ��������%������������=������

������� �������������#�O� ��O��� ������������������������������?�����M�������O��#��#�J� T �����������������������������?�����M�������O��#��8���� K���� ���������� ����������������������������>����� ���"������ & ��E������������������������� ����������������������������� �������������������������=�����������������!���������

����#!��� S�+�������������������������A���E��������������������B�����#�O� �� @� �������������������������%��"� ���� =����!�%������������������������������������������������� ���

������ ���$���%���������PU� &���� � �� ��� ��� ���� ��� ��� ,L�� ���� ���������� �����������

���������������� �����

Page 12: Word Desde Delphi

����� ��� ������� 8����� ������� ������������������� �A������������������������������������������ �B�

�$ ������ ��� ��� ��������� ���������� ���� ��� ����� �� ���� ���� ����� ?����� �������� ���������������+�������������������������K���%� ����������������������;��

������� ������ ��� �

������$ ��������� 8������������ ����������� ����������� *�������=� ������� 8������� ��� ��������� ���������� C�������"��������� 8������ ������������ ������������+����������� )�������O��#�� 8�������������%� ����������������������O���!�

#�J�D�

������?����@������� .�� ���� ��� ����� ��� ��������� ����� ������ ���������

I�

�1O��2�!�1#�J2��������������%��'������������������� ������&�����������������������������1�����������%� ���2��& ���������18���2���%��������������������������������� ��������������� ������ ��������� �������A������������ �������!������� ������������ ���� ��������������������� �������������� ������������B;��

������� ��������� ��� �

��������������"������� & ��������� ����������������� *���������������� �������� ���� �����������A7E�������

�� �������'�� ������������������ ����B�C�

������"�������� 8������� �� ���������������� ������� )�������=�! ��� 8������ ���� ����� ��� ��� ���� ���

��� ������� ��� � � ������� ���� ����������'�������

D�

�1�����2�����������������+���������������������������������������1O��2�!�1#�J2�� !�� ���� ������� �� ����� � ������ ��� ��� ������ �� � ����������� K��� ������������%���� ���������������������� �������������������������� ����������������������������������$�'��VC��DR(��-V��������� �����������C��D�I�(�!�-���1����#!��2� ������� ��� ����� ������� ��� �������� ����� �� �������� =�� %� �� 1*2���������������1C2����� ����������!�1)2����� ���������

var Si, No, Pagin, Blanco, Elem, Rang : OleVariant;

begin Rang := 4; Pagin := '1, 3-4'; Si := 1; No := 0; Blanco := ''; Elem := 0; WordApplication1.PrintOut (Si, No, Rang , Blanco, EmptyParam, EmptyParam,

Elem, Si, Pagin); end; �

Page 13: Word Desde Delphi

&������������ �����������C��D�!�I��� �����������!����������������������������������������������$����� ��� ������ ���������� ��!� ����� ��������� ���� �������� �� �� ������ ��� &����������� ������������������ ������1@������2����� ��� ����1��$�� �������2��&��������� �����������������;��

� ������� ������

����F��6������ & ������������������� ���������1F��6�����2��=�������������������+��������������%� �����1@������2�

����"�������� 8������ ������ ��� ����������� �� � �������������������18����M�������"�������2���� ������������������� ����3����� ����� ��� ��������

��������� 8������� � ������������� ������� �����A�����B��$ ����������������������������������������� ��

����&%�������8�$��������@��� @� ���� �� ������� ��� �������� ����� ��� ����������������A���E ������������B�

����0�����#���� 8������� ������������������ ����� ��0����������@�������8�$��������@��� @� ���� �� ������� ��� �������� ������� ��� �����

�����������A���E ������������B�������������� 8������ ���������������� ��������������������

18����M���������������2���� ������������������� ����3����� ����� ��� ��������

���������������������������������������������������������������K�����������������������������+�������� ������ ����������������%������������������ ��%����"���������������%���������������������������� �������� ����������������������������� ���������� �������������� ���������!���������� ��������������������� ���������������!��������������������� ����������� �����������18���2��������!��������������������������%��������� ������������������������������������� ������������� �������'������������� ����� ������ ��� ����%�� � � ���������� ������ #��$�� ��������� !� �����+�� ���������������+ ������������������"����������3�����������������A����������%����������B��� ����%��������� �������� ��C���� �� ��������������������!� ���������������� �������� %� �� A� � ���� ��� ������ ��� ���%����� ��� ��������� � � �������� ��� ������� !� ��'�������%������B��$��������������������� �����%�� ������%��������������������&��� ������ ���������������%��� ����������������������������;��

Var Si, No, Blanco, Nombre, Clave, Tipo, Dos : OleVariant;

Begin Dos := 2; Tipo := 0; Si := 1; No := 0; Blanco := ''; Clave := 'Contraseña'; Nombre := 'C:\Documento.doc';

Page 14: Word Desde Delphi

WordApplication1.Documents.Open (Nombre, No, No, No, Clave, Blanco, Si, Blanco, Blanco, Tipo).Activate;

WordApplication1.Visible := True; WordApplication1.Documents.Add (Blanco, No).Activate; WordApplication1.ActiveDocument.Range (EmptyParam, EmptyParam).Text :=

'Hola'; WordApplication1.Documents.Item (Dos).Activate; WordApplication1.ActiveDocument.Range (EmptyParam, EmptyParam).Text :=

WordApplication1.ActiveDocument.Range (EmptyParam, EmptyParam).Text + 'Num2';

end; ������+����������� ���� ���%���� ������������������������������������%��������� ���������� �� ����������������� ����� ����������� ��$����������� ���3�����������������������%���!� ������%��������� �������� '�����&����������������+��������������� �����������C��!�� ��������'���������������� ����������$ ����%�� ���������������������� �����$��������%������ ���������A� ��������'�����������B�!� ���3�������� �������17��)2���0�!������������������������� ���������+�����������������������������������������������!�����������������������������������%��'��������� �������������� ����������������� ���'�� ������������ 1?����2�� =�� ����� ��� ��� ��� ������ ��� ������ ���� ����%���� ����������1?����2�������� �������� ������������!�������������� ���&������������� ��� ��� ������ �� ���������� ��� ������� ���� ��� ���������� �� ���� �� ����� ����������������

Var Dos : OleVariant; Rango : Range;

begin Dos := 2; … WordApplication1.Documents.Item (Dos).Activate; Rango := WordApplication1.ActiveDocument.Range (EmptyParam, EmptyParam); Rango.Text := Rango.Text + 'Num2';

end; �&����� '������������� ������������������ �������E �������� ������ ����������$������� �����������1��������2��������� ��������������1?����2��������1$���%���������2������������������������������������ ����������������%���� �����$������������� �!��������������������'���������������������������������� ���������������������������� ������������ ���������������������������!���������� �����$��'�%������� ���������� ������������������������������������������������������� �������������� ���������� ������� ������ C/I� ����������� 7�� %�!� �� ����� ���'� ������� � �� ������+� ���� ���������������������� �*�������� �C/I���������� ��E����*� ������ �������������A��������B������ ��� ������������ ��� � � ����%�� ��,-����� A� ������.� �� ���������-���B�������� �������� ���� ����� ��� ��� ����������� ��� ������� #����� � ��� ��������� ���

1��$�2���������� ��;�1��$�$�� ��2���1��$�8��"���"����2��

Page 15: Word Desde Delphi

� WordDocument1.Sections.First.Borders.Item(4).ArtStyle :=

wdArtChristmasTree; WordDocument1.Sections.First.Borders.Item(3).ArtStyle :=

wdArtChristmasTree; WordDocument1.Sections.First.Borders.Item(2).ArtStyle :=

wdArtChristmasTree; WordDocument1.Sections.First.Borders.Item(1).ArtStyle :=

wdArtChristmasTree; �

"����������������� ����������������� ����������������� ������������K���������

������ ��� ���� ���� ��� ����� ��������� ������ � ��� ������� � � %� �� �� � E ����� �����

����������#����+������������� ��������� ��� ���������� ����

#����+�������������3������������������������������� ���� ���� ��������&�������

���������������������� ������� ����������������������%������

� WD.Paragraphs.Item(6).Borders.Enable := 0; //Quitar/ Poner todos los

bordes a un párrafo WD.Paragraphs.Last.Borders.Item(2).Visible := True; //Poner 1 borde �

K�����������1��������2������������%��� �����������1F����2�� �$��'���������

����������������������� �������A&��� ��;M�CB��������� ��A&��� ��;M�*B�������%�W�

�������%�������������K��������� ����� ������������ �����������;�

��� � �� ���

C� $����

)� 8��������

D� $�����

I� �������

��� ���������������������������������������������

��� ��������������������������� ��������� �������������������A1$�����2B��� �

�� �� ��� ��� '����� A1"� �8����2B�� ���� �� ��� ��� '����� A1K���=�! �2B� !� ��� ������

A1K�������2B��$������������%����� ������&���������� ����� �����������)D��������������

������ ��� ������ �������& � ����������� ��������� �����%����,-�A� ������.� �� �

��������-���B;�

������� �����

��K���=�! �7���� =��������

��K���=�! �=��� �� F�������� 4����� '����

��K���=�! ����� F�������������

��K���=�! ����� �� ��� �������

��K���=�! �#���#���6K���P��� ��� �������� '���������R�����

��K���=�! �&�����D�� &������D�������������

��K���=�! �&���%�D�� &������D��������������

��K���=�! �=��� ��%!� @��� ����

&������������ ����'������������������������������������������ ��������

&���������!������������������� ����� �1=������2��&�������������������������� ��

���������� � �������"�������������� ���������������� ��� ��� �����������A1=��������

Page 16: Word Desde Delphi

F��6�����������"� �8����2B�� � � �� �� ��� ����� � ���� A1=��������

O�������������"� �8����2B�!������+�� ������������ ��� ��A1=�������#�����2B��

������� ������

��#�����$���� �����������A��#�����=� ��B�

��#�����=� ��� & ��� ����������� ���� ������������

��#�����0������� � ?�!����������� ���������

��#�����>����� � ?�!���%����� ���������

��#�����"���� $������������������������� �

��#������������ "���� $��������������������� ���

��#������������ ����� ������� ���������������������

��#������������ .�� ������� ��������������������

��#�����(*������� (*Q��� ��� ����������� ����

$���������!�� ������%��������������������+������K����!�����������!�������� ������

������� ����� �������� �3���+��� ��� 1��62� � � ������ �����+�� ��� 1#�����2�� .��

����� ��;�1��#�������6>����� 2��#����+���������������������������������������

�� � ����������K���%� ����%�'������)�(����)�(���������� ��� ��������������������;�

1��#�����)��(������2�� 1��#�����(������2�� 1��#�����-��(������2��

1��#�����C*������2��������������� ����������,-�(�A1��#�����,-��(������2B��

& �E �������� �������������� ����������������&�������������������������������������

���� ��� ������ �3���������� �������� #����+�� �������� �� ��� ������� !� ������� �����

������������ ����1$��������2��!������� ����������������������;�

������� ���������

��$��������7���� 7��������#��������� ��

��$���������������?��$���� .����� �������!��������������%���� �������� �

������

��$���������������F ��6$���� "����� ���������������������

��$��������K��>����K������ K��������������� ���� �������� �������

��$��������F ��6���F��6������ $ ����� ����� �������������!���������� ����

��$��������=������ 0��������� ����������%���������

��$��������=��6 �#���� =����������������� ���� ����� �������

#����� � ��� ��� ���%��� �� ��� ���������� !� ��� �������� &���� ������'������ � �� ���

�������� ������� ����������� ������� ����������������� ������������������������

�?#O�

������������ ���������������� '������������� �����������������1=����)2��1=����C2�

!� 1=����C(2������������������ 1��������2��"���+������ � � ������������ � � ��� ��� � �

���� ���C�(�%������ ����� ��

#����� ����������!� ������� �����������������%�����������������������%������������

��������� ��������� ����� ������������������!������+�����������������������������

������� ����%����������� ����� ����������!����� ���������!���������������#��������

���� �������%� ��� ����� %��� ���� ��� ������ �� �� ��� ��� ������� ���+� � � ����������

����������

Page 17: Word Desde Delphi

#�� ����

K������ ������ ���������������������������������������!���.�����!����������

������ ������ �����������������������+��������� ��������&���������� ���������!�������� ���

!������ �������<���������������������� ����� �����#����� �����!�������������������

������������;������������� ��������%����������������� ���� ���������� ������ ������

������ �������������� ���������� ���� ����

"���������� ���������� �;�

�WordDocument1.Tables.Add (WordDocument1.Paragraphs.Last.Range,3,4); ������� �� ��� �� ����� � � ����� ����� ��!� "# � ������� ��������������� � !�

��� ������� -�������� �������$������ � ��#���&#����� ����$ ������$��� ����/���

� �#���� ����$��$0�����$ ��-���� ���#� �� � ���� ���# �����#��� ����� �����&#�� �#���

$��� �&#�� � ����� � � � � ����� +� !� � � ����� �'-�1���� �# � � ( �� � � ����� ������� � � ���

������$��� �� ��� �$ ����$#����� ������� -������+��&#����� �2�� �$��� ����-�3� �� �

�&#��������$��� ���� �$#���������!�

�#������������������;� & � ����� ��� � � ����%�� �� ����� �� ��� ����E������� �� ��� !�

�E��������� �������=����!�� �E������������ ������������������������ ��������� ��

��� ���

�WordDocument1.Tables.Add (WordDocument1.Paragraphs.Last.Range,3,4); WordDocument1.Tables.Item(1).Rows.Item(1).Range.Bold := 1; WordDocument1.Tables.Item(1).Columns.Item(2).Cells.Item(3).Range.Text :=

‘Texto’; �

$��'������������ ������������ ���!��������� �������$����������� ����� �������� ��

�������� ��!� ���� �������� �������E ��������������� ���� ������������ ����������

�� �������� ����������� ���!� ���������� ��������@�������������������� ����� ������;�

�WD.Tables.Item(1).Cell(1,1).Range.Text := 'Texto';

������� ��������� ����������������������������18���2�A��� ���������������������!�

��� ��B���������������������� ������������ �����������1"����2�;�1����������"����2��

1#�� ���"����2��

Page 18: Word Desde Delphi

K������ ���������������1#�� ���8���A�B��� ���2������ ����� ���!��� ������������� ��

���������������� ����� ����������������������������A1=����"� �2B��������������+�

���������� ����������� �����!��������������������������������� ����� ���

������� ������

���� ���"� �=����K���� K����� ������� ���������� ��������� ������� �����������

���� ���"� �=����.�� K����� ������� ���������� ������������ ����������������

���� ���"� �&����?��� =����������� ���� ��

���� ���"� �&����"� ���� =����������� ���� �����

���� �������� ���� �� ����� ��� �� �������� ���� ��� � ���� !� ��� �� ������� ������ � ����

����������

�WordDocument1.Tables.Add (WordDocument1.Paragraphs.Last.Range,4,4); WordDocument1.Tables.Item(1).Columns.Item(2).Cells.Item(2).Merge (WordDocument1.Tables.Item(1).Columns.Item(3).Cells.Item(3) ); �

$��'��������������� �����I�I��!� ����� ��������� ������������������������� ���

��������� ����� ��+�����1=� ��2��� �������� ���������� ��E����������%����� ���!�

�� ��������������������

K������ ��������+������������������������������������ �����������1F����2�����

%�������� ������� ��$�������

& � � ��� !� ������ ��� ��� �� ����� �� ��� !� �� ������ ��� ����� ��� ���� ��� �����������

10�����2�!�1����2��=����������� ������������ ��������������� ������������ ���� ����������

����� ������������� ���������� ����� �����

�WD.Tables.Item(1).Rows.Height := 61; //Todas las filas WD.Tables.Item(1).Columns.Item (1).Width := 61; //Una columna �

=�� ��� �� ��� ��� %� �� ������%������� �����3��� ��� �� �������� =�� ���������

���������������3������������������ ���������������� ������ ���������=�� ��������������

������������ ��������������� ��������������� ����� ������ ������������������������

������ ������������������ ���������� ������������� �� ������������������������������

'����������������������� ����#����� ����� ����������� �����������������������

F������� ������

.���������'�������������������������� ��������������������!����������� ������ ��

����������� ��������� �������� ��������������������� ����� �������������������

�������� ��� ��������� ��� �� ����� � �%�� ���� ������'��� ���� ��� � � ���������� ����

������������

���������������� ����� ��������O������������������������������?������=�� ����������

����������������E��������!������������� ���������������&��������������� ���������

����������������� ��� ������������������K��������������������������������;�

!��� �� ���� �������

#���� & �����������������������

Page 19: Word Desde Delphi

O���� & ������������������������������� �������

O����� 8������ �� ������ �� ��� �� �E�������� #��� X� 0����� ������� O� ��� X�

0����������

�����"���� ��������������������� ������� �����!E��� ���!� ������E��� ���

��������������� ��������������������� ������� ����� ����������������!� ����������

������� ���� ����������������� ��������� ���������%����������������� ��

������ ������ .�� ���� ��� ���������� ��� �E������� A& � ��������� YZ[� !� � �

�����������Y\[�B�

O����� �������������������������� ���

��� 8������ ������������������� ����� ����� ��� ������������>����� ��

?�� �������� &������ ��>��������

K����������������������������� ���%� ��������E�� ���������������������������

������������������������������� ����� ����� ��� ����������;�

������� �������

��O���$�6� �������������������������

��O���"�������� "��������

�O���=���� ����