Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Merge to Word HELP!!! 1

Status
Not open for further replies.

MAPetry

Technical User
Aug 2, 2000
43
0
0
US
I have an Access 97 file that I am trying to link to Word 97. So the user can hit a button in access form that will open Word and merge current record data. Then the user can save and print the merged document. I have approx 35 fields that will be linked.
I have downloaded numerous code examples and can't seem to make it work. Here is my code:

Private Sub cmdWord_Click()
On Error GoTo Err_cmdWord_Click

Dim oApp As Object
Dim trf As Object

Set oApp = CreateObject("Word.Application")
oApp.Visible = True
oApp.Documents.Open "i:\swap\mergetest"

Set trf = oApp.ActiveDocument

With trf
.Item("Subject").Value = (Me![Subject])
End With

Exit_cmdWord_Click:
Exit Sub

It opens Word and my document by then says "Object doesn't support this property or method" and closes Word back to Access. Any ideas?

(I tried the GetObject rather than CreateObject (Thread 701-52131) and it would not work maybe 97 to 2000 difference?

Also is this the best way to merge data from access to Word?
My end result document is approx 20 pages and has the same 35 fields throughout.
Since I have never done this before I was wondering is this slow? Am I prone to get error messages? Other comments?

 
It looks like your code is fine, except 2 items.

#1
Replace

Dim oApp As Object
Dim trf As Object

with...

Dim oApp as Word.Application
Dim trf as Document

I believe what you were attempting to do is what is called, "late binding". Correct me if I am wrong. I think it would probably help your program performance a little bit if you declare your Word objects specifically.

#2
I don't think the 'trf' object has an 'Item' property. This is probably the cause of your error. Perhaps you were trying to access a bookmark? In that case, the code should look something like this:

"trf.Bookmarks("Subject") = (Me![Subject])"


For awhile, while you are testing your product, you should probably comment out the error handling code. This makes it easier to debug at run-time. When the product is ready for release, enable your error handlers.

Let me know how it goes.

Gary
gwinn7


 
I have trouble when I say

Dim oApp as Word.Application

Error message: User defined type not found.
The remainder of code is same as above.
any suggestionss?

Mary Ann Petry
 
Is it not possible to do it by adding an Active X control. Everyone on here is a purist programmer using lots of code when Access can do most of this stuff automatically. I mean I thought that's why we are all using Access.

Look at your form in design view and go to tools, active-x controls and I am certain there is a button you can add which opens Word.

Otherwise, does not Access97 support linking with Word97. It does in 2000.

Try the former first. I will check it out again and try to help someone for once. If you haven't found a solution already by know.

Good luck.
And don't let the frustration wear you down - stops your brain working!
 
Access supports OLE Automation (97/2000). You can access these exposed objects by adding a reference to those interfaces that are supported.

You should make sure that the Microsoft Word Object Library has a check mark in your References.

As you type the "Dim Oapp as Word.Application", does the "Word" appear in the auto-complete list? Check your object browser and try to view the MS Word objects. If you can't then you have a reference problem. Add it.

Gary
gwinn7

 
Ok, I am over one hurdle. The Word object was not checked. Thanks for the help.

And to answer Gary's question you can create a command button that will run Word, Excel etc (see below). The problem with this instance was that I wanted to not only open word but open a specified file in word. Thus assigning the oAPP as an application and giving it a file name to GET not CREATE.

Private Sub Command46_Click()
Dim oApp As Object

Set oApp = CreateObject("Word.Application")
oApp.Visible = True

End Sub


However, Now my problem is linking the fields together. It now runs code to the "With" statement and doesn't know what to link.
I have tried setting up bookmarks as you suggested but that didn't work I get "method or data member not found". Someone out there had got to have done this before PLEASE HELP.

 
Here is some sample code that I used in a recent project...

If Not rc.BOF Then
For Each B In W.ActiveDocument.Bookmarks
B.Range.Text = Nz(rc.Fields(B.Name), "Unknown")
Next B
End If

"rc" is a DAO recordset. "B" is a Bookmark object. "W" is the Word application object. What happens here is that I used the For Each to loop through the bookmarks and place text where the bookmark name corresponds with the field name. The text should be inserted just fine.

To insert text into the bookmark, you need to use the "Range.Text" property of the Bookmark.

That code should help point you in the right direction and give you the solution you need for referencing bookmarks.

Good luck.

Gary
gwinn7
 
OK. I got it to merge using bookmarks. Thanks that works great.

Just one problem you can not have the same bookmark duplicate times in a document (can you)? I couldn't seem to get it to work. And most of my fields are duplicated many times throughout the 20 page document. Can I do it with bookmarks or do I need to use text fields?

 
That is an excellent question. Its an issue I am facing myself at this point with another project. Yes, you can only have one instance of a Bookmark in a document. Ouch! But, so far, I have a couple of possible resolutions.

1. We can iterate the identical bookmarks, like:
MyField1, Myfield2, Myfield3...etc. You could then, use a function to quickly truncate the numbers and compare the name with the field name. If identical, repeat the value in the doucment. This seems kinda clumsy, but workable.

2. Not use bookmarks at all. Instead, Tag the field names in the document. Then use a find and replace-like function to replace all instances of the Tagged field with the corresponding value. Like:

The amount of the sale was <<Field1>> and the money received was <<Field1>>.

Personally, I am leaning toward the 2nd option. I hope that helps.

Gary
gwinn7
 
I agree with you the second choice. Being unfamiliar with VB I am stumbling my way through. I have set up corresponding field boxes in word and need to link to access but am unsure of which command to use to link. I downloaded some sample code that says to use:

.item.(&quot;WORDFIELD&quot;).Value = Nz(Me![accessfield])

in place of the bookmark commands however I can not get it to work right.
 
What is the object/collection behind the &quot;.item&quot; ? Could you be more specific with your current issue with that code?

Someone else may have to step in here. Its getting late and I probably won't respond to the next response until tomorrow.

Gary
gwinn7
 
I GOT IT TO WORK.
I am Soooo happy this has been bothering me all weekend

What you do is:
1.) create your Word document tamplate
2.) Under File, Properties, Custom,
Add the field names you want to link to Access
For the type select a choice (text, number, etc.)
For the value I entered 1 thru whatever for my different field names.
4.) I then saved my document as &quot;MergeTest&quot; just to test it out.
5.) under the command button on my Access form I put the following code:
Private Sub cmdWord_Click()

