Microsoft Access 2010 Bible

(Rick Simeone) #1

Part III: More-Advanced Access Techniques


804


‘An error is thrown if Word is not running,
‘so use CreateObject to start up Word:
Set WordObj = CreateObject(“Word.Application”)
End If
‘Make sure the user can see Word:
WordObj.Visible = True
‘Warning:
‘Specify the correct drive and path to the
‘file named Thanks.dotx in the statement below:
WordObj.Documents.Add _
Template:=“C:\Thanks.dotx”, NewTemplate:=False
With WordObj.Selection
.GoTo what:=wdGoToBookmark, Name:=“FullName”
.TypeText rsCust![ContactName]
.GoTo what:=wdGoToBookmark, Name:=“CompanyName”
.TypeText rsCust![CompanyName]
.GoTo what:=wdGoToBookmark, Name:=“Address1”
.TypeText rsCust![Address1]
.GoTo what:=wdGoToBookmark, Name:=“Address2”
If IsNull(rsCust![Address2]) Then
.TypeText ““
Else
.TypeText rsCust![Address2]
End If
.GoTo what:=wdGoToBookmark, Name:=“City”
.TypeText rsCust![City]
.GoTo what:=wdGoToBookmark, Name:=“State”
.TypeText rsCust![State]
.GoTo what:=wdGoToBookmark, Name:=“Zipcode”
.TypeText rsCust![Zipcode]
.GoTo what:=wdGoToBookmark, Name:=“PhoneNumber”
.TypeText rsCust![PhoneNumber]
.GoTo what:=wdGoToBookmark, Name:=“NumOrdered”
.TypeText Forms!Orders![Quantity]
.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
.GoTo what:=wdGoToBookmark, Name:=“FName”
iTemp = InStr(rsCust![ContactName], “ “)
If iTemp > 0 Then
.TypeText Left$(rsCust![ContactName], iTemp - 1)
End If
.GoTo what:=wdGoToBookmark, Name:=“LetterName”
.TypeText rsCust![ContactName]
DoEvents
WordObj.Activate
Free download pdf