unit AutoDok;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ComObj;
type
TMyForm = class(TForm)
OpenBut: TButton;
OpenDialog1: TOpenDialog;
PrintBut: TButton;
MyPanel: TPanel;
procedure NewOnClick(Sender: TObject);
procedure OpenButClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure PrintButClick(Sender: TObject);
private
procedure Find;
procedure Proverka;
procedure ProverkaWindows;
procedure ExitDok;
{ Private declarations }
public
i,t,t1,ch,ch1,bt,bt1,n,k : integer;
a:String;
W : Variant;
ReadList: TStringList;
EditList: TStringList;
WriteList : TStringList;
{ Public declarations }
end;
var
MyForm: TMyForm;
implementation
{$R *.dfm}
procedure TMyForm.FormCreate(Sender: TObject);
begin
i := 0;
t := 25;
ch := 58;
bt := 16;
t1 := 25;
ch1 := 58;
bt1 := 16;
MyForm.ClientHeight := 58;
MyPanel.Height := 58;
PrintBut.Enabled := False;
ReadList := TStringList.Create;
EditList := TStringList.Create;
WriteList := TStringList.Create;
end;
procedure TMyForm.Find;
var
x: TLabeledEdit;
begin
if W.Selection.Find.Execute('$##') then
begin
n := W.Selection.Start;
end
else
begin
exit;
end;
if W.Selection.Find.Execute('^?##') then
begin
k := W.Selection.End;
i := i + 1;
if odd(i) then
begin
OpenBut.TabOrder := OpenBut.TabOrder + i;
PrintBut.TabOrder := PrintBut.TabOrder + i;
ReadList.Add(W.ActiveDocument.Range(n, k).Text);
x:=TLabeledEdit.create(self);
x.parent:=MyPanel;
x.name := 'edit' + IntToStr(i);
EditList.Add('edit' + IntToStr(i));
x.EditLabel.Caption:= W.ActiveDocument.Range(n+3, k-2).Text;
x.EditLabel.Font.Size := 11;
x.Text := '';
x.left:=10;
x.top:=t;
x.Width:=250;
x.TabOrder:=i-1;
TLabeledEdit(FindComponent('edit' + IntToStr(i))).OnEnter := MyForm.newonclick;
t := t + 50;
bt := bt + 50;
OpenBut.Top := bt;
PrintBut.Top := bt;
MyForm.ClientHeight := ch + t - 30;
MyPanel.Height := ch + t - 30;
end
else
begin
OpenBut.TabOrder := OpenBut.TabOrder + i;
PrintBut.TabOrder := PrintBut.TabOrder + i;
ReadList.Add(W.ActiveDocument.Range(n, k).Text);
x := TLabeledEdit.create(self);
x.parent :=MyPanel;
x.name := 'edit' + IntToStr(i);
EditList.Add('edit' + IntToStr(i));
x.EditLabel.Caption := W.ActiveDocument.Range(n+3, k-2).Text;
x.EditLabel.Font.Size := 11;
x.Text := '';
x.left:=280;
x.top := t1;
x.Width:=250;
x.TabOrder:=i-1;
TLabeledEdit(FindComponent('edit' + IntToStr(i))).OnEnter := MyForm.newonclick;
t1 := t1 + 50;
bt1 := bt1 + 50;
OpenBut.Top := bt1;
PrintBut.Top := bt1;
MyForm.ClientHeight := ch1 + t1 - 30;
MyPanel.Height := ch1 + t1 - 30;
end;
W.Selection.Start := k;
end;
end;
procedure TMyForm.NewOnClick(Sender: TObject);
begin
ProverkaWindows;
W.ActiveDocument.Range(0, 0).Select;
W.Selection.Find.Execute(ReadList.Strings[ActiveControl.TabOrder]);
end;
procedure TMyForm.Proverka;
var
q:integer;
begin
for q := 1 to EditList.Count do
begin
if TLabeledEdit(FindComponent('edit' + IntToStr(q))).Text = '' then
begin
ShowMessage('У вас заполнены не все поля');
Abort;
end;
end;
end;
procedure TMyForm.ProverkaWindows;
begin
if not SetForeGroundWindow(FindWindow(nil,PChar(a + ' - Microsoft Word'))) then
begin
ShowMessage(' Вы по ошибке закрыли приложение Word' +#13 + 'откройте документ заново и повторите поиск');
ExitDok;
Abort;
end;
end;
procedure TMyForm.ExitDok;
var
q:integer;
begin
for q := 0 to EditList.Count-1 do
begin
TLabeledEdit(FindComponent(EditList.Strings[q])).Free;
end;
i := 0;
t := 25;
ch := 58;
bt := 16;
t1 := 25;
ch1 := 58;
bt1 := 16;
OpenBut.TabOrder := 0;
OpenBut.Top := 16;
PrintBut.Top := 16;
MyPanel.Height := 58;
MyForm.Constraints.MaxHeight := MyPanel.Height+34;
MyForm.Constraints.MinHeight := MyPanel.Height+34;
MyForm.Position := poDesktopCenter;
OpenBut.Enabled := True;
PrintBut.Enabled := False;
ReadList.Clear;
EditList.Clear;
WriteList.Clear;
MyForm.Visible := True;
end;
procedure TMyForm.OpenButClick(Sender: TObject);
var
q:integer;
begin
if not OpenDialog1.Execute then
exit
else
begin
MyForm.Visible := false;
W := CreateOleObject('Word.Application');
W.Visible := False;
W.Documents.Add(OpenDialog1.FileName);
a := W.Application.ActiveWindow.Caption;
for q:=1 to W.ActiveDocument.Words.count do
begin
Find;
end;
end;
if OpenBut.TabOrder <> 0 then
begin
OpenBut.Enabled := False;
PrintBut.Enabled := True;
MyForm.Constraints.MaxWidth := 567;
MyForm.Constraints.MinWidth := 567;
if MyPanel.Height <= 560 then
begin
MyForm.Constraints.MaxHeight := MyPanel.Height+34;
MyForm.Constraints.MinHeight := MyPanel.Height+34;
MyForm.Constraints.MaxWidth := 550;
MyForm.Constraints.MinWidth := 550;
end
else
begin
MyForm.Constraints.MaxHeight := 600;
MyForm.Constraints.MinHeight := 600;
end;
SetWindowPos(MyForm.Handle,HWND_TOP,round((screen.Width/2)-(MyForm.Width/2)),0,0,0,SWP_nosize);
MyForm.Visible := True;
SetWindowPos(FindWindow(nil,PChar(a + ' - Microsoft Word')),HWND_TOP,0,(MyForm.Height),0,0,SWP_nosize);
W.Visible := True;
TLabeledEdit(FindComponent('edit1')).SetFocus;
W.ActiveDocument.Range(0, 0).Select;
W.Selection.Find.Execute(ReadList.Strings[0]);
end
else
begin
ShowMessage('Нет полей редактирования');
OpenBut.Enabled := True;
PrintBut.Enabled := False;
MyForm.Visible := True;
W.ActiveDocument.Close;
W.Quit;
W := Unassigned;
end;
end;
procedure TMyForm.PrintButClick(Sender: TObject);
var
q:integer;
begin
ProverkaWindows;
Proverka;
W.ActiveDocument.Range(0, 0).Select;
for q := 0 to EditList.Count-1 do
begin
WriteList.Add(TLabeledEdit(FindComponent(EditList.Strings[q])).Text);
if W.Selection.Find.Execute(ReadList.Strings[q]) then
W.Selection.TypeText(WriteList.Strings[q]);
end;
W.ActiveDocument.PrintOut;
MyForm.Visible := False;
W.ActiveDocument.Close(False);
W.Quit;
W := Unassigned;
ExitDok;
end;
procedure TMyForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if SetForeGroundWindow(FindWindow(nil,PChar(a + ' - Microsoft Word'))) = True then
begin
W.ActiveDocument.Close(False);
W.Quit;
W := Unassigned;
end;
ReadList.Free;
EditList.Free;
WriteList.Free;
end;
end.