D
Dennis
I am using Office 2002. I am attempting to send data from an Access form to a
Word document using VBA code. The code creates a new word document from a
word template and has bookmarks where I need to place the data. This
technique is defined in the KB article
http://support.microsoft.com/kb/313193
It does not work. I get an error message stating the Range cannot be
deleted. I have spent many hours trying different methods but nothing is
working. Initially I thought the problem might be due to the fact the
bookmarks were within a table in the document. So I created another template
without a table, and the had the same results. The bookmarks were created
using the Word forms toolbarthen placing a forms text field in the document
and giving it the bookmark name. The document was then saved as a templete in
the root directory for testing. Below is the code which does not work. I have
removed all code that follows where the error is given to make this post
shorter. I have also tried creating an object for tha active document and
then referencing it as shown in the code from the article with the same
results and error messages.
Public Function SendOccupancy()
Dim oWord As Object
' Create a Word Application object where the data will be sent. If Word is
already open, use it, otherwise
' create new instance
On Error Resume Next
Set oWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set oWord = CreateObject("Word.Application")
End If
' Display Word and give user control of its lifetime
oWord.Visible = True
On Error GoTo 0 ' simplified for testing
oWord.Documents.Add "c:\AutomationTest.dot" ' opens new document based on
template
oWord.ActiveDocument.Bookmarks("FacilityName").Select ' no errors, so I know
it sees the bookmark
' everything up to this point works as expected
'oWord.ActiveDocument.Bookmarks("FacilityName").Range.Text =
Forms!frm_Facility!FName
oWord.ActiveDocument.Bookmarks("FacilityName").Range.Text = "Test text"
'crashes with either of the above 2 lines
End Function
Thanks for any help.
Word document using VBA code. The code creates a new word document from a
word template and has bookmarks where I need to place the data. This
technique is defined in the KB article
http://support.microsoft.com/kb/313193
It does not work. I get an error message stating the Range cannot be
deleted. I have spent many hours trying different methods but nothing is
working. Initially I thought the problem might be due to the fact the
bookmarks were within a table in the document. So I created another template
without a table, and the had the same results. The bookmarks were created
using the Word forms toolbarthen placing a forms text field in the document
and giving it the bookmark name. The document was then saved as a templete in
the root directory for testing. Below is the code which does not work. I have
removed all code that follows where the error is given to make this post
shorter. I have also tried creating an object for tha active document and
then referencing it as shown in the code from the article with the same
results and error messages.
Public Function SendOccupancy()
Dim oWord As Object
' Create a Word Application object where the data will be sent. If Word is
already open, use it, otherwise
' create new instance
On Error Resume Next
Set oWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set oWord = CreateObject("Word.Application")
End If
' Display Word and give user control of its lifetime
oWord.Visible = True
On Error GoTo 0 ' simplified for testing
oWord.Documents.Add "c:\AutomationTest.dot" ' opens new document based on
template
oWord.ActiveDocument.Bookmarks("FacilityName").Select ' no errors, so I know
it sees the bookmark
' everything up to this point works as expected
'oWord.ActiveDocument.Bookmarks("FacilityName").Range.Text =
Forms!frm_Facility!FName
oWord.ActiveDocument.Bookmarks("FacilityName").Range.Text = "Test text"
'crashes with either of the above 2 lines
End Function
Thanks for any help.