Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Docs in a Form using Command Button

Status
Not open for further replies.

gazza1

Technical User
Nov 2, 2001
7
ES
Can I insert a command button in a Form to automatically open a specific Word Template Document? Is it then possible that when that Word Document opens that text from the Form can automatically appear on the Word Doc be setting a "Field" on the Word Doc, linked to the information in specific Fields of my form?

I beleive it to be possible because I have seen it in other Access Databases but I just don´t know how to do it. I know that a command button can launch Microsoft Word but I need it to do more.

Any help would be very much appreciated.

Thanks,

 
To open the word doc you can make your command button a hyperlink to the .dot file
or in vba you can use the following

Dim stAppName As String
Dim stDocName As String
stDocName = """C:\My Documents\Memos\Learning Agreement memo.doc"""
stAppName = "winword.exe" + " " + stDocName
Call Shell(stAppName, 1)

there are as many sets of "" as there are spaces in the docments path + 1 set. so above there are 2 spaces and 3 sets of ""

As for inserting text search the forums i think there is a way mentioned
 
Thanks guitardave78 for your help. I did manage to get it to work. My next problem is having the document open with the information from the Form entering automatically in my Word document.

I´ve looked in the Office forums and nothing. I even started a thread pleading for help and nothing yet.

I´m living in hope.

 
here ya go

Private Sub Command4_Click()



Dim objWord

'Copy the control
DoCmd.GoToControl "Details" 'The text box with the data you want to put into word
DoCmd.RunCommand acCmdCopy

'Start Microsoft Word
Set objWord = CreateObject("Word.Application")

With objWord
'Make the application visible.
.Visible = True

'Open the document.
.Documents.Open ("C:\MyMerge.doc")

'Paste the text.
.Selection.Paste
End With

End Sub

for more info look at
 
Thanks for your help guitardave78. I have the following included and it seems to work. The only problem is that once it prints off once, if I move onto another record and try to print the letter for another client it fails to print a second time. It does input any text. It would appear that the bookmarks on the Word document are lost even though the microsoft support link you provided warns of this happening and provides a solution. Unfortunately the solution does not appear to work. I may have input the the line in the wrong place. Any help or advice greatly appreciated.

Private Sub MergeButton_Click()
On Error GoTo MergeButton_Err

Dim objWord As Word.Application

Set objWord = CreateObject("Word.Application")

With objWord
.Visible = True

.Documents.Open ("\\Rightway-3\C\My Merge Documents\Policy Documents to Client.doc")