Dim objWord As Word.Application
Dim prps As Object

REM: Tell word what document to open
Set objWord = CreateObject(&quot;word.application&quot;)
objWord.Visible = True
objWord.Documents.Open &quot;I:\swap\mergetest&quot;

REM: tell word that you are assigning the Custom Document Properties
Set prps = objWord.ActiveDocument.CustomDocumentProperties

REM: Link the fields together
With prps
.Item(&quot;TransID&quot;).Value = Nz(Me![TransID])
.Item(&quot;MailingAddress1&quot;).Value = Nz(Me![MailingAddress 1])
End With

Rem: Tell word to select the entire document and update the values for associated fields.
With objWord
.Visible = True
.Activate
.Selection.WholeStory
.Selection.fields.Update
.Selection.fields.ToggleShowCodes
.Selection.MoveDown Unit:=wdLine, Count:=1
End With

End Sub


It works like a champ now all I need to do is add errors and add code to ask the user what he/she wants to save the created document as so they do not overwrite the master.

hope this helps with your application

Mary Ann



 
Your solution is interesting. I am confused as to how you placed or referenced the custom properties in places on the document. I understand how you described to define them, but I don't know how you linked the custom properties in the presentation of the document. Could you explain?

Thanks,
Gary
gwinn7
 
In your Word document. Go to INSERT, FIELD, (all), DOCPROPERTY, Options tab at bottom. The custom fields that you added will be there and you can click &quot;add to field&quot;. Since I had so many custom fields I put a &quot;*&quot; in front of the fields I entered so they would pop up in the front.

MAP
 
See faq702-2379

Boss quote from an office meeting: We're going to continue to have these meetings until we figure out why no work is getting done ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top