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!

problem trying to automate word with Access 2000!!!

Status
Not open for further replies.

neemi

Programmer
May 14, 2002
519
GB
I am trying to write some code using OLE so that I can mailmerge an access 2000 table with Word. I am doing this by trying to create a new document when the Function runs.

The code I have so far doesn't include many fields as it is for testing purposes only untill I get it working.

The code is as follows:

Public Function AutomateWord()

Dim objWD As Word.Application ' Declare the variable

Set objWD = CreateObject("Word.Application") ' Set the variable to run new instance of word

objWD.Documents.Add

objWD.ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
objWD.ActiveDocument.MailMerge.EditMainDocument
objWD.ActiveDocument.MailMerge.OpenDataSource Name:="E:\Natdat\Code\automatewordtest\automatewordtest.mdb", ConfirmConversions:=False, ReadOnly:=False, Linktosource:=True, addtorecentfiles:=False, passworddocument:="", passwordtemplate:="", revert:=False, writepassworddocument:="", Format:=wdOpenFormatAuto, Connection:="TABLE stuDetails", SQLStatement:="SELECT * FROM [StuDetails]", SQLStatement1:=""

objWD.ActiveDocument.MailMerge.EditMainDocument
objWD.ActiveDocument.MailMerge.Fields.Add Range:=Selection.Range, Name:="STU_FNM1"
objWD.Selection.TypeText " "
objWD.ActiveDocument.MailMerge.Fields.Add Range:=Selection.Range, Name:="STU_SURN"
objWD.Selection.TypeParagraph
objWD.Selection.TypeParagraph
objWD.Selection.TypeText "It Works!!!!!!!" ' Add some text

objWD.ActiveDocument.SaveAs filename:="E:\Natdat\Code\MailmergewithOLE.doc"

objWD.Quit

Set objWD = Nothing

MsgBox "Word doc complete"

End Function

=====================================================

The problem is that when I run the code I get a runtime error 5825 (Object has been deleted), at the line that reads:

objWD.ActiveDocument.MailMerge.Fields.Add Range:=Selection.Range, Name:="STU_FNM1"

Can anyone help me....

Cheers in advance

Any help appreciated.

Neem

 
objWD.ActiveDocument.MailMerge.Fields.Add Range:=Selection.Range, Name:="STU_FNM1"
...
objWD.ActiveDocument.MailMerge.Fields.Add Range:=Selection.Range, Name:="STU_SURN"

In the above two lines of code 'Range:=Selection.Range' should be 'Range:=objWD.Selection.Range'

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top