Vba word objects examples. Programming in Word


10. Programming in Word

10.1 Why program in Word

Creating VBA Applications in Word, typical situations

Word is the oldest and most popular app, included in Microsoft Office. In most organizations, users prepare documents in Word.

From a programming point of view, Word is primarily a tool for producing reports on databases. In this case, a report is any document that is generated on the basis of information from the database, for example, an agreement, an acceptance certificate, a cash receipt order, an announcement for a cash contribution, an order to the accounting department, an invoice, etc. Of course, reports that can be generated in Word also include documents with summary data - period reports, statements, etc.

The author had to create applications with reports developed in a variety of software products - Microsoft Access, Crystal Reports, Microsoft Reporting Services, etc. If your application generates database reports in Microsoft Word, then, most likely, such reports will not be the fastest in terms of their generation and not the simplest in terms of programming. But they will definitely be the friendliest towards the end user. Why?

Very often at an enterprise there is a need to correct just a couple of lines in the report form - for example, instead of “Director” put “Acting Director”. If the report was created in Crystal Reports or Microsoft Reporting Services, you will need to contact the developer immediately. And after some time I.O. will be confirmed as director, and the developer will have to edit the report again.

If the report is initially created in a Word document, then the user can always make the necessary changes to the created document - the vast majority of users in the enterprise know how to work in Word. There is no need to urgently look for a developer anymore.

There are other benefits to producing reports in Word. As a rule, when producing reports in Word, values ​​from the database are substituted into the report template, which is stored in the database or file system (in a *.dot file). If the report format is complex, with a lot of specific design (for example, an announcement for a cash contribution), then it is much easier to prepare its template in Word than, for example, in Crystal Reports or Reporting Services.

Another software application Word - working with different document formats. Word can work with documents of a variety of formats. These capabilities can be used for mass processing documents.

A case from practice: in a directory on disk we have collected several hundred “various-sized” documents from different users. Some of them were created in Word different versions, some are just text files, some documents are in HTML, XML or EML formats (email messages). The enterprise has implemented a document management system based on SharePoint Portal Server and we need to bring all these documents to a single format (Word 2003) and upload them to SharePoint Portal Server. Of course, without automation in such a situation you will have to tinker for a very long time.

The third software application of Word is formatting documents, for example, programmatically applying styles, searching and replacing sections of text in many documents at once, working with document structure, etc. Typically, such tasks are set in publishing houses, for example, when preparing manuscripts.

On a Word document page, you can organize work with a fairly complex program, without even creating separate Windows forms for this, which are launched, for example, when you open a document or when you click on a button on the document page, but simply by placing data entry fields on it and showing programmatically on the same page the results after changing the data.

However, many mathematical calculations are easier to perform in Excel; when storing information in databases, Access has very great capabilities for processing this information. When working with databases in large information systems It may be possible to generate reports with their output in Word or Excel, and the development of such programs can only be carried out by a specialist who is well acquainted with both the database architecture of the information system and the VBA language.

You can learn some principles of working with Word objects if you record a certain sequence of your actions using the Word Record Macro tool, which is present on the Developer tab of the ribbon. This tab is present if, in Word Options on the General page, the Show Developer tab on the ribbon checkbox is checked.

Let's do it the following actions:

1) click on the Record macro button; neither the button nor the keys can be assigned to the macro; we set where the text of the macro will be saved: The macro is available for the current document;

2) move to the end of the document from the current position (it was not at the end) by pressing the Ctrl+End keys;

3) click Enter key and write the word “Hello!”;

4) repeat the operation of step 3 again;

5) select the last word by pressing Shift+Home and press the Delete key;

6) go to the beginning of the first line “Hello!”, press Shift+End and then the Delete key;

7) on the Developer tab, first click the Stop recording button, then the Visual Basic button;

8) in the Visual Basic editor window that opens on the left in the Project panel, let’s do double click mouse on the section Progect() - Modules - NewMacros and see on the right a window with the text of the program in VBA, which is shown in Figure 5.43.

When saving a document that contains software modules, V Microsoft system Office 2007 must select the file type Word Document with macro support, otherwise program text will not be saved.

In the macro text in Figure 5.43, Selection is one of the main objects that contains the main Application object - the Word application.

Figure 5.43. Macro text in VBA


When writing program text, you can use the pop-up list of properties and methods that appears for standard objects in the Visual Basic editor after writing the object name and clicking the dot after it (see Figure 5.44), Full description properties and methods of objects is present in the help system.

You can also get a pop-up list of properties and methods by pressing the Ctrl+Space key combination.


In addition, when writing program text for standard procedures and functions, tooltips appear on their syntax (see Figure 5.45).

MsgBoA(Protr [ShsteLz VbMsgBoxStule = vbOKOPlu], )







2024 gtavrl.ru.