'Move to each bookmark and insert text from the form.
.ActiveDocument.Bookmarks("Salutation2").Select
Selection.Text = (CStr(Forms!CLIENTS!Salutation))
'Add this line to reapply the bookmark name to the selection.
.ActiveDocument.Bookmarks.Add Name:="Salutation2", Range:=Selection.Range
.ActiveDocument.Bookmarks("FirstName").Select
Selection.Text = (CStr(Forms!CLIENTS!FirstNames))
'Add this line to reapply the bookmark name to the selection.
.ActiveDocument.Bookmarks.Add Name:="FirstName", Range:=Selection.Range
.ActiveDocument.Bookmarks("Surname2").Select
Selection.Text = (CStr(Forms!CLIENTS!Insured))
'Add this line to reapply the bookmark name to the selection.
.ActiveDocument.Bookmarks.Add Name:="Surname2", Range:=Selection.Range
.ActiveDocument.Bookmarks("Address").Select
Selection.Text = (CStr(Forms!CLIENTS!Address))
'Add this line to reapply the bookmark name to the selection.
.ActiveDocument.Bookmarks.Add Name:="Address", Range:=Selection.Range
.ActiveDocument.Bookmarks("Postcode").Select
Selection.Text = (CStr(Forms!CLIENTS!Postcode))
'Add this line to reapply the bookmark name to the selection.
.ActiveDocument.Bookmarks.Add Name:="Postcode", Range:=Selection.Range
.ActiveDocument.Bookmarks("Town").Select
Selection.Text = (CStr(Forms!CLIENTS!Town))
'Add this line to reapply the bookmark name to the selection.
.ActiveDocument.Bookmarks.Add Name:="Town", Range:=Selection.Range
.ActiveDocument.Bookmarks("Region").Select
Selection.Text = (CStr(Forms!CLIENTS!Region))
'Add this line to reapply the bookmark name to the selection.
.ActiveDocument.Bookmarks.Add Name:="Region", Range:=Selection.Range
.ActiveDocument.Bookmarks("ClientID").Select
Selection.Text = (CStr(Forms!CLIENTS!ClientID))
'Add this line to reapply the bookmark name to the selection.
.ActiveDocument.Bookmarks.Add Name:="ClientID", Range:=Selection.Range
.ActiveDocument.Bookmarks("CoverNo").Select
Selection.Text = (CStr(Forms!CAR!CoverNo))
'Add this line to reapply the bookmark name to the selection.
.ActiveDocument.Bookmarks.Add Name:="CoverNo", Range:=Selection.Range
.ActiveDocument.Bookmarks("Salutation").Select
Selection.Text = (CStr(Forms!CLIENTS!Salutation))
'Add this line to reapply the bookmark name to the selection.
.ActiveDocument.Bookmarks.Add Name:="Salutation", Range:=Selection.Range
.ActiveDocument.Bookmarks("Surname").Select
Selection.Text = (CStr(Forms!CLIENTS!Insured))
'Add this line to reapply the bookmark name to the selection.
.ActiveDocument.Bookmarks.Add Name:="Surname", Range:=Selection.Range
.ActiveDocument.Bookmarks("PolicyNumber").Select
Selection.Text = (CStr(Forms!CAR!PolicyNumber))
'Add this line to reapply the bookmark name to the selection.
.ActiveDocument.Bookmarks.Add Name:="PolicyNumber", Range:=Selection.Range
.ActiveDocument.Bookmarks("Make").Select
Selection.Text = (CStr(Forms!CAR!Make))
'Add this line to reapply the bookmark name to the selection.
.ActiveDocument.Bookmarks.Add Name:="Make", Range:=Selection.Range
.ActiveDocument.Bookmarks("Model").Select
Selection.Text = (CStr(Forms!CAR!Model))
'Add this line to reapply the bookmark name to the selection.
.ActiveDocument.Bookmarks.Add Name:="Model", Range:=Selection.Range
.ActiveDocument.Bookmarks("Administrator").Select
Selection.Text = (CStr(Forms!CAR!Administrator))
'Add this line to reapply the bookmark name to the selection.
.ActiveDocument.Bookmarks.Add Name:="Administrator", Range:=Selection.Range

End With
objWord.ActiveDocument.PrintOut Background:=False

objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

objWord.Quit
Set objWord = Nothing
Exit Sub

MergeButton_Err:
If Err.Number = 94 Then
objWord.Selection.Text = ""
Resume Next

End If

Exit Sub

End Sub
 
mmmm
try SaveChanges:=wdSaveChanges

that may save the bookmarks
but not sure mate not had to use it !!
 
Thanks for suggestion guitardave78 but there is already DoNotSaveChanges in the EventProcedure. This is so Word closes and doesn´t lose the bookmarks.
The Event Procedure is working fine. When I press the command button it opens Word, inserts the correct information, prints off the Document and then closes Word. The problem is when I move to the next record. On pressing the command button it opens Word, opens the correct document but does not change the text. It just stops at the very first bookmark. Now, the bookmarks are not lost because the strange thing is, If I close the Access Database and reopen it and then move to the next record for whom I wish to print off a letter the Event Procedure works perfectly. For some reason I have to close down Access every time I want to print a letter off for a new client. Before putting this command button on my own database I tried it on the Norton Sample Database as suggested in the Microsoft Support link you provided and it worked perfectly. I´m really stuck and this could be really advantageous to me. Any other ideas or any idea where or who could advise? Many thanks for all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top