rupes_mahal
09-19-2001, 06:40 AM
Hi..
What I am trying to do is this:
I have a form which has details of Doctors, such name, address, doctors code, etc. I want a command button to open up a Mircosoft Word Document called "LetterHead". On the document, I want the Doctors Name, address and doctors Code to be pasted straight in, when it opens. The details of the Doctors, come from the form.
I have never attempted this before.....can this be done and if so, how?
Please help...
Thankyou in advance
ruby
Hi http://www.access-programmers.co.uk/ubb/smile.gif
Y dont u consider to build a report which will just looks like a letter head n fill in all the details of doctors. Because thats what reports r meant for. Let me know if u need help regarding reports. Building a report n then exporting it into word could also be one option.
Cheers!
Aqif
jwindon
09-30-2001, 06:04 AM
Ruby:
Yes this can be done, with VBA. You can create the same type thing in a report, but I am gathering your looking for a letter type document. Access is a lousy word processor. Designing a letter in Report View is difficult at best because field sizes can shift your control boxes around.
The first step you will need to take is to create a Word Document and put bookmarks in the document that Access will "read". Then there is some code you will add to your OnClick event of your button "Letterhead".
I will give you an example of my code and let you play with it. Hopefully, you can fit it to make yours work. And also, I wouldn't mind some improvements from others as this was first attempt at this task as well.
Good luck.
Private Sub Command7_Click()
Dim dbs As DAO.Database
Dim rstMergeThese As Recordset
Dim oApp As Object
Set oApp = CreateObject("Word.Application")
oApp.Visible = True
With oApp
.Documents.Open "C:\My Documents\DataMerge.doc"
' Move to each bookmark and insert text from the form.
.ActiveDocument.bookmarks("FirstName").Select
.selection.Text = (CStr(Forms!frmForm1!FirstName))
.ActiveDocument.bookmarks("LastName").Select
.selection.Text = (CStr(Forms!frmForm1!LastName))
'if word isn't running, start and activate it
If Err Then
Shell "C:\Program Files\Microsoft Office\Office\" & "WinWord / Automation", vbMaximizedFocus
AppActivate "Microsoft Word"
End If
' Print the document in the foreground so Microsoft Word 97
' will not close until the document finishes printing.
.ActiveDocument.PrintOut Background:=False
' Close the document without saving changes.
.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' Quit Microsoft Word and release the object variable.
.Quit
Set oApp = Nothing
End With
End Sub
Note: This works on the current record only. If you want to do more than one at a time, use a Mail Merge Doc. There is different Code for that. I also have this if you need it.
Also, make sure you have your object libraries for Word and DAO checked. (In the VB window, goto Tools>References and check Word 8.0 or 9.0 and the DAO box.)
[This message has been edited by jwindon (edited 09-30-2001).]
CandyBoy
10-01-2001, 12:52 PM
I would love to see the code you have have for a mail merge to word.
jwindon
10-01-2001, 03:34 PM
CandyBoy: This code prints out the document and closes. I decided to take out that option to give the view a chance to do something else with the document (i.e. save or alter)
As I said above, this is my first try. It works and I was thrilled!
Function Merge()
Dim oApp As Object
Set oApp = CreateObject("Word.Application")
oApp.Visible = True
With oApp
.Documents.Open "C:\My Documents\MailMerge.doc"
.ActiveDocument.MailMerge.Execute
.ActiveDocument.PrintOut Background:=False
' Close the document without saving changes.
.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' Quit Microsoft Word and release the object variable.
.Quit
Set oApp = Nothing
End With
End Function
PS. Forgot to mention that you need to have created a Word Mail merge document that is already set with it's data source as your query or table in your database. Just like if you were doing a merge from Word, WITHOUT hitting the merge button. Save it and let Access command it to merge.
[This message has been edited by jwindon (edited 10-01-2001).]
rupes_mahal
10-05-2001, 02:08 PM
Jwindon...
Thankyou for your help..
I have inserted your code and changed it according to my form. But I keep getting a error message:
ActiveX component can't create object.
I don't know what this means and why it keeps appearing...
Please help
jwindon
10-05-2001, 03:04 PM
I don't know either. There's nothing involving an Active X component in my code. Did you set your library references?
rupes_mahal
10-06-2001, 09:22 AM
yea I did set the reference....but I keep getting the error message as above.
It highlights the following line:
Set oApp = CreateObject("Word.Application")
I think it has something to do with CreateObject.....
Please help..
thankyou
ruby