DenMiller57
Registered User.
- Local time
- Today, 12:33
- Joined
- May 3, 2006
- Messages
- 13
I am attempting to send data from an Access form to a Word document using VBA code. The code creates a new 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 had the same results. The bookmarks were created using Word a "forms text field" from the forms toolbar, then 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.
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 believe 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.
Dennis
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 had the same results. The bookmarks were created using Word a "forms text field" from the forms toolbar, then 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.
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 believe 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.
Dennis