Jump to content

Recommended Posts

Posted

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...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...