Tiffosi2007
Registered User.
- Local time
- Today, 16:57
- Joined
- Sep 13, 2007
- Messages
- 50
Hi,
This code is taken from Fornatians post way back in 2003, i am having some problems with it and dont know if it is the original code or me being dumb.
Basically the code runs a maketable query and then uses the results and runs a mail merge. The code runs fine through to the highlighted segment. Where i get a Run time 13 type mismatch.
Public Function CreateLetters()
' Open a letter in Word and insert text - used by menu command.
'You must put Word in the DAO Reference Library.
Dim Dbs As Database
Dim rstTEMPOwnersMerge As Recordset
Dim rstSourceTable As String
Dim appWord As Word.Application
Dim intPages As Integer, StrMessage, I As String
Dim Worddoc As String
'Make declarations of Source and MergeFiles
'Set the temp table created by query as rstSourcetable
rstSourceTable = "TempOwnersMerge"
'Set location of word document
Worddoc = "H:\Access\Plan & Directory DB\Reports\PlanDistLetter.doc"
'Set it so that it doesnt come up with warnings re deleting data and such
DoCmd.SetWarnings False
'Run query which adds records to tempownersmerge (query is addtable one)
DoCmd.OpenQuery ("qryPlanMailMerge"), acViewNormal, acReadOnly
'Turn warnings back on
DoCmd.SetWarnings True
' set db to current
Set Dbs = CurrentDb()
'set rstTEMPOwnersMerge recordset to rstSourceTable (which is in turn set to TempOwnersMerge)
Set rstTEMPOwnersMerge = Dbs.OpenRecordset(rstSourceTable)
' If no records are returned then exit
If rstTEMPOwnersMerge.RecordCount = 0 Then
MsgBox "There are no matching records for your criteria, please try again", 0, "No Records"
Exit Function
End If
'Switch to Microsoft Word so it won't go away when you finish.
On Error Resume Next
AppActivate "Microsoft Word"
'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
On Error GoTo 0
'Get an Application object so you can automate Word.
Set appWord = GetObject(, "Word.Application")
'Open a document based on the memo template, turn off the
'spell check
With appWord
.Documents.Add Worddoc
.ActiveDocument.ShowSpellingErrors = False
End With
MsgBox "The letter is now on screen and is ready for final editing and merging", 0, "Letter Ready..."
Set Dbs = Nothing
Set rstTEMPOwnersMerge = Nothing
Set appWord = Nothing
Set StrMessage = Nothing
End Function
I have yet to test the rest of the code but hopefully its is just the one bit i am stuck with. For further info, my query is called qryPlanMailMerge and the table the query adds to is TempOwnersMerge.
Thanks
This code is taken from Fornatians post way back in 2003, i am having some problems with it and dont know if it is the original code or me being dumb.
Basically the code runs a maketable query and then uses the results and runs a mail merge. The code runs fine through to the highlighted segment. Where i get a Run time 13 type mismatch.
Public Function CreateLetters()
' Open a letter in Word and insert text - used by menu command.
'You must put Word in the DAO Reference Library.
Dim Dbs As Database
Dim rstTEMPOwnersMerge As Recordset
Dim rstSourceTable As String
Dim appWord As Word.Application
Dim intPages As Integer, StrMessage, I As String
Dim Worddoc As String
'Make declarations of Source and MergeFiles
'Set the temp table created by query as rstSourcetable
rstSourceTable = "TempOwnersMerge"
'Set location of word document
Worddoc = "H:\Access\Plan & Directory DB\Reports\PlanDistLetter.doc"
'Set it so that it doesnt come up with warnings re deleting data and such
DoCmd.SetWarnings False
'Run query which adds records to tempownersmerge (query is addtable one)
DoCmd.OpenQuery ("qryPlanMailMerge"), acViewNormal, acReadOnly
'Turn warnings back on
DoCmd.SetWarnings True
' set db to current
Set Dbs = CurrentDb()
'set rstTEMPOwnersMerge recordset to rstSourceTable (which is in turn set to TempOwnersMerge)
Set rstTEMPOwnersMerge = Dbs.OpenRecordset(rstSourceTable)
' If no records are returned then exit
If rstTEMPOwnersMerge.RecordCount = 0 Then
MsgBox "There are no matching records for your criteria, please try again", 0, "No Records"
Exit Function
End If
'Switch to Microsoft Word so it won't go away when you finish.
On Error Resume Next
AppActivate "Microsoft Word"
'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
On Error GoTo 0
'Get an Application object so you can automate Word.
Set appWord = GetObject(, "Word.Application")
'Open a document based on the memo template, turn off the
'spell check
With appWord
.Documents.Add Worddoc
.ActiveDocument.ShowSpellingErrors = False
End With
MsgBox "The letter is now on screen and is ready for final editing and merging", 0, "Letter Ready..."
Set Dbs = Nothing
Set rstTEMPOwnersMerge = Nothing
Set appWord = Nothing
Set StrMessage = Nothing
End Function
I have yet to test the rest of the code but hopefully its is just the one bit i am stuck with. For further info, my query is called qryPlanMailMerge and the table the query adds to is TempOwnersMerge.
Thanks