C
ccn08
I have designed an Access form that allows the user to enter data. the data should be merged to a Word that I already have bookmarked. I found the following code in the "Microsoft Office Access 2007 Bible".
Public Sub MergetoWord_Click()
On Error Resume Next
Dim rsCust As New ADOB.Recordset
Dim sSQL As String
Dim WordObj As Word.Application
Dim iTemp As Integer
sSQL = "SELECT * FROM Customers " _
& "WHERE CustomerNumber = " _
& Forms!Orders![Customernumber]
rstCust.Open sSQL, CurrentProject.Connection
If rsCust.EOF Then
MsgBox "Invalid Customer", vbOKOnly
Exit Sub
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.Document.Add _
Template:="C:\Thanks.dotx", NewTemplate:=False
With WordObj.Selection
.GoTo what:=wdGotoBoomark, Name:="FullName"
.Typetext rsCust![ContactName]
.GoTo what:=wdGotoBoomark, Name:="CompanyName"
.Typetext rsCust![CompanyName]
.GoTo what:=wdGotoBoomark, Name:="Adress1"
.Typetext rsCust![Adress1]
.GoTo what:=wdGotoBoomark, Name:="City"
.Typetext rsCust![City]
.GoTo what:=wdGotoBoomark, Name:="State"
.Typetext rsCust![State]
.GoTo what:=wdGotoBoomark, Name:="Zipcode"
.Typetext rsCust![Zipcode]
.GoTo what:=wdGotoBoomark, Name:="PhoneNumber"
.Typetext rsCust![PhoneNumber]
.GoTo what:=wdGotoBoomark, Name:="NumOrdered"
.Typetext rsCust![Quantity]
.GoTo what:=wdGotoBoomark, 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:="FullName2"
.Typetext rsCust![ContactName]
.GoTo what:=wdGoToBookmark, Name:="FullName3"
.Typetext rsCust![ContactName]
DoEvents
WordObj.Activate
.MoveUp wdLine, 6
End With
Set WordObj = Nothing
DoCmd.Hourglass False
End Sub
It is not working! How do I create the button that will execute this?
Continue reading...
Public Sub MergetoWord_Click()
On Error Resume Next
Dim rsCust As New ADOB.Recordset
Dim sSQL As String
Dim WordObj As Word.Application
Dim iTemp As Integer
sSQL = "SELECT * FROM Customers " _
& "WHERE CustomerNumber = " _
& Forms!Orders![Customernumber]
rstCust.Open sSQL, CurrentProject.Connection
If rsCust.EOF Then
MsgBox "Invalid Customer", vbOKOnly
Exit Sub
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.Document.Add _
Template:="C:\Thanks.dotx", NewTemplate:=False
With WordObj.Selection
.GoTo what:=wdGotoBoomark, Name:="FullName"
.Typetext rsCust![ContactName]
.GoTo what:=wdGotoBoomark, Name:="CompanyName"
.Typetext rsCust![CompanyName]
.GoTo what:=wdGotoBoomark, Name:="Adress1"
.Typetext rsCust![Adress1]
.GoTo what:=wdGotoBoomark, Name:="City"
.Typetext rsCust![City]
.GoTo what:=wdGotoBoomark, Name:="State"
.Typetext rsCust![State]
.GoTo what:=wdGotoBoomark, Name:="Zipcode"
.Typetext rsCust![Zipcode]
.GoTo what:=wdGotoBoomark, Name:="PhoneNumber"
.Typetext rsCust![PhoneNumber]
.GoTo what:=wdGotoBoomark, Name:="NumOrdered"
.Typetext rsCust![Quantity]
.GoTo what:=wdGotoBoomark, 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:="FullName2"
.Typetext rsCust![ContactName]
.GoTo what:=wdGoToBookmark, Name:="FullName3"
.Typetext rsCust![ContactName]
DoEvents
WordObj.Activate
.MoveUp wdLine, 6
End With
Set WordObj = Nothing
DoCmd.Hourglass False
End Sub
It is not working! How do I create the button that will execute this?
Continue reading...