1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
unit uMain; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs ,ComObj, StdCtrls; type TfrmMain = class(TForm) btnWordConnect: TButton; btnDocCreate: TButton; btnTableCreate: TButton; btnRowAdd: TButton; btnRowDel: TButton; btnTableDel: TButton; btnWordDisconnect: TButton; edCell1: TEdit; edCell2: TEdit; edCell3: TEdit; edCell4: TEdit; edCell5: TEdit; edIndexRowDel: TEdit; Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; Label5: TLabel; edIndexRowAdd: TEdit; Label6: TLabel; procedure btnWordConnectClick(Sender: TObject); procedure btnDocCreateClick(Sender: TObject); procedure btnTableCreateClick(Sender: TObject); procedure btnRowAddClick(Sender: TObject); procedure btnRowDelClick(Sender: TObject); procedure btnTableDelClick(Sender: TObject); procedure btnWordDisconnectClick(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } //Ссылка на Word.Application. wdApp : Variant; //Ссылка на документ Word. wdDoc : Variant; //Ссылка на таблицу Word. wdTable : Variant; //Ссылка на строку в таблице. wdRow : Variant; procedure WordDisconnect(); end; var frmMain: TfrmMain; implementation {$R *.dfm} //Запускаем приложение MS Word, получаем на него ссылку. //И делаем видимым главное окно MS Word. function WordConnect() : Variant; begin TVarData(Result).VDispatch := nil; Result := CreateOleObject('Word.Application'); Result.Visible := True; end; //Создает документ. function DocCreate(aWdApp : Variant) : Variant; begin TVarData(Result).VDispatch := nil; if TVarData(aWdApp).VDispatch = nil then begin Exit; end; Result := aWdApp.Documents.Add(); end; //Создаёт таблицу. function TableCreate(aWdDoc : Variant; aRowCount : Longword; aColCount : Longword) : Variant; var wdRange : Variant; EndIndex : Longword; begin TVarData(Result).VDispatch := nil; if TVarData(aWdDoc).VDispatch = nil then begin Exit; end; //Диапазон документа. wdRange := aWdDoc.Range; //Позиция, расположенная после последнего символа в документе. EndIndex := wdRange.Characters.Count - 1; //Диапазон, представляющий позицию в конце документа. wdRange := aWdDoc.Range(EndIndex, EndIndex); //Добавляем таблицу в конец документа. Result := aWdDoc.Tables.Add(wdRange, aRowCount, aColCount); end; //Удаляет таблицу. function TableDel(var aWdTable : Variant) : Boolean; begin Result := False; if TVarData(aWdTable).VDispatch = nil then Exit ; aWdTable.Delete; TVarData(aWdTable).VDispatch := nil; Result := True; end; //Добавляет новую строку в таблицу. //Если номер строки задан отрицательным числом, то считаем, что вставку //нужно сделать в конец таблицы. function TableRowAdd(aWdTable : Variant; aRowNum : Integer = -1) : Variant; var RowCount : Longword; RowNum : Longword; wdRowBefore : Variant; begin TVarData(Result).VDispatch := nil; if TVarData(aWdTable).VDispatch = nil then begin Exit; end; //Число строк в таблице. RowCount := aWdTable.Rows.Count; if aRowNum < 0 then begin //Подразумевается вставка в конец таблицы. RowNum := RowCount + 1; end else if aRowNum = 0 then begin //Строки нумеруются с единицы. Поэтому исправляем номер на 1. RowNum := 1; end else begin //Принимаем номер как есть. RowNum := aRowNum; end; if RowNum < RowCount + 1 then begin //Для того, чтобы добавить новую строку в позицию RowNum, //требуется произвести вставку перед уже имеющейся строкой с номером RowNum. wdRowBefore := aWdTable.Rows.Item(RowNum); Result := aWdTable.Rows.Add(wdRowBefore); end else begin //В случае, когда строка добавляется в конец таблицы. Result := aWdTable.Rows.Add(); end; end; //Удаляет строку из таблицы. Если номер строки задан отрицательным числом, //то считаем, что требуется удалить последнюю строку в таблице. function TableRowDel(aWdTable : Variant; aRowNum : Integer = -1) : Boolean; var RowCount : Longword; RowNum : Longword; begin Result := False; if TVarData(aWdTable).VDispatch = nil then begin Exit; end; //Число строк в таблице. RowCount := aWdTable.Rows.Count; //Если таблица состоит из одной строки, то удаление строки считаем невозможным. if RowCount = 1 then begin Exit; end; if aRowNum = 0 then begin //Строки нумеруются с единицы, поэтому делаем поправку. RowNum := 1; end else if (aRowNum < 0) or (aRowNum > RowCount) then begin //В этом случае полагаем, что требуется удалить последнюю строку. RowNum := RowCount; end else begin //Принимаем номер как есть. RowNum := aRowNum; end; aWdTable.Rows.Item(RowNum).Delete; Result := True; end; //Записывает значение в ячейку. procedure TableCellWrite(aWdTable : Variant; aRowIndex : Longword; aColIndex : Longword; aStrValue : String); begin if TVarData(awdTable).VDispatch = nil then begin Exit; end; aWdTable.Cell(aRowIndex, aColIndex).Range.Text := aStrValue; end; //Читает значение из ячейки. function TableCellRead(aWdTable : Variant; aRowIndex : Longword; aColIndex : Longword) : String; begin Result := ''; if TVarData(awdTable).VDispatch = nil then begin Exit; end; Result := aWdTable.Cell(aRowIndex, aColIndex).Range.Text; end; //****************************************************************************** procedure TfrmMain.btnWordConnectClick(Sender: TObject); begin wdApp := WordConnect(); end; procedure TfrmMain.btnDocCreateClick(Sender: TObject); begin wdDoc := DocCreate(wdApp); end; procedure TfrmMain.btnTableCreateClick(Sender: TObject); begin wdTable := TableCreate(wdDoc, 1, 5); end; procedure TfrmMain.btnRowAddClick(Sender: TObject); begin //Добавляем строку. if edIndexRowAdd.Text <> '' then begin //Добавляем строку в указанную позицию. wdRow := TableRowAdd(wdTable, StrToInt(edIndexRowAdd.Text)); end else begin //Добавляем строку в конец таблицы. wdRow := TableRowAdd(wdTable); end; //Заполняем строку значениями. TableCellWrite(wdTable, wdRow.Index, 1, edCell1.Text); TableCellWrite(wdTable, wdRow.Index, 2, edCell2.Text); TableCellWrite(wdTable, wdRow.Index, 3, edCell3.Text); TableCellWrite(wdTable, wdRow.Index, 4, edCell4.Text); TableCellWrite(wdTable, wdRow.Index, 5, edCell5.Text); end; procedure TfrmMain.btnRowDelClick(Sender: TObject); begin //Удаялем строку. if edIndexRowDel.Text <> '' then begin //Удаляем строку с указанным номером. wdRow := TableRowDel(wdTable, StrToInt(edIndexRowDel.Text)); end else begin //Удаляем нижнюю строку в таблице. wdRow := TableRowDel(wdTable); end; end; procedure TfrmMain.btnTableDelClick(Sender: TObject); begin TableDel(wdTable); end; procedure TfrmMain.btnWordDisconnectClick(Sender: TObject); begin WordDisconnect(); end; procedure TfrmMain.FormCreate(Sender: TObject); begin WordDisconnect(); end; procedure TfrmMain.WordDisconnect; begin //Обнуление ссылок. TVarData(wdRow).VDispatch := nil; TVarData(wdTable).VDispatch := nil; TVarData(wdDoc).VDispatch := nil; TVarData(wdApp).VDispatch := nil; end; end. |
|
|
|
Пожалуйста, выделяйте текст программы тегом [сode=pas] … [/сode]. Для этого используйте кнопку [code=pas] в форме ответа или комбобокс, если нужно вставить код на языке, отличном от Дельфи/Паскаля.
Следующие вопросы задаются очень часто, подробно разобраны в FAQ и, поэтому, будут безжалостно удаляться:
1. Преобразовать переменную типа String в тип PChar (PAnsiChar)
2. Как «свернуть» программу в трей.
3. Как «скрыться» от Ctrl + Alt + Del (заблокировать их и т.п.)
4. Как прочитать список файлов, поддиректорий в директории?
5. Как запустить программу/файл?
… (продолжение следует) …
Вопросы, подробно описанные во встроенной справочной системе Delphi, не несут полезной тематической нагрузки, поэтому будут удаляться.
Запрещается создавать темы с просьбой выполнить какую-то работу за автора темы. Форум является средством общения и общего поиска решения. Вашу работу за Вас никто выполнять не будет.
Внимание
Попытки открытия обсуждений реализации вредоносного ПО, включая различные интерпретации спам-ботов, наказывается предупреждением на 30 дней.
Повторная попытка — 60 дней. Последующие попытки бан.
Мат в разделе — бан на три месяца…
Удаление строк шаблона Word
, сабж
- Подписаться на тему
- Сообщить другу
- Скачать/распечатать тему
|
|
Junior Рейтинг (т): 0 |
Творю программу для подготовки отчетов в *.doc-формат. Вот кусок кода: procedure TForm1.Button1Click(Sender: TObject); begin … … if CreateWord then begin VisibleWord(true); If OpenDoc(ExtractFileDir(application.ExeName)+’1.doc’) then begin StartOfDoc; FindAndPasteTextDoc(‘###a1!’,a1.text); StartOfDoc; FindAndPasteTextDoc(‘###a2!’,a2.text); StartOfDoc; FindAndPasteTextDoc(‘###a3!’,a1.text); Шаблон документа выглядит так: ###a1! ###a2! ###a3! Как, например, удалить строку в шаблоне ###a2!, если a2.text не трогаю (под «контролем» чекбокса), чтобы не было текста «###2!» между ###1! и ###a3! Сейчас так: текст из а1 ###a2! текст из а3 Хочу так: текст из а1 текст из а3 Сообщение отредактировано: slaventos — 30.11.08, 12:23 |
ViktorXP |
|
1) вставлять пустоту ###a1!{if ###a2!} ###a2! {/if}###a3! а уже при парсировании смотриш если нашол слово {if значит между им и закрывающей скобкой должна быть обявлена переменная. опань и вправду есть… значит проверяем по масиву… и тут она есть а че в ней лежит… в ней какойто текст, значит смотрим что между закрывающей скобкои и {/if} включаем эту часть тоже в парсирование иначе выключаем этот кусок. |
Игорь Акопян |
|
а ещё в ворде есть Bookmarks или Закладки. Не надо никаких поисков и парсеров Просто значению Закладки присваиваем нужный текст, исключается блок просто — весь блок в шаблоне выделяется и назначается закладка типа «Блок1_есть» и если по условию его не должно быть — присваивается пустая строка. Минусы — если надо вставлять какой-то одинаковый текст — там, наверное, удобнее заменить несколько спец символов на нужную последовательность |
vecs |
|
Для Word-отчетов можно использовать ArWordReport. В шаблоне пишем: В коде: //Указываем, где брать шаблон WordReport1.WordReport1.Filename := ‘c:мой_шаблон.doc’; //На вход отчета передаем три параметра WordReport1.CustTagValues[‘a1’] := EditA1.Text; WordReport1.CustTagValues[‘a2’] := EditA2.Text; WordReport1.CustTagValues[‘a3’] := EditA3.Text; WordReport1.CustTagValues[‘checked_a2’] := IntToStr(Ord(CheckBoxA2.Checked)); //Запускаем отчет try WordReport1.Preview; finally WordReport1.CloseWord; end; |
slaventos |
|
Junior Рейтинг (т): 0 |
Всем спасибо, но сделал по другому Была функция Function FindAndPasteTextDoc(findtext_,pastetext_:string):boolean; begin FindAndPasteTextDoc:=true; try W.Selection.Find.Forward:=true; W.Selection.Find.Text:= findtext_; if W.Selection.Find.Execute then begin W.Selection.Delete; W.Selection.InsertAfter (pastetext_); end else FindAndPasteTextDoc:=false; except FindAndPasteTextDoc:=false; end; End; Добавил новую Function FindAndPasteTextDoc2(findtext_,pastetext_:string):boolean; begin FindAndPasteTextDoc2:=true; try W.Selection.Find.Forward:=true; W.Selection.Find.Text:= findtext_; if W.Selection.Find.Execute then begin W.Selection.Delete; W.Selection.Delete; end else FindAndPasteTextDoc2:=false; except FindAndPasteTextDoc2:=false; end; End; И по условию выполняю FindAndPasteTextDoc и FindAndPasteTextDoc2. |
0 пользователей читают эту тему (0 гостей и 0 скрытых пользователей)
0 пользователей:
- Предыдущая тема
- Delphi: Общие вопросы
- Следующая тема
[ Script execution time: 0,0300 ] [ 16 queries used ] [ Generated: 14.04.23, 20:40 GMT ]
���������������� �� Delphi. Word
�� ��� � ��������� ������, ���������� ��� ������, �����ݣ���� ������ � Word �� Delphi…
«Delphi 4: ������������� ���������� MS® Office® ��� ������������ ������� �����������»
-
����������:
- ����� 1: ������ � MS Excel.
- ����� 1: ��������, ����������� � �������� ���������� Excel.
- ����� 2: ������ ������� — �������.
- ����� 3: �������� ��� �������� �����.
- ����� 4: ������ � ������� � ��������.
- ����� 5: �������� ������ ������� ����.
- ����� 6: �������� ������ ��������� ����� ������ � DDE.
- ����� 7: ������ ������ ������� � Excel ��������� VCL � OLE.
- ����� 2: ������ � MS Word.
- ����� 1: ���������� Word-�� ����� OLE.
- ����� 2: ������� ���������� �������� ������, ������ � ������������ � ����������.
- ����� 3: �������� ��������� ��������� VCL.
- ����� 4: ������ � ���������.
- ����� 5: ������ � �������, ��������� � ��������.
����� 2. ������ � MS Word.
����� 1. ���������� Word-�� ����� OLE.
����� �� ���������� ������ ����, ��� ��������� ��������� Word-� (Excel — ����������) �� �������� �� Delphi.
�) ��� ���� ��� ����� ?
������ ����� ���� ����� ������, � ����� ������ ��� ������������� ������������ Word-� � ����� ���������, �-�: �������� ������ �� ����������; ������ ������, �������; ������� ������� � Word ��� ����������� �������� �� ��� � �.�.
�) ���������������� ������.
�� ����� ���� ���������� ��������� �������� ������� ���, �� ���������� ������ ���� (������ ����� Delphi 5, � Delphi 5 ��� ����� ���� ���������� �� �������� Servers ������������ � ��������� ���� �� ��������������� ����������, ������ ��� ��).
��� ������ ������ ����� ������ File, New Application; File, Save All. �������� ��������� ����� ��� ������� � �������� Unit1 ��� Main, � Project1 ��� WordWriter.
����� ��� ������ � Word-�� ��� ����������� ���������� ����� Word-�, ��� �������� ���:
Project, Import Type Library, Add, ����� ��������� � �����, ��� ����� Word ( � ���� ��� — «c:program filesmicrosoft office»), ������� � ����� Office � �������� ���� — msword8.olb (����� -? ������ Word-� — � ��� ����� ���������� ) ��� excel8.olb (��� Excel). �������� �k. Delphi ������� 2 ����� — Word_tlb.pas � Office_tlb.pas, �� ���� �������� � ������ uses ������ Main ������ �������:
uses ... , Office_Tlb, word_tlb;
�) ������ �������� ��������������� �����������������.
� ������� var ������ ��������� ����������:
// ����� ���������� ����� WordApp:Word_tlb.Application_; // ����� ����-�� ���� ���������, // �.�. �������� - �������� ������ � ... ��, � ��������� ���������� // � ��� ����� � ��������� � ����� �������� ��� � 1 �������� ARange,TempRange:Range; // ������ ���������� Docs:documents; // 1 �������� Doc:document; // ������ ���������� pars:Paragraphs; // 1 �������� par:Paragraph; // ��������� ��� �������� Template,temp,OpenAsTemplate:olevariant; // ������ ������ tabls:Tables; // 1 ������� tabl:Table; // ������� ���������� i:integer;
����� ����������� �����:
1. �������� ����� ����� ����� ������ — button1 ���� TButton, �������� ��������� (��-�� Caption) �� ‘�����’;
2. ��� ������� ��������� ������ — panel1 ���� TPanel. ������ ������ �������� ��������� — bevel1 ���� TBevel, �������� ��-�� Align �� alClient (��� ���� ����� ���������� �� ��� ������);
3. ������ ������ (����� ��� ���������� ����� ����������� ������ ���� ������) ��������� ����� — label1 ���� TLabel, �������� �������� ��-�� Caption �� ‘�������� � ������’;
4. ���� ����� �������� ����� — label1 ���� TLabel, ��-�� Caption �������� �� ‘X=’;
5. ������ ����� �������� ��������� Edit1 ���� TEdit, ��-�� Text ������ �� ‘1’;
6. �� ������ ������� Edit1 �������� ��������� UpDown1 ���� TUpDown, � ������ ��-�� ‘Associate’ �������� Edit1, ��-�� ‘Position’ ������������ ‘1’;
7. ���� ��������� ������ � ��������� ���� 4-6, ������� Edit1 �� Edit2, UpDown1 �� UpDown2, Label1 �� Label2 ��������������;
8. ���� ��������� ��� ���� ����� — label4 ���� TLabel, ������ ��-�� ‘Caption’ �� ‘����� �������� ������:’;
9. ���� ��������� ��������� Edit3 ���� TEdit, ��-�� Text ������ �� ‘0’;
10. �, �������, � ����� ���� ������ ��������� ������ BitBtn1 ���� TBitBtn, ������ ��-�� ‘Kind’ �� ‘bkOk’.
������ ������� ����������� — ������ � ��� � ����������� ��� ���������������� ���������:
1. �������� ���������� OnClick ���������� Button1 :
procedure TForm1.Button1Click(Sender: TObject); begin // ���� ��������� '�����', �� ��������� ��������� if button1.caption='�����' then begin Application.Terminate; exit end // ����� (��� ������ �������, ����� � ��� ��������� '�����') //��������������� ��������� � '�����' else button1.caption:='�����'; panel1.Visible:=true; // ������� ��������� ����� WordApp:=CoApplication_.Create; // ������ ��� ������� WordApp.Visible:=true; // ������ template:='Normal'; // ������� ������ OpenAsTemplate:=false; // ���-�� ���� ��������� with, ����� ���� � �������� ���������� Docs:=WordApp.Documents; // ��������� �������� Doc:=Docs.Add(template,OpenAsTemplate); // �������� ��� ARange:=Doc.Range(EmptyParam,EmptyParam); // ������ ���������� pars:=doc.Paragraphs; // ���������� - �������� template:=arange; // ����� �������� par:=pars.Add(template); // ���� �������� par.Range.Font.ColorIndex:=11; // ��������� ����� par.Range.InsertBefore('������ !!!'); // ���������� - �������� template:=par.Range; // ����� ��������, ����� ������� �� ������� ����� par:=pars.Add(template); // ���� ������ par.Range.Font.ColorIndex:=0; // ��������� ����� par.Range.InsertBefore('�������������� � ���������, ����� ���������� ������ ����� ����� !'); // ���������� - �������� template:=par.Range; // ����� ��������, ����� ������� �� ������� ����� par:=pars.Add(template); // �������� �������� arange:=par.Range; // ����� - ������ ARange.Font.Bold:=1; // ����� - ���������� ARange.Font.Italic:=1; // �������� ������ ������ tabls:=aRange.Tables; // ��������� ����� ������� �������� 5 �� 5 tabl:=tabls.Add(arange,5,5); // � ����� for i:=1 to 5 do // ������ �������� ����� tabl.Cell(i,1).Range.Text:=inttostr(i); end;
2. ������� ���������� ����� :
procedure TForm1.FormDestroy(Sender: TObject); var // ��� ���������� SaveChanges:olevariant; begin // ���� Word �� ������ if not VarIsEmpty(WordApp) then begin { � ����� ��������� ���������: // ��� ����� � ��� template:='���.doc'; // ���� �� ��������, �� if doc.Saved=false then // ��������� Doc.SaveAs(template, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam); ������, ������ ��� �������, ������� ����� � ��������� 'ctrl'+' ' � �������� ������������ ������ � ��-�� } //��������� �� ��������� SaveChanges:=false; // �� ������� ������� �������� Doc.Close(SaveChanges,EmptyParam,EmptyParam); // � ����� � ���� WordApp.Quit(SaveChanges,EmptyParam,EmptyParam) end; end;
3. �������� ���������� OnClick ���������� Bitbtn1 :
procedure TForm1.BitBtn1Click(Sender: TObject); begin // � ����� ������ ������ ����� ��������, // � ����� � �������� - �������� �������� �� ������ � ���������� tabl.Cell(UpDown2.Position,UpDown1.Position).Range.Text:=Edit3.Text; end;
�) � �����-��, ��� ��� …
����� 2: ������� ���������� �������� ������, ������ � ������������ � ����������.
������� ��������.
� ������ ��������� ��������� ��������������� ������ �������� ���������� ������ *.doc � *.rtf. ����� ������ � ���� ������, ����� �������� ������� ��������� ���� ������ �� ����� ����������, ���� ���� ���� �������� � ���������� ��������� — ����� ������, ���� � �.�. ��������� ������ ��� Word, ������� ���������� � �������� �� � ����� ����� ���� ����, ��� ��� ������ � ������ ����� ��� ���� ����������������. ���������� �� ������� ������� ����� ��� � �� �������, ��� ��� �������� ���������� ������
������� ������������ ���� Word2000.pas � ������� �� Visual Basic for Applications. �� �, �������, ��������� ������ �������������.
����� ���������, ��� � �� ���������������� �����������, ��� ��� � �������� ����������� ������� �� ����� — ��� � ��� �� ����� ����������. ������, �� ��������� ������, ������ �������� �� ����� ���������� WordApplication � WordDocument � ������� Servers. ��� ������ ������������ �������� � ������ ���� �����������.
���������� ���������� Word ������������ ���������� �������� ������, ������� � �������� ������. ��� �������� ���������� ������������ ����� ���������� WordDocument ComputeStatistic(). �� ����� ���� ��������, ���������������, ��� ������ �������, �������������� �� ���� ����������������� ���������. ��������� ������� � ������������ ����� Word2000.pas, �� ����� ������ � /Delphi/Ocx/Servers.
����������������� | ���������� ����������� | ����� |
---|---|---|
$00000000 | wdStatisticWords | ���������� ���� |
$00000001 | wdStatisticLines | ���������� ����� |
$00000002 | wdStatisticPages | ���������� ������� |
$00000003 | wdStatisticCharacters | ����� ��� �������� |
$00000004 | wdStatisticParagraphs | ���������� �������� |
$00000005 | wdStatisticCharactersWithSpaces | ����� � ��������� |
��� ���� ��������, ��� ���� �����. �� � ������ �� �������.
�������� �� ����� ���������� ����������, �����, ��� ������� � ������� � ��� ������ ����. � ������ ������� ������� ������������ � ������� ConnectKind ���������� WordApplication. ��� ����� ��������� ��������� ��������, �� �� ������� ���������� �� ��������� �������� ckRunningOrNew. ��� ��������, ��� ���������� ���������� � ��� ���������� ��������, ��� ��� ���������� ����������� �����. ��� �������, ��� ������ ����������.
������ ����� ������� ��������. �������������� ���� �������� ���������� FileName, ��� ����� ���� OleVariant, ������� �������� ������ � ������ �����.
WordApplication1.Connect; WordApplication1.Documents.Open(FileName, EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam); WordDocument1.ConnectTo(WordApplication1.ActiveDocument);
�������� �������� �� ���������� ����������-«��������». �� ����� ������ ����, ������� ������ ���������� � �������. ��, � ����, �� ������ ������. ����������� ��� ���, ��� «�������» ������� ������������� ��� MS Word 97, � ����� ������ ��� ������ � Word 2000 � Word XP.
������� �����.
������� ������ ���������� ���������� ���� LongInt (� ����� ������� ����� ��� ��� ������������ �� ���������� ���������� � �������� ����� ��������� ������ ������, ��� ������� �������� ������ ����), ����� ��� � ���������� � ��������. ��������, ��������� ����� ����, ������ � ��������� � ��� �������� �������� ������, � ����� ���������� ������� � ���������. ���������� �������� �������������� � «�������» ���������� WCount, SCount, CCount, � PCount.
WCount:=WordDocument1.ComputeStatistics($00000000); CCount:=WordDocument1.ComputeStatistics($00000003); SCount:=WordDocument1.ComputeStatistics($00000005); PCount:=WordDocument1.ComputeStatistics($00000002);
������ ������ �������� � Word’� � ������ ������ �������� ����������, �������� �������, ��� �������� ���������� ����� ���������� ���������� ���������� �� ���������� ������� «��������� ��� ������».
������.
������ � ���������� ����� ����
������� � ��������. �� ���� ���� ������ ������������� ����� ������ ��������, �� �������� — ������ � ����� ���������. ����� ����, ��� ����� ���������� � ���������� — �������������� ��� �������� �������������. ������ � ������� ������ ��� � ������ ��������. � ������������ ��������� ������ Word — Footnotes. ������� ���� ��������� ���������� ����� ������:
ifcount:=WordDocument1.DefaultInterface.Footnotes.Count;
������� ���������� ������ � ������ ������������ ���:
FWCount:=WordDocument1.DefaultInterface.Footnotes.Item(ifoot).Range.ComputeStatistics($00000000);
����� ifoot — ����� �����, «����������» ������. ��� ����, ����� ������ ���� ������ ������, ������� ���:
FWCount:=FWCount+WordDocument1.DefaultInterface.Footnotes.Item(ifoot).Reference.ComputeStatistics($00000000);
��� �� ��������� ��� ������� ���������� ���� � ������ � ������� ifoot � �� ����� — ��� ���������������� ��������� � �������� «������» ����� ���� ����� �����������. ����� �������� ���������� �� ���� �� ������. ��� ���� ������� ������, ��� ����� ���������� ������ ���������� �������� � ���������� �� «�������». �� ����:
for ifoot:=1 to ifcount do begin FWCount:=FWCount+WordDocument1.DefaultInterface.Footnotes.Item(ifoot).Range.ComputeStatistics($00000000); FCCount:=FCCount+WordDocument1.DefaultInterface.Footnotes.Item(ifoot).Range.ComputeStatistics($00000003); FSCount:=FSCount+WordDocument1.DefaultInterface.Footnotes.Item(ifoot).Range.ComputeStatistics($00000005); FCCount:=FCCount+WordDocument1.DefaultInterface.Footnotes.Item(ifoot).Reference.ComputeStatistics($00000003); FSCount:=FSCount+WordDocument1.DefaultInterface.Footnotes.Item(ifoot).Reference.ComputeStatistics($00000005)+1; if WordDocument1.DefaultInterface.Footnotes.Item(ifoot).Reference.Text<>IntToStr(ifoot) then begin FWCount:=FWCount+ WordDocument1.DefaultInterface.Footnotes.Item(ifoot).Reference.ComputeStatistics($00000000); end; end;
����������� ������� ���������� ������, ��� ����� ���������� ������ � ������� �� ��������� � ���, ��� ������ ���������� ���������� Word. ����� ������� ������ � ������� ������ Word ������ ������, ������� ������-�� �� �����������. �������� �������� ����������, ��� ������������� ������ ������ — �� ��������� ��� ���. � ��������� ������ ������� ��������� ���������� ���� � ����������� ������. ����� ����� ���� ���������, ����������� �� ����������� ���������� ����������. ����� ����, ���� � ��� ����
�� 1 — ��� ���������� ��������� ������ � MS Word, �� � ����������� ���� ��������� �������� ����.
������ �������� � �������� �������. ������������ ��� �� �� �����, ������ ������ ����� «Footnotes» ����� «Endnotes». � ��� ������������� �� ������� — ������-�� ��� ������� �������. � � ������ ������ �������� ���: �������� �������� ��� ������ ������, ��������������� �������� ������ � ������� � ����� ���, ��� ������� ����. ���������� ���������:
WordDocument1.SaveAs(FileName, FileFormat),
��� � ������� ����� ��� ��������� ���� OleVariant — ��� ����� � ����������������� ���������, �������� ������ �����. ��������� ���������:
����������������� | ���������� ����������� | ����� |
---|---|---|
$00000000 | wdFormatDocument | �������� Word |
$00000004 | wdFormatDOSText | ������� ����� |
$00000006 | wdFormatRTF | ���� RTF |
������ ������ �������� ������� ����� ����� ��� � ��� �� ����� Word2000.pas. � ��� ���� ���������� ������ — ���� ������ ��������� � ������ ��� ���������, �������� �� �����. ������� �������������� �������� ��� ����������, ��������� �� ��������������� �������� � ������ ����� ���������.
��, � ������, ����������, ����� ��������� � �������. ��������������� �������� ������ � ������� ���������� ���:
WordDocument1.DefaultInterface.Endnotes.Convert;
������ �� ����� ��������, � ������� ���������� ������ ������� ������. � ���� ������� ������� �� ���������, ������, ��� � ���� ��������, ��. ����. ���� ���������� ���������� �������� ������ ����� ������, ������� �������������� ���������� ������� ������, ��������� �� � «��������» ���������� � ������� ��� ��� ����� ���������������. ������� ���� ���������� �������� ������ �� �����������. ������ ���������� ������ � �������� ������, �������� ���������� ��������� � ������ ������ ���, ��� �� ����
��� Word.
�����������.
��� �� �������� ��������� ����������� Microsoft. ��� ���������, Word ���������� �� ���, ��� ���������� � ���������. �� ����������� � ������ �����������. � ���� � ��� ����� ����������� �������� ����� ������, �������� � ��������, ������� � �.�. �����������, Word �� �� ����� ���� �������, �� ��� �� ����������. ��� � ���������, ��� �� ��� ����� ��������� ��� �������.
����������� � ��������� ����� ������� � ��������� ���������� ������ ��� ��������� «�������» — Sections. ������ ������ ����� ����� ������� � ������ �����������. ������ ������ ����� ���������� ���������� �������.
isectct:=WordDocument1.DefaultInterface.Sections.Count;
����� � ��� ����� ���������� isectct, icofct, icohct ���������� �������������� ���������� �������� ��� �������, ���������� ������ � ������� ������������ ������� �������. ���������� isec ������ «�������» �������, ���������� icof, icoh «��������» �������������� ������ � ������� ����������� � �������� ������� �������. ���������� ������������ � ������� ���������� ���:
icofct:=WordDocument1.DefaultInterface.Sections.Item(isec).Footers.Count; icohct:=WordDocument1.DefaultInterface.Sections.Item(isec).Headers.Count;
������ ��� ����� «�������» ����� �� �����������:
CBWCount:= WordDocument1.DefaultInterface.Sections.Item(isec).Footers.Item(icof).Range.ComputeStatistics($00000000);
� ������ ������ �� ��� ������� ��������� ����� ����, ������������ � ������ ����������� ��� ������� icof, ������������� ������� ��� ������� isec. ������ ����� �������� «�������» ���� ��� �������� ���������� ������� � ������ ������������. ��������� ��� ����� ��������� ���:
isectct:=WordDocument1.DefaultInterface.Sections.Count; for isec:=1 to isectct do begin icofct:=WordDocument1.DefaultInterface.Sections.Item(isec).Footers.Count; icohct:=WordDocument1.DefaultInterface.Sections.Item(isec).Headers.Count; for icof:=1 to icofct do begin CBWCount:=CBWCount+ WordDocument1.DefaultInterface.Sections.Item(isec).Footers.Item(icof).Range.ComputeStatistics($00000000); CBCCount:=CBCCount+ WordDocument1.DefaultInterface.Sections.Item(isec).Footers.Item(icof).Range.ComputeStatistics($00000003); CBSCount:=CBSCount+ WordDocument1.DefaultInterface.Sections.Item(isec).Footers.Item(icof).Range.ComputeStatistics($00000005); end; for icoh:=1 to icohct do begin CHWCount:=CHWCount+ WordDocument1.DefaultInterface.Sections.Item(isec).Headers.Item(icoh).Range.ComputeStatistics($00000000); CHCCount:=CHCCount+ WordDocument1.DefaultInterface.Sections.Item(isec).Headers.Item(icoh).Range.ComputeStatistics($00000003); CHSCount:=CHSCount+ WordDocument1.DefaultInterface.Sections.Item(isec).Headers.Item(icoh).Range.ComputeStatistics($00000005); end; end;
� ����������, � ������� �� ������ ���� ����������� ���������� ����������, ����� �������� ���� �������� ��������� ��������� ���������� ����, ������ � ��������� � ������ ��� �������� �� ���� ������������.
����� �������������� ��� ��������� ������ ��������� ������� � ������ ��������� ����� ������������ �������. ��� Word ��������� ������� �� «����������», �� �������� ������� ���������� — ������, �� ��������������� ������������ � ������ ������������. � ��������� ������ — Shapes. ��� ��� ���������� ����� ����������. ��-������, ���, ��� ��������� �� ������ ���������, �������� Shapes. �� ���� � �������� ��� Word’� ��� �����, ��������� �������, ������ WordArt ��� �������������� ������. ������ � ���
��������� �������� ���������, ��� ���� ����� Shape ����� ������������������, �� �����, �� Frame ��� InlineShape. ��� ��� �������� �����������, ��� ���, �������� ��, ��� � �������. �� ��������� Microsoft, �������, ������ ��� �������. ��-������ � ���������� ������������, ��� Shapes ���������� �������� ���� OleVariant. ��� � ��� ������ ������, ������. ���� ������ ����������� ������� ����� �����, ��� ����������� ������� ������� Shape �� Frame �������� ������. � ���� ���������� ����������, �� ����� ���� ����� ����������
�������� �������. ������, ���� �����-�� �������� � ������� � ��������� ���������. ��-������, InlineShape ����� � ����� ����������. ������� ������ ��� ����������� �� ���������, �� � ���������� InlineShapes ��������� ����������� �������. ���������� ���������� ������� ������� ������ �������� ���� ��� RTF � ���������� ��� ���, �� ��� ����� ������� ��������. ���������� �� ��������� ����� � �������, ��� ���-�� � ����� ����������� � ����� ������ ������ � Shapes «�����������» ���������.
��, ��� ����������� � ���. �������� ���, ��� �� ��������, ����� ���������� ��������� ���� ������ ����������. ��� ���� ���������. ����� ��������� Word ����� «��������», ����� �� �� ������ �� ������:
WordApplication1.Visible:=False;
��� �������� ����������, �������� ���� � ��������� ���������� ���-�� ����� �������� ������, ���������, ��� � ���� ������� ���������. ������ ���������� ��������� � ��������� ��������:
WordDocument1.Save; WordDocument1.Close;
�� �, �������, ������ ������� Word Disconnect, ����� �� ������ ��� ��� �� �����.
� ������ �������������� ���, ��� ��������������� ������ �������� � ����� �������������������� ���. ���� ������� ����� ������ � Windows, ������ �� ���� � ��� ������ ������� ��������� ������ �����. ��� ������� �������� �������� ���������� � ���� ����� ������ ����� ��������� «������� �����». �� �� ����� �����������, ���� ����� ������� RTF — ����� � ������� Shapes � ����������� �� � ������� ����� � Word ���������� ����������� «���������» ����. ��� ��� ����� ������������� �������������� ���������
������ ������ ��� ��������� ������� ����������. � ���� ����� WindowsXP, ������� � ����� ����� �������� �������������. ����� ����, ���� ����������� ����� �� ���� ��������, ����� ������ ��������� ����� ��� ����. ��� ��� ������ ��� ����� ����������� ��� ������� ����������� ����� � ���������� �����������.
����� 3. �������� ��������� ��������� VCL.
������� ��������.
������, ��������, ���� ��� ����������� � �������������� ������ ������. � Delphi ������� ��� ����� ����������� ����������, �� ��� �������� �� ��� ���������� ������� ����������� �� ����� ������������� ������. ����� �� ������� ����� ������� ������������� ��������� MS Word. ����� �� ����� �������� ��������� ���������� ������, ��� ������� �������� � �������� � ���� ������ ������ ������, ��� ���� ����������� � ������ �������� �� Delphi, �������� ������ ����� ����������� ��������. � �� ����������
�� ��� ���� �������� ������������� ����� ������ �.�. �������������� «���� SQL � Delphi 5». �� ��� ����� ������� ������ ����� �������������, ��� ������������� ����������� � ������������������� ��������� ����? � ������ ������ � ����������� ������ ����������� ������ � ��������� ��������� Word.
��� ����� ���� ��� ����. ������ — ���� �� ����� ������� ��������� ������ ������, ����� ����������� ������, ���� � ������ ������� ����� ������ ������� ������ ������. � ������ — ������� ����� � ����, ������ � ��������� �������, ��������� ��. ��� ���� �� ����� ���������� �������� ��� ������� ������ � �������, ���������� ��� ������� ������ — ����� ���, ��� �� ������ � ����� Word’e. ���, ��� ����������� — ���������� WordApplication � WordDocument � ������� Servers.
������ ��� �� ������� — ��������� ���� � ����������. �������������� ��������� ���������� FileName, ���� OleVariant, ������� ����������� ������ � ������ �����.
WordApplication1.Connect; WordApplication1.Documents.Open(FileName, EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam); WordDocument1.ConnectTo(WordApplication1.ActiveDocument);
�������� �������� �� ���������� ���������� — «��������». �� ����� �� ��������� � ���, ��� ������ ���������� � �������. ����������� ��� ���, ��� «�������» ������� ������������� ��� MS Word 97, � ����� ������ ��� Word 2000 � Word XP.
�������� ������ ��������� �������� �����:
WordApplication1.Connect; WordApplication1.Documents.Add(EmptyParam, EmptyParam, EmptyParam, EmptyParam); WordDocument1.ConnectTo(WordApplication1.ActiveDocument);
����� ����� ������ �� ���� «��������» ������ — �� ��� �� ����� ��������. ����� ����, ������� ����� ����� �� ��������� �������� ����������, ����� Word �� ������ ����� ����������:
WordApplication1.Options.CheckSpellingAsYouType:=False; WordApplication1.Options.CheckGrammarAsYouType:=False;
�� ��������� ������ ��� ���� ��������� ��� ����������� ��� �����:
WordDocument1.PrintOut; WordDocument1.SaveAs(FileName);
��� ���������� � ������� ���� OleVariant, �� ����������� ������ � ������ �����.
������ Range.
� ��������� ��� ���� ������ ����� ���������� ������ Range, ������� ��� ����������� ��� �������� �������. �� ������������ �� ���� ����� ������, ������� ����� �������� � ���� ��� ���� ����� ���������, ��� � ����� ��� �����. �� ����:
var range1, range2, range3, a, b : OleVariant; ... range1:=WordDocument1.Range; a:=5; b:=15; range2:=WordDocument1.Range(a,b); range3:=WordDocument1.Range(a);
������ ��� ������ �������� � ���� ���� ����� ���������, � ������� �� ���������� ������� 5-� � 15-� ���������, ������ ������������ �� ���� ���� ����������� ����� ���������, ������� � 5-�� �������. ������ ����� ��������� �������� �������, ��������, � ��� ������� ����� �������� ����� � ��������:
range2.InsertAfter('MS Word');
��� �� �������� ����� ����� ����������� Range. ����� ����� ����� �������� ����� � ����� ���, ��� ����� ������ ����� InsertBefore(). �����, ����������� � ������� Range, ����� �������� ���:
WordDocument1.Range(a,b).Text;
����� ����, � ������� Range ����� �������� ����� � �������� �������. ������:
a:=5; b:=15; WordDocument1.Range(a,b).Font.Bold:=1;
���� ����� �������� ��������� ������ �������, ����������� 0. ���������� ����� ������� ����� ��������, ������������ — �������� WordDocument1.Range.Font., � ����� ���� ���������, ����� ����� ���� ��������. ������ Select, Cut, Copy � Paste �������� ��� � ������� ������. � ������� Paste ����� �� ����� ���������� Range �������� �� ������ ������, �� � �������, ����������� � ������ ������.
����� 4. ������ � ���������.
������� ��������.
������ �� ���������, �������� � ��������.
������� � ��������� Word �������� ��������� Tables. �� ���������� ����� ������ ���:
tcount:=WordDocument1.Tables.Count;
� ��������� ������� ���������� �� �� ������:
i:=1; WordDocument1.Tables.Item(i) ...,
��� i — ����� �����. � ������ ������ �� ���������� � ������ �������, � ������ i ����� ��������� �������� �� 1 �� WordDocument1.Tables.Count. ���� ��� ���������� ������� ������� �����, ������� ��������� ���:
WordDocument1.Tables.Add(WordDocument1.Range, i, j, EmptyParam, EmptyParam);
��� ������� — ������������, ��� ����� � ���������, ��� ��� ��� �������� ����� ��������� � ����� ���������� ������ Range. � ������ ������ �������� ������� �� i ����� � j ��������. ���� ��� �c�� �����-�� �����, ������� ���� ���������, ���������� ����������� ������� ����� ������� ������� ������� Range:
a:=5; b:=15; WordDocument1.Tables.Add(WordDocument1.Range(a,b), i, j, EmptyParam, EmptyParam);
���������� a � b ������ ���� ��������� ��� OleVariant.
�� ���, ������ � ��� ���� �������. �������, ����������� ��� ��� � ��������� ��� �� ������� �� ����. ���������, ��� �� �� � ��� ����� �������. ����� �������� � ����� ������ ���:
i:=1; k:=WordDocument1.Tables.Item(i).Columns.Count; j:=WordDocument1.Tables.Item(i).Rows.Count;
����� �� ����� ���������� � ������ �������, �� ����� �������� � ����� — ���� ������ ��������� ������� �� �����. ������ ������� ������ �������� ��� ������ �����:
WordDocument1.Tables.Item(i).Columns.Width:=90; WordDocument1.Tables.Item(i).Rows.Height:=45;
���������� ����� �������� ������� ��������� ����� � ��������:
WordDocument1.Tables.Item(i).Columns.Item(j).Width:=90; WordDocument1.Tables.Item(i).Rows.Item(j).Height:=45;
����� j — ����� ���� ����� �����, ���������� �� 1. ����� ��������� � ��������� ������, ��������� ��� �������� ������������ � ��� �����:
WordDocument1.Tables.Item(i).Cell(j,k).Range.Text;
����� j � k ����� ����������, ���������� �� 1 �� ����� ����� ��� �������� ��������������. �������� ������ �������� ��������� ���������, ������, ��� ��� ��������� � ������ (j,k). ��������� ����������, �� � �������� Word �� ������ ����� ����� ������ ����� ������. ����� ����� ������ ���������� ������� �� ���� �����, ��� ��� ���� ������� �����, ��� � ��� ��������� ������:
WordDocument1.Tables.Item(i).TopPadding:=10; WordDocument1.Tables.Item(i).BottomPadding:=10; WordDocument1.Tables.Item(i).RightPadding:=10; WordDocument1.Tables.Item(i).LeftPadding:=10;
� ������ ������ �� ������ ���������� ������� ��� ���� �������, �� ����������� ������ �������� ���� � � ��������� ������. �������� ������ ������, ������� ��� ������ ����� ��������� �������:
WordDocument1.Tables.Item(i).Cell(j,k).Select; WordDocument1.Tables.Item(i).Columns.Item(j).Select; WordDocument1.Tables.Item(i).Rows.Item(j).Select;
����� ����, ����� ��������� ������� ����� �� �����������. ��� ����� �������� ����� AutoFit:
WordDocument1.Tables.Item(i).Columns.AutoFit;
�������� ������ ��� ������� ����� �� ������������ ����������:
WordDocument1.Tables.Item(i).Columns.Add(EmptyParam); WordDocument1.Tables.Item(i).Rows.Add(EmptyParam);
�� �������� ������ ����� � ������� ������. ������ ������� ������� � ������������ ����� �������:
var i, j: Integer; varcol: OleVariant; ... j:=2; varcol:=WordDocument1.Tables.Item(i).Columns.Item(j); WordDocument1.Tables.Item(i).Columns.Add(varcol);
���������� ���������� ��������� � �� ��������. ������ � ������� ������� ������ ��� �������, ����� �������� ���������� �������. ������ ���� ������� � ������� ������-�� ������, ���� ����� ����������.
������ ��� ����������� �����. �������� ������:
WordDocument1.Tables.Item(i).Cell(j,k).Merge(WordDocument1.Tables.Item(i).Cell(j,k+1));
�� ���������� ��� �������� �� ����������� ������ (j,k) � (j,k+1). ��� ���� ����������, ��� ������� ������ ��� �� ����� ��� «������». ���������� ���� ����������� � ��� ����������� �� ���������. ��� ����� ��� ��, �� � ���������� ����� ����� ����������� ���� �������� �� ��������� ���������� �������� � ��� ������� ��������� ������� ��������� ������.
������ �������� ������.
varrow:=1; varcol:=2; WordDocument1.Tables.Item(i).Cell(j,k).Split(varrow, varcol);
����� �� ������� ������ (j,k) �� ��� �� �����������. ���������� varcol � varrow ���� OleVariant, ��� ���������� �������� � �����, �� ������� ����������� ������ ������. ����� ����� � ���������� ���������� �������, ��� ��� ���� ������ ��������� � ����������� ����� ������������ ������ ����� ������������� �������. � ����� ������� ����� ������� ����������� �������.
������ ��� ������� ������ �� ������� ������ ������� ��� ������ ������:
WordDocument1.Tables.Item(i).Columns.Item(2).Delete; WordDocument1.Tables.Item(i).Rows.Item(3).Delete;
������� ��� �������.
���������� �������, �������, ��������� �� �����. ������ ���������, ��� �� ����� �� �����������. ��� ������� ��� ������� ������������� ����� ������������ ��������. ��������� ��� ����� ���:
WordDocument1.Tables.Item(i).Cell(j,k).Shading.Texture:=wdTexture20Percent;
���������� ���������� ����� ������� �������� � ����� ������� ��� ������:
WordDocument1.Tables.Item(i).Columns.Item(j).Shading.Texture:=wdTexture20Percent; WordDocument1.Tables.Item(i).Rows.Item(j).Shading.Texture:=wdTexture20Percent;
�������� �������� ����������������� ����������, ������ �������� ����� ����� ������������ ����� Word2000.pas. ����� �� ������������ ��� � �����������������, ��� � � ���������� ����. ����� �� ������������ ��������, �������� �������� ����� ���������� � «����������» � ����� ������. ����� ���������, ��� ������� ����� �����-����� ��� � ����� ������. ������� ������������ ������ ���� ��� � �� ������� ����������. ����� ������ ��������� �������� ���������� �������. �� ����� ������������, ����� ��������
��������.
����� �������� ���-������ ������, ����� �������� ����� ������ � ������������ ������. ��� ����� ������������� ���������� ������� Selection:
WordDocument1.Tables.Item(i).Cell(1,2).Select; WordApplication1.Selection.Font.Color:=clRed; WordApplication1.Selection.Font.Italic:=1; WordApplication1.Selection.Font.Size:=16;
� ������ ������� �� ������� ���� ������ � ������ (1,2) �������, �������� ��� �������� � �������� ������ �� 16. ����� ����, ����� ������� ����� ������������, ������������� � �.�.
��� ���� ������ �������� ������� ��� ������� — ������������ �������� ������� Word’a. � ������� ������� ����� AutoFormat, ������� ������ ������� ��� ������� � ������������ � ������ ����������������� �������. � ������������ ����� �� ������ ��������� �������:
procedure AutoFormat(var Format: OleVariant; var ApplyBorders: OleVariant; var ApplyShading: OleVariant; var ApplyFont: OleVariant; var ApplyColor: OleVariant; var ApplyHeadingRows: OleVariant; var ApplyLastRow: OleVariant; var ApplyFirstColumn: OleVariant; var ApplyLastColumn: OleVariant; var AutoFit: OleVariant);
������ �������� ������������ �� ���� ���������� ���������, �������� �����, � ��������� ����������, ����� �� ���������� ������ ����� ����������� ��������� � ��������, ����, ������, �����, ������ ������, ��������� ������, ������� ������� � ���������� �������. ��������� �������� � ������ ���������, ���� �� ��������� ������ ����� �� �� ����������� — ����� ������ ����� ������� AutoFit.
��� �������� ��������, �������� ������ ��� ������ ���������. ��� ��������� �������� «����������». �� ���� ��� ������ ����� ������ ������ ��������� ����� ������, �� � �� ��� �������. ��������� ����� ������ ���� � ����������, ������ �� ������ ����� � ������������ �����. ��� ������� �������� � ����� ������� ����� «���3». ������ ������� ��������� ������ ���������� ����������, ������� ����������� wdTableFormatApplyBorders. �� ���� �� �������� ��� �������� ���:
var tformat, tappbrd: OleVariant; ... tformat:=wdTableFormatWeb3; tappbrd:=wdTableFormatApplyBorders; i:=1; WordDocument1.Tables.Item(i).AutoFormat(tformat, tappbrd, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);
� ��� �� ����� ������� �������� ������.
�����, �������������� ������� � MS Word, ��������, ���� ���������� � ��������� ����������, ������������ � ������� ��������������� ������� ����������������� �����. ��� �� ��� ��� � ����������, ��� �� ��� ��������. ����� ��� ����� ���� ��������� ��� ������ Range, � ������ ��� ��������� � ���� ����� ConvertToTable. � ������������ ����� ��� �������� ���:
function ConvertToTable(var Separator: OleVariant; var NumRows: OleVariant; var NumColumns: OleVariant; var InitialColumnWidth: OleVariant; var Format: OleVariant; var ApplyBorders: OleVariant; var ApplyShading: OleVariant; var ApplyFont: OleVariant; var ApplyColor: OleVariant; var ApplyHeadingRows: OleVariant; var ApplyLastRow: OleVariant; var ApplyFirstColumn: OleVariant; var ApplyLastColumn: OleVariant; var AutoFit: OleVariant; var AutoFitBehavior: OleVariant; var DefaultTableBehavior: OleVariant)
����� ����� ����� ��� �� �� ��������� ���������� �����, ��� � � �����������. ������ � ������� �� ���� ��� ��� ������ �� ��������. ������, ���� � ���� ������ ����������, ��� ��� ��� ����� �� �������� � ����� �� ��������� �������� ��������� ������ ��� «��������» — ����, ����� �����-����, ������ ���� ���������� «���������� ���������». ������ �� �������. ������ �������� � ��� ������ ������, ������� ����� ���������� ������ ����� ������ ����� ������� �������, ������ — ����� ����� �������, ������
— ����� ��������, ����� ���� ������ ��������. ��������� ������ ���������� ������ ����� ������� � ����������� ��� ����������, ����������� �������� �������� ���� � ������� «����������». ��������� ��� ��������� ������ �������� ������� ����� �� �����������, �� �� ����� ���� �� ��������. ��� ��� ������ ������ ��� EmptyParam. �, �������, ������������ ������. �����������, �� ������� ����� �������� � ������� � ���� ������ �����:
WordDocument1.Range.InsertAfter('column1'); WordDocument1.Range.InsertAfter(#9); WordDocument1.Range.InsertAfter('column2'); WordDocument1.Range.InsertAfter(#9); WordDocument1.Range.InsertAfter('column3'); WordDocument1.Range.InsertAfter(#9); WordDocument1.Range.InsertAfter('column4'); WordDocument1.Range.InsertAfter(#13);
����� �������� �������� ������, � � ��� ����� ��������� ��� ������� �� 4 ������� � 3 ������. ������� ������� ���������� ��������� ���������, � ������ — ���������� �� ����� ������. ������ �������� ������ Range — � ������ ������ ��� ���� ����� ���������, � ���������� ��� � �������:
var tsepar, tnumrows, tnumcols, tincolw, tformat, tappbrd, tappshd, tappfnt, tappclr, tapphr, tapplr, tappfc, tapplc: OleVariant; ... tsepar:=wdSeparateByTabs; tnumrows:=3; tnumcols:=4; tincolw:=150; tformat:=wdTableFormatNone; tappbrd:=wdTableFormatApplyBorders; tappshd:=wdTableFormatApplyShading; tappfnt:=wdTableFormatApplyFont; tappclr:=wdTableFormatApplyColor; tapphr:=wdTableFormatApplyHeadingRows; tapplr:=wdTableFormatApplyLastRow; tappfc:=wdTableFormatApplyFirstColumn; tapplc:=wdTableFormatApplyLastColumn; WordDocument1.Range.ConvertToTable(tsepar, tnumrows, tnumcols, tincolw, tformat, tappbrd, tappshd, tappfnt, tappclr, tapphr, tapplr, tappfc, tapplc, EmptyParam, EmptyParam, EmptyParam);
� �����-��, �� ����� �������� ������ ������ ����� ����������, � ��� ��������� �������� �� «��������». ������������, ��� ����� ���������, ��� �������� «�����������». � ������ ������ � �������� ����������� ����� �������������� ������ ���������. ������ �������� �������� � ��������� ������� «����������», �� ��� �������, ������ ������������ ������ �� ��� ���.
�������� ��� ���, ������������, ��� ��� ����� ����������� � �������. ��� ��� ��������� ���������� �������� ������ � HTML.
������ ��������� ��������.
�������� ���� Word2000.pas
�������� | ���������� ����������� | ����������������� |
�������� | ||
��������� ��������� ������� — �������� Word2000.pas � «��������», ������� � ������ ������ «texture», � �������� ������ ������. | ||
wdTextureNone | $00000000 | |
wdTexture2Pt5Percent | $00000019 | |
wdTexture7Pt5Percent | $0000004B | |
wdTexture35Percent | $0000015E | |
wdTexture62Pt5Percent | $00000271 | |
wdTextureSolid | $000003E8 | |
wdTextureDarkHorizontal | $FFFFFFFF | |
wdTextureCross | $FFFFFFF5 | |
������ ������� | ||
��������� ������� �������. ���������� ��� ������������� ����������� � �������������� ������ � �������. | ||
wdTableFormatNone | $00000000 | |
wdTableFormatSimple1 | $00000001 | |
wdTableFormatSimple3 | $00000003 | |
wdTableFormatClassic1 | $00000004 | |
wdTableFormatClassic3 | $00000006 | |
wdTableFormatColorful1 | $00000008 | |
wdTableFormatColumns1 | $0000000B | |
wdTableFormatGrid1 | $00000010 | |
wdTableFormatGrid3 | $00000012 | |
wdTableFormatList1 | $00000018 | |
wdTableFormat3DEffects1 | $00000020 | |
wdTableFormat3DEffects2 | $00000021 | |
wdTableFormat3DEffects3 | $00000022 | |
wdTableFormatContemporary | $00000023 | |
wdTableFormatElegant | $00000024 | |
wdTableFormatProfessional | $00000025 | |
wdTableFormatSubtle1 | $00000026 | |
wdTableFormatWeb3 | $0000002A | |
����� ������� | ||
��������� «���������� ���������» ����� �������. � ������ ��������������, ��� ����� �������� ��������, �������� �� ����� ������, ������, ����� � �.�. � ����������� ��������, ����� ���������, ���������������, ��� ����� ��������� �����-�� ���������� ������. | ||
wdTableFormatApplyBorders | $00000001 | |
wdTableFormatApplyShading | $00000002 | |
wdTableFormatApplyFont | $00000004 | |
wdTableFormatApplyColor | $00000008 | |
wdTableFormatApplyAutoFit | $00000010 | |
wdTableFormatApplyHeadingRows | $00000020 | |
wdTableFormatApplyLastRow | $00000040 | |
wdTableFormatApplyFirstColumn | $00000080 | |
wdTableFormatApplyLastColumn | $00000100 | |
����������� | ||
��������� ������������ ��� ����������� ������ � �������. | ||
wdSeparateByParagraphs | $00000000 | |
wdSeparateByTabs | $00000001 | |
wdSeparateByCommas | $00000002 | |
wdSeparateByDefaultListSeparator | $00000003 |
����� 5. ������ � �������, ��������� � ��������.
������� ��������.
�����.
������� � ����� ������� — ���������� � �������� Word ������ ������ ������. �������� �� ����� ���������� WordDocument, WordApplication � WordParagraphFormat � ������� Servers. ��� ���������� � ������ ������� �������� Range ���������� WordDocument � �������� Selection ���������� WordApplication. �������� ����������, ��� ��� �������� ������� �� ������� Range � Selection. Range ������������ �� ����, ����� ������, ����� ������, ��� ����� ���� ��� ���� ����� ���������, ��� � ����� ��� �����. ���
������� �������� ����� (��� ������) ����������� ���� OleVariant. ��������:
var range1, range2, range3, a, b : OleVariant; ... range1:=WordDocument1.Range; a:=5; b:=15; range2:=WordDocument1.Range(a,b); range3:=WordDocument1.Range(a);
������ ��� ������ �������� � ���� ���� ����� ���������, � ������� �� ���������� ������� 5-� � 15-� ���������, ������ ������������ �� ���� ���� ����������� ����� ���������, ������� � 5-�� �������. ������ ����� ��������� �������� �������, ��������, � ��� ������� ����� �������� ����� � ��������:
range2.InsertAfter('MS Word');
��� �� �������� ����� ����� ����������� Range. ����� ����� ����� �������� ����� � ����� ���, ��� ����� ������ ����� InsertBefore(). �����, ����������� � ������� Range, ����� �������� ���:
WordDocument1.Range(a,b).Text;
����� ����, � ������� Range ����� �������� ����� � �������� �������. ������:
a:=5; b:=15; WordDocument1.Range(a,b).Font.Bold:=1; WordDocument1.Range(a,b).Font.Size:=14; WordDocument1.Range(a,b).Font.Color:=clRed;
���� ����� �������� ��������� ������ �������, ����������� 0. ���������� ����� ������� ����� ��������, ������������ — �������� WordDocument1.Range.Font., � ����� ���� ���������, ����� ����� ���� ��������. ������ Select, Cut, Copy � Paste �������� ��� � ������� ������. � ������� Paste ����� �� ����� ���������� Range �������� �� ������ ������, �� � �������, ����������� � ������ ������.
WordDocument1.Range(a,b).Select; WordDocument1.Range(a,b).Cut; WordDocument1.Range(a,b).Copy; WordDocument1.Range(a,b).Paste;
� ������� Range ����� ����� � ��������� ������ ������. ����� � ������ ���������� ����� «picture». ��������, ��� �� ��� ����� ���� ����� �������� �������.
var a, b, vstart, vend: OleVariant; j, ilengy: Integer; ... ilengy:=Length(WordDocument1.Range.Text); for j:=0 to ilengy-8 do begin a:=j; b:=j+7; if WordDocument1.Range(a,b).Text='picture' then begin vstart:=j; vend:=j+7; end; end; WordDocument1.Range(vstart,vend).Select;
����� ��������� ������� � �������� ������ ����� ������.
������ ��� Selection, �������������� �� ���� ���������� �������� ���������. ���� ��������� ���, ��� ������� ������� ������� � ���������. � ��� ������� ����� �������� ���-���� �� ����� ����������� ���������, ������� ������������, �������� �����. �� ����� ����� ������ InsertAfter() � InsertBefore():
WordApplication1.Selection.InsertAfter("text1"); WordApplication1.Selection.InsertBefore("text2");
�������������� ����������� ������ ���������� ���������� Range, ��������:
WordApplication1.Selection.Font.Bold:=1; WordApplication1.Selection.Font.Size:=16; WordApplication1.Selection.Font.Color:=clGreen;
��� ������������ ����� ��������������� ����������� WordParagraphFormat. ������� ������ ����� «����������» ��� � ����������� ��������� ������:
WordParagraphFormat1.ConnectTo(WordApplication1.Selection.ParagraphFormat); WordParagraphFormat1.Alignment:=wdAlignParagraphCenter;
�������� ��� �������� Alignment ����� ��������� �������� wdAlignParagraphCenter, wdAlignParagraphLeft, wdAlignParagraphRight, ����� ������� ��������. ������� � ������ Cut, Copy � Paste, ������� � ���������� ���� �� ���������:
WordApplication1.Selection.Cut; WordApplication1.Selection.Copy; WordApplication1.Selection.Paste;
������� ��������� � ������� ������ Collapse. ��� ���� ���������� �������, � ����� ������� ��������� ������, ����� �� �� �� ����� ����������� ��������� ��� �����:
var vcol: OleVariant; ... vcol:=wdCollapseStart; WordApplication1.Selection.Collapse(vcol);
��� ���� ��������� ��������, � ������ ������ ������� ����� ���������� ������. ���� ��������� ���������� �������� wdCollapseEnd, �� ������ ������������ �����. ����� ������ ��������� � ������� «��������»:
WordApplication1.Selection.Collapse(EmptyParam);
����� ����������� ��������� ������������ �� ���������, � ������ ����������� ������.
�������.
������� ���� �� ������������, ��� ������� ��������� ����� ������������ �� ���� ���������, ����������� ��������, � ��, ����������� � ���������� ��������, ������ ������ �� �������� — ���������, ������ � �.�. ������ ������ ��������� � WordDocument �� ��������������. ������ ����������� ���������� ������������� � �������� ������������� ������ ����������.
���������� ����� �������� � �������� ������� — �� ���������� �������� �� �� � ������������ — ����������� ��� � Word �� ������ ������. �����������, ������� � ��� ��������� � ���������� DBImage. ������� ����� ������� ��� � ����� ������:
Clipboard.Assign(DBImage1.Picture);
������ ��� ��� ������� ������� ��������������� ������� Paste �������� Range ��� Selection: WordApplication1.Selection.Paste ��� WordDocument1.Range(a,b).Paste. �������� ��� ������� ����������� ���������� ������ ����� � ������� � ������ ����� — ��� ��� ���� ������. ���� �� ������� ������� ������, ��� ����� �������� ��������� — ��� ����� ������� ��������� ������� ������� ���������� ���-�� �������. ����� ����������� ��� ������ ������, ��� �������� �������� �����-���� �������� �����. � ���,
��� ����� � ��������� ������ �����, ��. ����.
� ������ � ��������� ���� ������� ������� �������, ������� ��������� �������� � ���������� � ���� ��� ����������� ���������� ��� �� ���������, �������������� � �������� ������� ����� �������� � �������. ������, ����������, ��� �� — �������� �� ������ ������, �� �� ����� � ��������, � � «�����» — ��������� �������. � ��� ����� ���������� �� ������ �����, �� � ��������, ��� � �������������.
«�����» �������� ��������� Frames, ���������� ����� ��������, ����������� �������� �� 1 �� WordDocument1.Frames.Count. ������� � �������� �����, ������� �� ������ � ������� �������:
Clipboard.Assign(DBImage1.Picture); vstart:=1; vend:=2; WordDocument1.Frames.Add(WordDocument1.Range(vstart,vend)); i:=1; WordDocument1.Frames.Item(i).Height:=DBImage1.Height; WordDocument1.Frames.Item(i).Width:=DBImage1.Width; WordDocument1.Frames.Item(i).Select; WordApplication1.Selection.Paste;
����� ��� �������� ��������������, ��� ������ DBImage ����� ������� ����� ��������, � ����� ��� �� ����� ����� � ��� � ��������� �� ����. �������� �������� ������� �� ��������� ��������. ������ ����� ���� �������� �� ����, ��� ���������� � ��� �������. ����� ��� ����� ����� ������ �� ���������, ��� ������� ���������������� � ���� ��������. ��� ������� �������� ������ ����� ������ ������ ������ �������� ��� �� ���������. ����� ����, �������� Range ��� ���������� ����� ����� ������� ���� ��
������. ����� ���������� ��� ����� �������� � ����� ������� ���� ���������, � ��������� ����� ������ ��� ���� �� ����������. �� ��� ������ � ��� ������, ���� �� �� �������. ���� � ��������� ���� ���������, ����� �������� ������ ����������� ���������. ����� ������� ����� �� �������� � ������ ����� ������ ������-�� ��������� �����.
��� ������� ����� �� ��������� � ��������� � «�������». ��� ����� ������ �������� ��������������� � ������������� ����������������, ������� ������ �� ������ �� ������ �������� «����» ���������:
i:=1; WordDocument1.Frames.Item(i).VerticalPosition:=30; WordDocument1.Frames.Item(i).HorizontalPosition:=50;
������ ����� ������ ����� � ������� �������� ��������� �������:
WordDocument1.Frames.Item(i).HorizontalDistanceFromText:=10; WordDocument1.Frames.Item(i).VerticalDistanceFromText:=10;
� ������ � ���������������. ��� ����� ���������� ����� � ������ ����� �������� �� ���� � �� �� �����. ��������:
WordDocument1.Frames.Item(i).Height:=DBImage1.Height*1.5; WordDocument1.Frames.Item(i).Width:=DBImage1.Width*1.5;
��� ���� ���� �������� � ������� ���� ��������������� ����������. ����� ����� ����� � ���������, �� ������, ��� � �������, ������� �� ���� �����. ����������� ����� � ������ ��-������� � ���� ����� �� ����������. �������� ������ �����-���� ���� ��� �� ������� �������. �� �, �������, �������� �����:
WordDocument1.Frames.Item(i).Delete;
������.
������ � ��������� �������� ��������� Lists, � ���������� ������ ���������� WordDocument1.Lists.Item(i), ��� i ����� ����� �� 1 �� WordDocument1.Lists.Count … �� ���� ���. ��� �������, ����������� �� �� ��� ������� ����� ������, � ���� �������� ����� � ��� �������������. ������ ���������, ��������� ����� ������ ���� � �����:)) ������ �� ��� �� ��������� � ��, � ������. ��� ��� ��� ����������� — �������� Range ���������� ������, �� ���� ��� ����� ��� ���������� �� ������, � ����� �����������
��� ��������:
WordDocument1.Lists.Item(i).Range.Select;
��� ����� � ����� ������ ����������� ���������. �������, ��������� ��� � ����� ��������� �������� ��� �������� � ��������� �����. ��������� ������ ���: �������� � ���� ������/������, � ���������, ���� ��� ��������� ������ ������. � ��� ���������� ������ ������ ��� ������ � ����� ��������. ����� ����������, ��� �� ������ ������ ������� — ������ �����, �������� «Enter», ��������� ����� ������� ������. ������ �� �� �����, ������ ����������. �����������, � ��� ��� ������ �������� � ����������,
� �� ����� ������ � ������ ������ «Item 1» � «Item 2»:
var i: Integer; vcol: OleVariant; ... i:=1; vcol:=wdCollapseEnd; WordDocument1.Lists.Item(i).Range.Select; WordApplication1.Selection.Collapse(vcol); WordApplication1.Selection.InsertAfter('Item 1'); WordDocument1.Lists.Item(i).Range.Select; WordApplication1.Selection.Collapse(vcol); WordApplication1.Selection.InsertAfter(#13); WordDocument1.Lists.Item(i).Range.Select; WordApplication1.Selection.Collapse(vcol); WordApplication1.Selection.InsertAfter('Item 2'); WordDocument1.Lists.Items(i).Range.Select; WordApplication1.Selection.Copy;
�� ���� �� ��������� � �������� ����� ������� ������ ������, �� �������� �� ���� �����. ����� �������� � Word ������ �������� ������, �� ������ ��������� � ��� ����� ��� ������� ��� ������ ����� ������, ���� � ��������� ������ ������. �� � ��� �����, ������ ���������� ���. ��������� ��� ������ �����, ���� ������ ���������� � ��������� ����� — ����� �� ���������� ������ ����������� � ������ ������. ����� ������ � ���, ��� ����� ����� ��������� ������� ������ ������ � �� ���� ���� ��������,
����� ������ �������. ����� ��������� ��������, ��� ������ ���� ������, �������� � ������� Range ������ �����, �������� �� ������ ������ ����� WordDocument1.Range(a,b).Paste. ����� �� ��������� ���� � ����������, ����� ����� ����� �������� ������������� ��� ��� ������ ������, � ����� ������ ����� �� ���� ��� ���������� ���������:
var vsave: OleVariant; ... vsave:=wdDoNotSaveChanges; WordDocument1.Close(vsave);
��������� ���������� ��������� ����� ��������� ��������
����������������� | ���������� ����������� |
---|---|
$FFFFFFFF | wdSaveChanges |
$00000000 | wdDoNotSaveChanges |
$FFFFFFFE | wdPromptToSaveChanges |
������ �������� ��������� ���������, ������ ���� ����������� ����� ��� ���������� ���������. ��������� ��������� �������� ��� ������ ����������� ������ ���������� ���������. ����� ������� � ��������� ��-�������. ���� �� �� ����� ������� ����� ������� ������, �� ����� � ��� ������������ �������� �����:
var i,j: Integer; ... i:=1; j:=1; WordDocument1.Lists.Item(i).ListParagraphs.Item(j).Range.Text:='Item 1';
��� ��� ����� � ������� ��������� ������ ������� ������ ���������� ���������, � ����� �� ���������:
WordDocument1.Lists.Item(i).Range.Select; WordApplication1.Selection.Collapse(vcol); WordApplication1.Selection.InsertAfter(#13); j:=1; WordDocument1.Lists.Item(i).ListParagraphs.Item(j).Range.Text:='Item 1'; j:=2; WordDocument1.Lists.Item(i).ListParagraphs.Item(j).Range.Text:='Item 2';
��� ���� � �������������, ��� � ��� ���� ������� ������ � ��������� ��� ����. �� ���, � �����-��, � ��� ��� �����, ������ � ��������.
�� ���� ��� ���� ������ �� ���� «Delphi 4: ������������� ���������� MS® Office® ��� ������������ ������� �����������» �������. �������� ��� ����-������ �������� ������.