20.01.2015 Views

Управление документами Word и Excel на VBA - eDrive

Управление документами Word и Excel на VBA - eDrive

Управление документами Word и Excel на VBA - eDrive

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

5<br />

• ActiveWritingStyle — акт<strong>и</strong>вный ст<strong>и</strong>ль;<br />

• Selection — выделенный объект.<br />

На<strong>и</strong>более общ<strong>и</strong>м сч<strong>и</strong>тается класс Application. К нему обращаются<br />

тогда, когда в программе <strong>на</strong>до <strong>и</strong>спользовать средства <strong>Word</strong>, отсутствующ<strong>и</strong>е<br />

в языке <strong>VBA</strong>. З<strong>на</strong>я с<strong>и</strong>нтакс<strong>и</strong>с обращен<strong>и</strong>я, программ<strong>и</strong>ст может вызывать <strong>и</strong>х <strong>и</strong>з<br />

авторск<strong>и</strong>х процедур <strong>и</strong> функц<strong>и</strong>й.<br />

Класс Application расш<strong>и</strong>ряется классам<strong>и</strong> Window <strong>и</strong> Windows. Методом<br />

NewWindow класса Window создаются новые ок<strong>на</strong>, а методом Arrange класса<br />

Windows он<strong>и</strong> располагаются <strong>на</strong> экране. Каждое конкретное окно<br />

акт<strong>и</strong>в<strong>и</strong>з<strong>и</strong>руется методом Activate, а свойства Caption, DispayRulers,<br />

WindowState, Count определяют его характер<strong>и</strong>ст<strong>и</strong>к<strong>и</strong>.<br />

Класс Application расш<strong>и</strong>ряется, также, классом Documents,<br />

оп<strong>и</strong>сывающ<strong>и</strong>м все открытые в <strong>Word</strong> документы. Документы открываются<br />

методом Open <strong>и</strong>л<strong>и</strong> создаются методом Add <strong>и</strong> закрываются методом Close<br />

класса Documents. Для учета ч<strong>и</strong>сла открытых документов <strong>и</strong>спользуется<br />

свойство Count.<br />

В качестве компонентов класса Document выступают его свойства:<br />

абзацы (Paragraphs), предложен<strong>и</strong>я (Sentences), слова (<strong>Word</strong>s), с<strong>и</strong>мволы<br />

(Characters). Доступ к отдельным компонентам открывается по <strong>и</strong>х <strong>и</strong>ме<strong>на</strong>м<br />

<strong>и</strong>л<strong>и</strong> <strong>и</strong>ндексам, представляющ<strong>и</strong>м поз<strong>и</strong>ц<strong>и</strong><strong>и</strong> в документе — с первого по Count.<br />

Дв<strong>и</strong>гаясь в глубь объектной <strong>и</strong>ерарх<strong>и</strong><strong>и</strong>, программ<strong>и</strong>ст может<br />

ман<strong>и</strong>пул<strong>и</strong>ровать самым<strong>и</strong> разным<strong>и</strong> объектам<strong>и</strong>. С помощью методов <strong>и</strong>л<strong>и</strong><br />

свойств объекта, стоящего выше в <strong>и</strong>ерарх<strong>и</strong>ческом ряду, в вызывающую<br />

подпрограмму возвращается объект следующего уровня.<br />

Пр<strong>и</strong>меры<br />

Sub СтандартныйД<strong>и</strong>алог ()<br />

Application.Dialogs (wdDialogFileSaveAs).Show<br />

End Sub<br />

Sub МетодЗавершен<strong>и</strong>я ()<br />

MsgBox "Завершен<strong>и</strong>е работы <strong>Word</strong>"<br />

Application.Quit SaveChanges:=wdPromptToSaveChanges<br />

End Sub<br />

Sub Ок<strong>на</strong> ()<br />

' Есл<strong>и</strong> открыто несколько окон<br />

Windows (1).Activate<br />

ActiveWindow.Caption = "Первое окно"<br />

ActiveWindow.DisplayRulers = False<br />

Windows.Arrange<br />

ActivePrinter = "HP LaserJet III"<br />

MsgBox "Кол<strong>и</strong>чество окон равно " & Windows.Count

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!