G
Guest
I have an Access 2000 database that currently uses Automation to open word,
and then inserts text at preset bookmarks. It currently causes an illegal
operation error when used with Word 97 though.
Is there a way to mail merge from Access 2000 so that it will work for 2000
and 97 users?
The existing code (working with word 2000) is ugly and consists of a hugh
case statement switched on users choice of template with all of the bookmarks
for each template hard coded into each case block.
I want to change this so that users can add and configure their own
templates. This part I have sorted but when I try and use the same variable
declarations, objects, Set and create statements that have been used in the
code already working, I get error messages saying that the method is not
available.
Why would methods and properties of an object available in one part of the
code not be available to new objects of the same type in another part of the
code?
The code that follows is currently failing at : WordObj1.Documents.Add
template:="c:\Program Files\StandardLetters\" & strTemplate & ".dot",
newtemplate:=False
Error: Object doesn't support this property or method
Any Help would be much appreciated.
Anna
Private Sub MergeLetterButton_Click()
'Merges patient data to the Word letter template specified on the
PatientLetters Form
On Error GoTo Err_MergeLetterButton_Click
Dim rstBookmarks As Recordset 'The recordset for the PatientLetters form
contains the names of
'letter templates, their associated bookmark
names, the combobox
'name containing the merge text and the column
number of the
'merge text in the combobox.
Dim i As Integer 'For loop variable.
Dim strBookmark As String 'The name of the bookmark in Word Template.
Dim strWord As String 'The text to merge to Word Template.
Dim strQuery As String 'The name of the combobox on the LettersForm
holding text to merge
Dim intColumn As Integer 'Column number in the combobox where text is
located.
Dim strTemplate As String 'Name of the Word Template
Dim dbThisDatabase As Database
Dim WordObj1 As Word.Document
Set rstBookmarks = Forms!frm_PatientLetters.Recordset
strTemplate = Me!TemplateCombo.Column(1)
If rstBookmarks.RecordCount = 0 Then
MsgBox ("No Bookmarks defined for this template!")
Else
'Open a new Word document for merging
Set dbThisDatabase = CurrentDb()
DoCmd.Hourglass True
Set WordObj1 = GetObject("C:\Program
Files\Genesis\StandardLetters\AppointmentLetter.dot", "word.document")
If Err.Number <> 0 Then
Set WordObj1 = CreateObject("word.application")
End If
WordObj1.Application.Visible = True
WordObj1.Documents.Add template:="c:\Program Files\StandardLetters\" &
strTemplate & ".dot", newtemplate:=False
strBookmark = ""
strWord = ""
strQuery = ""
intColumn = 0
'Loop through the bookmarks retrieving the bookmark name, and its
corresponding
'merge string.
With rstBookmarks
.MoveFirst
For i = 1 To .RecordCount
strBookmark = !BookmarkName
intColumn = !ColumnNumber
If IsNull(!QueryName) Then
MsgBox ("Null Value")
Else
strQuery = !QueryName
Select Case strQuery
Case "PatientAddressCombo"
strWord = Me!PatientAddressCombo.Column(intColumn)
Case "PatientCombo"
strWord = Me!PatientCombo.Column(intColumn)
Case Else
MsgBox "Can't find the specified control"
End Select
'Insert the text into the open work document at the bookmark
MsgBox ("Bookmark is : " & strBookmark & " Value is : " &
strWord)
WordObj1.Goto what:=wdgotobookmark, Name:=strBookmark
'WordObj1.Selection.TypeText Text:= strWord
WordObj1.Selection.TypeText Text:="Type in this text"
.MoveNext
End If
Next i
End With
WordObj1.Activate
WordObj1.Selection.moveup wdline, 40
' Set the Word Object to nothing to free resources
Set WordObj1 = Nothing
DoCmd.Hourglass False
End If
Exit_MergeLetterButton_Click:
Exit Sub
Err_MergeLetterButton_Click:
MsgBox Err.Description
Resume Exit_MergeLetterButton_Click
End Sub
and then inserts text at preset bookmarks. It currently causes an illegal
operation error when used with Word 97 though.
Is there a way to mail merge from Access 2000 so that it will work for 2000
and 97 users?
The existing code (working with word 2000) is ugly and consists of a hugh
case statement switched on users choice of template with all of the bookmarks
for each template hard coded into each case block.
I want to change this so that users can add and configure their own
templates. This part I have sorted but when I try and use the same variable
declarations, objects, Set and create statements that have been used in the
code already working, I get error messages saying that the method is not
available.
Why would methods and properties of an object available in one part of the
code not be available to new objects of the same type in another part of the
code?
The code that follows is currently failing at : WordObj1.Documents.Add
template:="c:\Program Files\StandardLetters\" & strTemplate & ".dot",
newtemplate:=False
Error: Object doesn't support this property or method
Any Help would be much appreciated.
Anna
Private Sub MergeLetterButton_Click()
'Merges patient data to the Word letter template specified on the
PatientLetters Form
On Error GoTo Err_MergeLetterButton_Click
Dim rstBookmarks As Recordset 'The recordset for the PatientLetters form
contains the names of
'letter templates, their associated bookmark
names, the combobox
'name containing the merge text and the column
number of the
'merge text in the combobox.
Dim i As Integer 'For loop variable.
Dim strBookmark As String 'The name of the bookmark in Word Template.
Dim strWord As String 'The text to merge to Word Template.
Dim strQuery As String 'The name of the combobox on the LettersForm
holding text to merge
Dim intColumn As Integer 'Column number in the combobox where text is
located.
Dim strTemplate As String 'Name of the Word Template
Dim dbThisDatabase As Database
Dim WordObj1 As Word.Document
Set rstBookmarks = Forms!frm_PatientLetters.Recordset
strTemplate = Me!TemplateCombo.Column(1)
If rstBookmarks.RecordCount = 0 Then
MsgBox ("No Bookmarks defined for this template!")
Else
'Open a new Word document for merging
Set dbThisDatabase = CurrentDb()
DoCmd.Hourglass True
Set WordObj1 = GetObject("C:\Program
Files\Genesis\StandardLetters\AppointmentLetter.dot", "word.document")
If Err.Number <> 0 Then
Set WordObj1 = CreateObject("word.application")
End If
WordObj1.Application.Visible = True
WordObj1.Documents.Add template:="c:\Program Files\StandardLetters\" &
strTemplate & ".dot", newtemplate:=False
strBookmark = ""
strWord = ""
strQuery = ""
intColumn = 0
'Loop through the bookmarks retrieving the bookmark name, and its
corresponding
'merge string.
With rstBookmarks
.MoveFirst
For i = 1 To .RecordCount
strBookmark = !BookmarkName
intColumn = !ColumnNumber
If IsNull(!QueryName) Then
MsgBox ("Null Value")
Else
strQuery = !QueryName
Select Case strQuery
Case "PatientAddressCombo"
strWord = Me!PatientAddressCombo.Column(intColumn)
Case "PatientCombo"
strWord = Me!PatientCombo.Column(intColumn)
Case Else
MsgBox "Can't find the specified control"
End Select
'Insert the text into the open work document at the bookmark
MsgBox ("Bookmark is : " & strBookmark & " Value is : " &
strWord)
WordObj1.Goto what:=wdgotobookmark, Name:=strBookmark
'WordObj1.Selection.TypeText Text:= strWord
WordObj1.Selection.TypeText Text:="Type in this text"
.MoveNext
End If
Next i
End With
WordObj1.Activate
WordObj1.Selection.moveup wdline, 40
' Set the Word Object to nothing to free resources
Set WordObj1 = Nothing
DoCmd.Hourglass False
End If
Exit_MergeLetterButton_Click:
Exit Sub
Err_MergeLetterButton_Click:
MsgBox Err.Description
Resume Exit_MergeLetterButton_Click
End Sub