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