1. Trang chủ
  2. » Công Nghệ Thông Tin

Microsoft Office 2003 Super Bible phần 7 potx

64 309 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 64
Dung lượng 1,13 MB

Nội dung

361Chapter 15 ✦ Exchanging Access Data with Office Applications Just as in Access, Word is implicitly using its Application object; the command ChangeFileOpenDirectory is really a method of the Application object. Using the following code, you create an instance of Word’s Application object and call the method of the object: Dim WordObj As New Word.Application WordObj.ChangeFileOpenDirectory “C:\My Documents\” When using Automation, you should avoid setting properties or calling methods that cause the Automation Server to ask for input from the user via a dialog box. When a dialog box is dis- played, the Automation code stops executing until the dialog box is closed. If the server applica- tion is minimized or behind other windows, the user may not even be aware that he or she needs to provide input, and therefore may assume that the application is locked up. Closing an instance of an Automation object Automation objects are closed when the Automation object variable goes out of scope. Such a closing, however, doesn’t necessarily free up all resources that are used by the object, so you should explicitly close the instance of the Automation object. You can close an Automation object by doing either of the following: • Using the Close or Quit method of the object (consult the specific Automation Server’s documentation for information on which method it supports) • Setting the object variable to nothing, as follows: Set WordObj = Nothing The best way to close an instance of an Automation object is to combine the two techniques, like this: WordObj.Quit Set WordObj = Nothing An Automation Example Using Word Perhaps the most common Office application that is used for Automation from a database application like Access is Word. Using Automation with Word, you can create letters that are tailored with information from databases. The following section demonstrates an example of merging information from an Access database to a letter in Word by using Automation and Word’s Bookmarks. Ordinarily, you create a merge document in Word and bring field contents in from the records of an Access database. This method relies on using Word’s MergeField, which is replaced by the contents of the Database field. It normally requires that you perform this action in Word—thus limiting the scope and capability of the function. For example, you will merge all records from the table that is being used rather than a single record. Tip 362 Part II ✦ Collaborating and Integrating with Office 2003 The following example uses the Orders form, which calls a module named WordIntegration. The WordIntegration module contains a function named MergetoWord() that uses the Word Thanks.dot template file. When you attempt to run this example, you must make sure that the path for the template in the Visual Basic code is the actual path in which the Thanks.dot template file resides. This path may vary from computer to computer. The items that are discussed in this Word Automation example include the following: ✦ Creating an instance of a Word object ✦ Making the instance of Word visible ✦ Creating a new document based on an existing template ✦ Using bookmarks to insert data ✦ Activating the instance of Word ✦ Moving the cursor in Word ✦ Closing the instance of the Word object without closing Word This example prints a thank-you letter for an order based on bookmarks in the thank you letter template (Thanks.dot). Figure 15-5 shows the data for customers; Figure 15-6 shows the data entry form for orders; Figure 15-7 shows the Thanks.dot template; and Figure 15-8 shows a completed merge letter. The bookmarks in Figure 15-7 are shown as grayed large I-beams (text insert). The bookmarks are normally not visible, but you can make them visible by selecting Tools_Options, selecting the View tab and going to the top section titled Show and then turning on the Bookmarks option by checking the option (third choice in the first column). The names won’t be visible—only the bookmark holders (locations) will be visible, as shown in Figure 15-7. The names and arrows in Figure 15-7 were placed using text boxes to show where the bookmark names are assigned. Note Figure 15-5: Customer data used in the following Automation example is entered on the Customers form. 363Chapter 15 ✦ Exchanging Access Data with Office Applications Figure 15-6: Each customer can have an unlimited number of orders. Thank-you letters are printed from the Orders form. Figure 15-7: The Thanks.dot template contains bookmarks where the merged data is to be inserted. Figure 15-8: After a successful merge, all the bookmarks have been replaced with their respective data. 364 Part II ✦ Collaborating and Integrating with Office 2003 If you click the Print Thank You Letter button in Access while Word is open with an existing document—which lacks the bookmark names specified in the code—the fields will simply be added to the text inside Word at the point where the cursor is currently sitting. When the user clicks the Print Thank You Letter button on the Orders form, Word generates a thank-you letter with all the pertinent information. The following code shows the MergetoWord function in its entirety so you can see in-depth how it works. Public Function MergetoWord() ‘ This method creates a new document in MS Word ‘ using Automation. On Error Resume Next Dim rsCust As Recordset, iTemp As Integer Dim WordObj As Word.Application Set rsCust = DBEngine(0).Databases(0).OpenRecordset(“Customers”, _ dbOpenTable) rsCust.Index = “PrimaryKey” rsCust.Seek “=”, Forms!Orders![CustomerNumber] If rsCust.NoMatch Then MsgBox “Invalid customer”, vbOKOnly Exit Function End If DoCmd.Hourglass True Set WordObj = GetObject(, “Word.Application”) If Err.Number <> 0 Then Set WordObj = CreateObject(“Word.Application”) End If WordObj.Visible = True WordObj.Documents.Add ‘ WARNING: ‘ Specify the correct drive and path to the ‘ file named thanks.dot in the line below. Template:=”G:\Access 11 Book\thanks.dot”, ‘ The above path and drive must be fixed NewTemplate:=False WordObj.Selection.Goto what:=wdGoToBookmark, Name:=”FullName” WordObj.Selection.TypeText rsCust![ContactName] WordObj.Selection.Goto what:=wdGoToBookmark, Name:=”CompanyName” WordObj.Selection.TypeText rsCust![CompanyName] WordObj.Selection.Goto what:=wdGoToBookmark, Name:=”Address1" WordObj.Selection.TypeText rsCust![Address1] WordObj.Selection.Goto what:=wdGoToBookmark, Name:=”Address2" Caution 365Chapter 15 ✦ Exchanging Access Data with Office Applications If IsNull(rsCust![Address2]) Then WordObj.Selection.TypeText “” Else WordObj.Selection.TypeText rsCust![Address2] End If WordObj.Selection.Goto what:=wdGoToBookmark, Name:=”City” WordObj.Selection.TypeText rsCust![City] WordObj.Selection.Goto what:=wdGoToBookmark, Name:=”State” WordObj.Selection.TypeText rsCust![State] WordObj.Selection.Goto what:=wdGoToBookmark, Name:=”Zipcode” WordObj.Selection.TypeText rsCust![Zipcode] WordObj.Selection.Goto what:=wdGoToBookmark, Name:=”PhoneNumber” WordObj.Selection.TypeText rsCust![PhoneNumber] WordObj.Selection.Goto what:=wdGoToBookmark, Name:=”NumOrdered” WordObj.Selection.TypeText Forms!Orders![Quantity] WordObj.Selection.Goto what:=wdGoToBookmark, Name:=”ProductOrdered” If Forms!Orders![Quantity] > 1 Then WordObj.Selection.TypeText Forms!Orders![Item] & “s” Else WordObj.Selection.TypeText Forms!Orders![Item] End If WordObj.Selection.Goto what:=wdGoToBookmark, Name:=”FName” iTemp = InStr(rsCust![ContactName], “ “) If iTemp > 0 Then WordObj.Selection.TypeText Left$(rsCust![ContactName], iTemp _ - 1) End If WordObj.Selection.Goto what:=wdGoToBookmark, Name:=”LetterName” WordObj.Selection.TypeText rsCust![ContactName] DoEvents WordObj.Activate WordObj.Selection.MoveUp wdLine, 6 ‘ Set the Word Object to nothing to free resources Set WordObj = Nothing DoCmd.Hourglass False Exit Function TemplateError: Set WordObj = Nothing Exit Function End Function Creating an instance of a Word object The first step in using Automation is to create an instance of an object. The sample creates an object instance with the following code: 366 Part II ✦ Collaborating and Integrating with Office 2003 On Error Resume Next Set WordObj = GetObject(, “Word.Application”) If Err.Number <> 0 Then Set WordObj = CreateObject(“Word.Application”) End If Obviously, you don’t want a new instance of Word created every time a thank-you letter is generated, so some special coding is required. This code snippet first attempts to create an instance by using an active instance (a running copy) of Word. If Word is not a running application, an error is generated. Because this function has On Error Resume Next for error trapping, the code doesn’t fail, but instead proceeds to the next statement. If an error is detected (the Err.Number is not equal to 0), an instance is created by using CreateObject. Making the instance of Word visible When you first create a new instance of Word, it runs invisibly. This approach enables your application to exploit features of Word without the user even realizing that Word is running. In this case, however, it is desirable to let the user edit the merged letter, so Word needs to be made visible by setting the object’s Visible property to True by using this line of code: WordObj.Visible = True If you don’t set the object instance’s Visible property to True, you may create hidden cop- ies of Word that use system resources and never shut down. A hidden copy of Word doesn’t show up in the Task tray or in the Task Switcher. Creating a new document based on an existing template After Word is running, a blank document needs to be created. The following code creates a new document by using the Thanks.dot template: WordObj.Documents.Add Template:=”G:\Access 11 Book\thanks.dot”, _ NewTemplate:=False The path must be corrected in order to point to the Thanks.dot template on your computer. The Thanks.dot template contains bookmarks (as shown in Figure 15-7) that tell this function where to insert data. You create bookmarks in Word by highlighting the text that you want to make a bookmark, selecting Insert_Bookmark, and then entering the bookmark name and clicking Add. Caution Note 367Chapter 15 ✦ Exchanging Access Data with Office Applications Using Bookmarks to insert data Using Automation, you can locate bookmarks in a Word document and replace them with the text of your choosing. To locate a bookmark, use the Goto method of the Selection object. After you have located the bookmark, the text comprising the bookmark is selected. By inserting text (which you can do by using Automation or simply by typing directly into the document), you replace the bookmark text. To insert text, use the TypeText method of the Selection object, as shown here: WordObj.Selection.Goto what:=wdGoToBookmark, Name:=”FullName” WordObj.Selection.TypeText rsCust![ContactName] You can’t pass a null to the TypeText method. If the value may possibly be Null , you need to check ahead and make allowances. The preceding sample code checks the Address2 field for a Null value and acts accordingly. If you don’t pass text to replace the bookmark—even just a zero length string (“ ”)—the bookmark text remains in the document. Activating the instance of Word To enable the user to enter data in the new document, you must make Word the active application. If you don’t make Word the active application, the user has to switch to Word from Access. You make Word the active application by using the Activate method of the Word object, as follows: WordObj.Activate Depending on the processing that is occurring at the time, Access may take the focus back from Word. You can help to eliminate this annoyance by preceding the Activate method with a DoEvents statement. Note, however, that this doesn’t always work. Moving the cursor in Word You can move the cursor in Word by using the MoveUp method of the Selection object. The following example moves the cursor up six lines in the document. The cursor is at the location of the last bookmark when this code is executed: WordObj.Selection.MoveUp wdLine, 6 Closing the instance of the Word object To free up resources that are taken by an instance of an Automation object, you should always close the instance. In this example, the following code is used to close the object instance: Set WordObj = Nothing Note Tip 368 Part II ✦ Collaborating and Integrating with Office 2003 This code closes the object instance, but not the instance of Word as a running application. In this example, the user needs access to the new document, so closing Word would defeat the purpose of this function. You can, however, automatically print the document and then close Word. If you do this, you may even choose to not make Word visible during this process. To close Word, use the Quit method of the Application object, as follows: WordObj.Quit Inserting pictures by using Bookmarks It is possible to perform other unique operations by using Bookmarks. Basically, anything that you can do within Word, you can do by using Automation. The following code locates a bookmark that marks where a picture is to be placed and then inserts a .BMP file from disk. You can use the following code to insert scanned signatures into letters: WordObj.Selection.Goto what:=wdGoToBookmark, Name:=”Picture” WordObj.ChangeFileOpenDirectory “D:\GRAPHICS\” WordObj. ActiveDocument.Shapes.AddPicture Anchor:=Selection.Range, _ FileName:= _ “D:\GRAPHICS\PICTURE.BMP”, LinkToFile:=False, SaveWithDocument _ :=True Using Office’s Macro Recorder Using Automation is not a difficult process when you understand the fundamentals. Often, the toughest part of using Automation is knowing the proper objects, properties, and methods to use. Although the development help system of the Automation Server is a requirement for fully understanding the language, the easiest way to quickly create Automation for Office applications like Word is the Macro Recorder. Most versions of Office applications have a Macro Recorder located on the Tools menu (see Figure 15-9). When activated, the Macro Recorder records all events, such as menu selections and button clicks, and creates Visual Basic code from them. 369Chapter 15 ✦ Exchanging Access Data with Office Applications Figure 15-9: The Macro Recorder in Word is a powerful tool to help you create Automa- tion code. After selecting Tools_Macro_Record New Macro, you must give your new macro a name (see Figure 15-10). In addition to a name, you can assign the macro to a toolbar or keyboard combination and select the template in which to store the macro. If you are creating the macro simply to create the Visual Basic code, the only thing that you need to be concerned with is the macro name. Figure 15-10: Enter a macro name and click OK to begin recording the macro. In this example, the macro is named “MyMacro.” After you enter a macro name and click OK, the Macro Recorder begins recording events and displays a Stop Recording window, and the arrow changes to an open pointer attached to a cassette, as shown in Figure 15-11. You can stop recording events by clicking the Stop button (the button with a square on it). To pause recording events, click the other button, which is the Pause button. 370 Part II ✦ Collaborating and Integrating with Office 2003 Figure 15-11: The Macro Recorder records all events until you click the Stop button. After you have finished recording a macro, you can view the Visual Basic code created from your events. To view the code of a macro, select Tools_Macro_Macros to display a list of all saved macros. Then select the macro that you recorded and click the Edit button to display the Visual Basic editor with the macro’s code. Figure 15-12 shows the Visual Basic editor with a macro that recorded the creation of a new document using the Normal template and the insertion of a picture using the Insert_Picture_From File menu item. In the application for which a macro is created, the Application object is used explicitly. When you use the code for Automation, you must create an Application object accordingly. For example, the preceding macro uses the following code to create a new document: Documents.Add Template:=” Normal.dot”, NewTemplate:= False, DocumentType:=0 This code implicitly uses the Application object. To use this code for Automation, copy the code from the Visual Basic editor, paste it into your procedure, and create an object that you use explicitly, as follows: Dim WordObj as New Word.Application WordObj.Documents.Add Template:=” Normal.dot”, NewTemplate:= False, DocumentType:=0 [...]... service that provides a collaboration and information presentation environment that integrates with Microsoft Office 2003 applications such as Word 2003, Outlook, Excel, PowerPoint, and Access Windows SharePoint Services (WSS) is an evolution of SharePoint Team Services (STS), which shipped with Microsoft Office XP If you have previously used SharePoint Team Services, you will find the new and improved... be decrypted in Step 2 Distributing Office Documents Group collaboration on documents requires the capability to save Office documents somewhere where they are available to everyone in the group Office provides plenty of help to that end; the latest development in this process is SharePoint Team Services (STS) 390 Part II ✦ Collaborating and Integrating with Office 2003 Even if your organization isn’t... from moving, hiding, unhiding, resizing, or closing workbook windows • Password allows you to enter a password that users must have before they can unprotect the workbook 377 378 Part II ✦ Collaborating and Integrating with Office 2003 Figure 16-5: Protect elements of your workbook here 5 Protect and Share Workbook brings up a dialog box with only one box you can check, to prevent those sharing the... Collaborating and Integrating with Office 2003 Sharing Access Databases The information in the typical Access database is valuable not only to people working in Access but also to people working in all other Office applications Typically, the Access database changes constantly as changes are made to the data in it; by drawing on it, network users can ensure that their own Office projects always contain... box, and then select the group you want to add him or her to; click the New button 7 Enter the name of the user and the personal ID — a string of four to 20 characters of your choice that Access combines with the user’s name to identify that user in the group 3 87 388 Part II ✦ Collaborating and Integrating with Office 2003 8 Click OK to create the new account 9 To create a new group, click the Groups... Security Options dialog box from the previous section) This opens the Document Protection task pane shown in Figure 16-2 Figure 16-2: Protect Word documents using this task pane 375 376 Part II ✦ Collaborating and Integrating with Office 2003 2 Under Formatting restrictions, check the checkbox if you want to limit formatting to a selection of styles, and then click Settings to open the Formatting Restrictions... the most commonly shared types of Office files You can make them freely available or create very tight security for them by using the commands under Tools_Security on the menu ✦ E-mail is another way to share Office documents You can send documents to individuals or to a sequential group of recipients for review ✦ ✦ ✦ Windows SharePoint Services with Office System 17 C H A P T E R In This Chapter... 16-1 In This Chapter Resource sharing and security Collaborating in Word Sharing Excel workbooks Collaborating in PowerPoint Sharing Access databases Distributing Office documents 374 Part II ✦ Collaborating and Integrating with Office 2003 Figure 16-1: The Security dialog box in Word lets you restrict access to any file Three levels of file-sharing security are provided here: ✦ Password to open... part of your everyday Office experience Note Windows SharePoint Services requires installation on Microsoft Windows Server 2003, Standard Edition, Enterprise Edition, or Datacenter Edition This chapter assumes you have access to a server running Windows SharePoint Services If you don’t have such access, you might still find the information in this chapter informative Visit www .microsoft. com/sharepoint/... Note Although you can, and do, perform many tasks with Windows SharePoint Services sites using a Web browser, such as Internet Explorer, you can more successfully work with Office 2003 documents on SharePoint sites if the Office 2003 application that is associated with those documents is also installed Access methods depend on your administrator and how the site and server that support the site are . recording events, click the other button, which is the Pause button. 370 Part II ✦ Collaborating and Integrating with Office 2003 Figure 15-11: The Macro Recorder records all events until you click. choose Tools_Security Options. This opens the Security dialog box shown in Figure 16-1. 374 Part II ✦ Collaborating and Integrating with Office 2003 Figure 16-1: The Security dialog box in Word lets you restrict. Figure 16-2. Figure 16-2: Protect Word documents using this task pane. 376 Part II ✦ Collaborating and Integrating with Office 2003 2. Under Formatting restrictions, check the checkbox if you want

Ngày đăng: 13/08/2014, 22:21

TỪ KHÓA LIÊN QUAN