Automation: Sending data from Access to Word

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.
 
B

BeWyched

Hi Dennis

Short answer is, I don't know why your code is failing.

However, I achieve the same thing using a slightly different technique which
works perfectly and should work for you:

Example is:

Set rng = oWord.ActiveDocument.Bookmarks("FacilityName").Range
rng.InsertBefore "Test text"

Good luck.

BW
 
B

BeWyched

P.S. with my example, you don't need the:
oWord.ActiveDocument.Bookmarks("FacilityName").Select
line and the code will work with bookmarks within tables.
 
D

Dennis

Thanks
I posted this on 3 sites and got great timely replies back on 2 of them. I
read the other reply first and it worked. The solution was to remove the text
fields I was using from the forms toolbar, Replace them with simple bookmarks
at the point of insertion, then using his code to insert the text. Here is
the reply. One change I had to make was to copy the access form data to a
local variable before inserting it.
Public Sub GoToWord()
Dim wdDoc As Word.Document, objWord As Word.Application
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set wdDoc = objWord.Documents.Open("C:"YourPath", , True, False)
On Error Resume Next
With wdDoc
..Bookmarks("YourBookmark").Select
' objWord.Selection.TypeText Forms!frm_Facility!FName
Dim InsertText as string
InsertText = Forms!frm_Facility!FName
objWord.Selection.TypeText InsertText
..Bookmarks("YourNextBookmark").Select
objWord.Selection.TypeText "Your Text"
End With
Error_SendDataToWord:
' Figure this out later
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top