Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources] - نسخه متنی

Alison Balter

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



Controlling Word from Access


As you discovered in the preceding section, Excel exposes many objects. You can manipulate each of these objects separately, using Excel's own properties and methods. Prior to Office 97, this was not true for Word, because Word exposed only one object, called Word.Basic. Microsoft Word 97, and versions subsequent to it, all sport the Visual Basic for Applications language. These newer versions of Word expose many objects just as Excel and other Microsoft products do.

Just as with Excel, you can use the Dim statement or Dim as New statement to launch Word. Like Excel, Word launches as a hidden object. The Word application object has a Visible property, which makes the Word object visible. If you create a Word object using automation, Word will not automatically terminate, even if the object variable is destroyed.

Using Word to Generate a Mass Mailing


Figure 22.8 shows the form called frmMergeToWord, which shows the results of running a query called qryMailMerge. After the user clicks the Merge to Word command button, all the records displayed are sent to a Word mail merge and printed. Figure 22.9 shows an example of the resulting document, and Listing 22.8 shows the code that generated this document.

Listing 22.8 Generating a Word Mail Merge Document

Private Sub cmdMergeToWord_Click()
On Error GoTo cmdMergeToWord_Err
'Turn Hourglass on
DoCmd.Hourglass True
'Attempt to create a Word object
If CreateWordObj() Then
'If Word object created
With gobjWord
'Make Word visible
.Visible = True
'Open a document called CustomerLetter in the
'current folder
.Documents.Open CurrentProject.Path & _
"\customerletter.doc"
'Give the document time to open
DoEvents
'Use the MailMerge method of the document
'to perform a mail merge
With gobjWord.ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
.Execute
End With
'Send the result of the merge to the print preview
'window
.ActiveDocument.PrintPreview 'Preview
'Make Word visible
.Visible = True
End With
End If
cmdMergeToWord_Exit:
'Turn hourglass off
DoCmd.Hourglass False
Exit Sub
cmdMergeToWord_Err:
'Display error message, destroy Word object and go
'to common exit routine
MsgBox "Error # " & Err.Number & ": " & Err.Description
Set gobjWord = Nothing
Resume cmdMergeToWord_Exit
End Sub
Figure 22.8. The data that will be merged to Word.


Figure 22.9. The result of the mail merge.


The code begins by presenting an hourglass mouse pointer to the user. This helps to ensure that, if the process takes a while, the user knows that something is happening. It then calls the CreateWordObj routine to create a Word object. The CreateWordObj routine is similar to the CreateExcel routine shown earlier in the chapter. The code executes the Open method on the Documents collection of the Word object. It opens a document called customerletter in the current folder. The customerletter document already has been set up to do a mail merge with the results of a query called qryMerge. The subroutine sets the Destination property of the MailMerge object to a new document. It sets the SuppressBlankLines property to True, and then executes the mail merge with the Execute method. This merges the results of qryMailMerge and creates a new document with the mail-merged letters. The PrintPreview method is executed on the ActiveDocument object so that the merged document is printed. Finally, the Visible property of the Word object is set to True, making Word visible, and the hourglass vanishes.

Using Word to Overcome the Limitations of Access as a Report Writer


Although in most ways Access is a phenomenal report writer, it does have its limitations. For example, you cannot bold or italicize an individual word or phrase within a text box. This is quite limiting if you need to emphasize something such as a past due amount in a dunning letter. When the document I need to produce appears more like a letter than a report, I often think of Microsoft Word. The document pictured in Figure 22.10 produces a letter that provides information to the recipient of an order. The code shown in Listing 22.9 produces the letter.

Listing 22.9 Working with Word Bookmarks

Private Sub cmdSendConfirmation_Click()
Dim objDocument As Word.Document
'Launch Word
If CreateWordObj() Then
'Make Word visible
gobjWord.Visible = True
'Point the Document object at a new document
'based on the Order.dot template
Set objDocument = gobjWord.Documents.Add _
(CurrentProject.Path & "\Order.dot")
'Populate all of the bookmarks with the order information
With objDocument.Bookmarks
.Item("CompanyNameAddress").Range.Text = Nz(Me.txtShipName)
.Item("Address").Range.Text = Nz(Me.txtShipAddress)
.Item("City").Range.Text = Nz(Me.txtShipCity)
.Item("Region").Range.Text = Nz(Me.txtShipRegion)
.Item("PostalCode").Range.Text = Nz(Me.txtShipPostalCode)
.Item("CompanyName").Range.Text = Nz(Me.txtShipName)
.Item("Shipper").Range.Text = Nz(Me.txtShipName)
.Item("ShippedDate").Range.Text = Nz(Me.txtShippedDate)
.Item("FreightAmount").Range.Text = Nz(Me.txtFreight)
End With
End If
End Sub
Figure 22.10. Order confirmation letter produced in Microsoft Word.


The example first launches Word. It then gets a reference to a new document based on the Order.dot template. After that, it populates bookmarks in the document with values from the currently displayed order.


/ 544