[ Pobierz całość w formacie PDF ]
.The sample creates anobject instance with the following code: 366 Part II &' Collaborating and Integrating with Office 2003On Error Resume Next.Set WordObj = GetObject(,  Word.Application )If Err.Number 0 ThenSet WordObj = CreateObject( Word.Application )End IfObviously, you don t want a new instance of Word created every time a thank-you letter isgenerated, so some special coding is required.This code snippet first attempts to create aninstance by using an active instance (a running copy) of Word.If Word is not a runningapplication, an error is generated.Because this function has On Error Resume Next forerror trapping, the code doesn t fail, but instead proceeds to the next statement.If an error isdetected (the Err.Number is not equal to 0), an instance is created by usingCreateObject.Making the instance of Word visibleWhen you first create a new instance of Word, it runs invisibly.This approach enables yourapplication 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 bemade visible by setting the object s Visible property to True by using this line of code:WordObj.Visible = TrueIf you don t set the object instance s Visible property to True, you may create hidden cop-Cautionies of Word that use system resources and never shut down.A hidden copy of Word doesn tshow up in the Task tray or in the Task Switcher.Creating a new document based on an existing templateAfter Word is running, a blank document needs to be created.The following code creates anew document by using the Thanks.dot template:WordObj.Documents.Add Template:= G:\Access 11 Book\thanks.dot , _NewTemplate:=FalseNoteThe 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 functionwhere to insert data.You create bookmarks in Word by highlighting the text that you want tomake a bookmark, selecting Insert Bookmark, and then entering the bookmark name andclicking Add. Chapter 15 &' Exchanging Access Data with Office Applications 367Using Bookmarks to insert dataUsing Automation, you can locate bookmarks in a Word document and replace them with thetext of your choosing.To locate a bookmark, use the Goto method of the Selectionobject.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 intothe document), you replace the bookmark text.To insert text, use the TypeText method ofthe Selection object, as shown here:WordObj.Selection.Goto what:=wdGoToBookmark, Name:= FullNameWordObj.Selection.TypeText rsCust![ContactName]You can t pass a null to the TypeText method.If the value may possibly be Null, you needNoteto check ahead and make allowances.The preceding sample code checks the Address2 fieldfor a Null value and acts accordingly.If you don t pass text to replace the bookmark evenjust a zero length string (  ) the bookmark text remains in the document.Activating the instance of WordTo enable the user to enter data in the new document, you must make Word the activeapplication.If you don t make Word the active application, the user has to switch to Wordfrom Access.You make Word the active application by using the Activate method of theWord object, as follows:WordObj.ActivateDepending on the processing that is occurring at the time, Access may take the focus back fromTipWord.You can help to eliminate this annoyance by preceding the Activate method with aDoEvents statement.Note, however, that this doesn t always work.Moving the cursor in WordYou 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 thelocation of the last bookmark when this code is executed:WordObj.Selection.MoveUp wdLine, 6Closing the instance of the Word objectTo free up resources that are taken by an instance of an Automation object, you should alwaysclose the instance.In this example, the following code is used to close the object instance:Set WordObj = Nothing 368 Part II &' Collaborating and Integrating with Office 2003This code closes the object instance, but not the instance of Word as a running application.Inthis example, the user needs access to the new document, so closing Word would defeat thepurpose of this function.You can, however, automatically print the document and then closeWord.If you do this, you may even choose to not make Word visible during this process.Toclose Word, use the Quit method of the Application object, as follows:WordObj.QuitInserting pictures by using BookmarksIt is possible to perform other unique operations by using Bookmarks.Basically, anythingthat you can do within Word, you can do by using Automation.The following code locates abookmark 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:= PictureWordObj.ChangeFileOpenDirectory  D:\GRAPHICS\WordObj.ActiveDocument.Shapes.AddPicture Anchor:=Selection.Range,_ FileName:= _ D:\GRAPHICS\PICTURE.BMP , LinkToFile:=False,SaveWithDocument _:=TrueUsing Office s Macro RecorderUsing 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 methodsto use.Although the development help system of the Automation Server is a requirement forfully understanding the language, the easiest way to quickly create Automation for Officeapplications like Word is the Macro Recorder.Most versions of Office applications have a Macro Recorder located on the Tools menu (seeFigure 15-9).When activated, the Macro Recorder records all events, such as menu selectionsand button clicks, and creates Visual Basic code from them. Chapter 15 &' Exchanging Access Data with Office Applications 369Figure 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 keyboardcombination and select the template in which to store the macro.If you are creating the macrosimply to create the Visual Basic code, the only thing that you need to be concerned with isthe macro name.Figure 15-10: Enter a macro name and click OK to begin recording the macro.In thisexample, the macro is named  MyMacro [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • szamanka888.keep.pl