Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

364 Project 3 CREATING A CREATIVE LEARNING PROJECT

Summary

In this chapter, you added functionality to the Creative Learning form. While adding code to the form, you learned about a few new Windows forms controls, such as the ErrorProvider, FileSystemWatcher, NotifyIcon, and ContextMenu controls.

Chapter 17

Interacting with a

Microsoft Word

Document and

Event Viewer

366 Project 3 CREATING A CREATIVE LEARNING PROJECT

In the preceding chapter, you looked at how the Creative Learning application works. However, the way that the Creative Learning application works also involves accessing and processing a Microsoft Word document. In addition, the errors generated during the processing of the Microsoft Word document are trapped in Windows Event Viewer. Therefore, your application needs to interact with both the Microsoft Word document and Windows Event Viewer. This project covers the process of interacting with the Word document and Windows

Event Viewer.

Interacting with a

Microsoft Word Document

In the preceding chapter, you learned how to add a FileSystemWatcher component to your application. This component monitors a specified directory for any changes, such as adding or deleting a file. In addition, you changed the Filter property of the FileSystemWatcher component to *.doc. Doing this restricts the function of a FileSystemWatcher component to only monitoring the Word document. Therefore, when a user adds a Word file to the source directory, the FileSystemWatcher component generates a Created event.

The Created Event

The Created event is a public event of the FileSystemWatcher class.The FileSystemWatcher class generates the Created event when a new file is added to a directory specified in the FullPath property of the event.

To handle the Created event, the FileSystemWatcher class contains a delegate

FileSystemEventHandler. The FileSystemEventHandler delegate takes two para-

meters, the object that causes the event and an argument of the type FileSystemEventArgs that contains information about the event. This information includes the name and path of the directory that is monitored by the FileSystemWatcher component.

INTERACTING WITH A MICROSOFT WORD DOCUMENT

Chapter 17

367

 

 

 

 

In this case, each time a new cash memo is added to the source directory, the FileSystemWatcher component raises a Created event. Therefore, to access and process the cash memo, you need to add programming logic to the Created event

of the FileSystemWatcher class.

Adding Code to the Created Event

When a file is added to the source directory, your application needs to process the cash memo file. File processing involves extracting data from the cash memo, such as Cash Memo No. and Total amount payable. This information is then written to an XML document as a chronological summary of the sales recorded at the bookstores of Creative Learning.

Displaying a Notification Icon with a ToolTip in the Status Area

While the application processes one file, you need to disable the FileSystemWatcher component so that the component does not monitor the source directory for that time. In addition, you can change the notification icon in the status area of the taskbar to denote that the application is processing a file. You can also change the text that is displayed when the user points to the notification icon in the status area. You will now add code to the Created event to perform the activities mentioned earlier in the chapter.

watchDir.EnableRaisingEvents=false; icoNotify.Icon=m_Info; icoNotify.Text=”Processed: “+ e.Name;

The preceding code sets the EnableRaisingEvents property of the FileSystemWatcher class to false. Doing this disables the watchDir component for the time the value of the EnableRaisingEvents property is set to true. Next, the Icon property of the NotifyIcon control is changed to display a different icon in the status area. However, before doing this, you need to add the Info.ico file to the bin folder of your application and then create an instance of the Info.ico file by using the following statement:

private System.Drawing.Icon m_Info= new System.Drawing.Icon(“Info.ICO”);

368 Project 3 CREATING A CREATIVE LEARNING PROJECT

Creating an instance of the Info.ico file adds the file to the Icon class of the System.Drawing namespace. When the notification icon is displayed in the status area, you can use the Text property of the notification icon to display a ToolTip. The Text property appends the word Processed: to the name of the file that is being processed. Figure 17-1 displays the notification icon with a ToolTip.

Y

FIGURE 17-1 Notification icon with a ToolTipLin the status area

Extracting Data from a WordFDocument

the Word application, you need to create an instance of the Word.Application-

M A Your application is nowEready to process the Word document. However, to access

Class class. After you are able to access the Word application, you can create an

instance of the Word.DocumentClass class to access the Word document. To do so,

add the following statements to the

Created event of the FileSystemWatcher

component.

T

 

Word.Application wdApp= new Word.ApplicationClass();

Word.Document Doc = new Word.DocumentClass();

After creating the instance of the Word document Doc, you can use the instance to open the document file that the user adds to the source directory. To do this, you can use the Open() method of the Documents class. The Open() method takes 12 parameters. However, only the first parameter, which is an object containing the file name to be opened, is essential. To pass FileName as a parameter to the Open() method, you can create an object, filename, which stores the name of the file and the full path of the directory where the file is stored. To create the filename object, use the following statement:

object filename=e.FullPath;

TIP

The filename object is passed as a reference parameter to the Open() method.

Team-Fly®

INTERACTING WITH A MICROSOFT WORD DOCUMENT

Chapter 17

369

 

 

 

 

Except for the first parameter, the rest of the parameters of the Open() method are optional. Therefore, you can create an object of the instance of the Missing class

optional.

NOTE

The Missing class is a public sealed class in the System.Reflection namespace. You cannot inherit a Missing class.

The optional object can now be passed as optional parameters to the Open() method. To do this, use the following statements:

object optional=System.Reflection.Missing.Value;

Doc=wdApp.Documents.Open(ref filename, ref optional, ref optional,ref optional,ref

optional,ref optional,ref optional,ref optional,ref optional,ref optional,ref

optional,ref optional);

The preceding code opens the Word document present in the source directory and stores the entire content of the Word document in the instance of the Word.DocumentClass class Doc. However, in this case, you only require the information in the Cash Memo No. and Total amount payable fields. Figure 17-2 shows a sample cash memo document.

FIGURE 17-2 Sample cash memo document

370 Project 3 CREATING A CREATIVE LEARNING PROJECT

To retrieve the required data from the cash memo document, add the following code to the Created event:

Word.Range wdRange; wdRange=Doc.Paragraphs.Item(2).Range; string strMemo, strAmount;

int intParacount; strMemo=wdRange.Text; strMemo=strMemo.Substring(15,4); intParacount=Doc.Paragraphs.Count; intParacount=intParacount-2;

wdRange=Doc.Paragraphs.Item(intParacount).Range; object count=”-1”;

object wdCharacter=”1”;

wdRange.MoveEnd(ref wdCharacter,ref count); strAmount=wdRange.Text; strAmount=strAmount.Substring(23);

The preceding code creates an object of the type Range, wdRange that stores the content of the Word document. You then use the Item property of the Paragraphs collection to retrieve data from a specified paragraph. As shown in Figure 17-2, Cash Memo No. is the second paragraph in the cash memo document. Therefore, you need to retrieve the content of the second paragraph of the cash memo document by using the Range property. The content that is retrieved is then stored in

wdRange.

The Text property of the wdRange object is used to store the text of the paragraph in a string type variable, strMemo. Until now, the strMemo variable stores the entire content of the second paragraph. However, to just retrieve the value of the Cash Memo No. field, use the Substring() method. The Substring() method takes two parameters, the starting position from where the text is retrieved and the number of characters retrieved.

Similarly, you can store the text of the Total amount payable field in another string type variable, strAmount. The Total amount payable field is the second last paragraph in the cash memo document.Therefore, you need to declare an integer variable, intParacount, that stores the number of paragraphs in a document. You use the Count property of the Paragraphs collection to count the number of paragraphs in the document.