For some time now I have automated various letters from Access. It has always worked well until this last few days. It still does everything it’s supposed to but for one thing. It now saves the original blank letter with the inserted text despite the instruction not to. It should send the information to a data file then open the blank letter, merge it then close it unsaved. Part of the code starts in Access but the instruction to insert and merge is on a button in Word.
The odd thing is this. If I open the blank letter in Word and run the insert and merge code (makealetter) it works perfectly but if I start it from Access then it saves the blank with its insert(b4 merge).
Any help out there? Here is the code.
Code from the Access DB
A button on main form sets it off (There is a choice of buttons and the results are the same for all
Sub Button_Click
DoaLetter(“myquery”,”mydata”,”myletter”)
end sub
Function DoaLetter(ByVal strQuery as string, strData as string, strFolderDoc as String)
strData = “C:My Directory” & strData & “.txt”
strFolderDoc = C:MyotherDirectory” & strFolderDoc & “.doc”
Kill strData
DoCmd.TransferText acExportMerge, , strQuery, strData, True
Set myApp = GetObject(strFolderDoc, “Word.Document”)
myApp.Application.Visible = True
Set myApp = Nothing
end function
This creates the basis for a letter in Word, which has a list of standard letter inserts on menu. The Word Code is as follows:
Sub MyLetter()
MakeLetter “myletter.doc”
End Sub
Sub MakeLetter(ByVal thisdoc As String)
On Error GoTo ErrorHandler
thisdoc = “C:My Directory” & thisdoc
With Selection
.EndKey unit:=wdStory
.InsertFile thisdoc
End With
With ActiveDocument
.MailMerge.Destination = wdSendToNewDocument
.MailMerge.Execute
.Close (WdSaveOptions.wdDoNotSaveChanges)
End With
End Sub