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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access VBA for Word control

Status
Not open for further replies.

Knoxster

Programmer
Sep 18, 2002
2
0
0
CA
Is there a way in Access VBA to open an already created document, and then simply add in bits of data at certain points througout?
I have a system and on one of the forms, there is a button that when pressed I would like to have it generate a completed proposal document based on the data displayed on the form (the current record). I already have a general version of the document typed up and formatted in Word, without the customer specifics. I would like to add some information right at the beginning of the document, and then add some other detailed information a few pages into the document at certain points.

Thanks
 
Hello,

I have done this to create merge letters and labels in word 2000 from VBA.

What I do is create a table using a sqlstr, then I open word and execute a marco that opens a document thats setup as a merge document and setup to use this table created in the above sql call. I also created a macro in word that is also called and does all the work for the user so that the document comes up and ready to edit or merge.

Process to create table, which I called tester:

' delete table "tester" so we can re-create it from scratch
' each time
str_sql = "DROP TABLE tester;"
CurrentDb.Execute str_sql

Next I build my sqlstr to create table

sqlstr = "SELECT * INTO tester FROM table1"

Dim dbs As Database
Set dbs = CurrentDb
dbs.Execute sqlstr
dbs.Close

' start win word and run macro mhyfcmerge
Dim stAppName As String
stAppName = "C:\Program Files\MicrosoftOffice\Office WINWORD.EXE /mhyfcmerge"
Call Shell(stAppName, 1)

Where /mhyfcmerge is a macro in word that opens the correct document and either stops before the merge or after the merge depending on your needs.

My word marco looks like this:

Sub hyfcmerge()
'
' hyfcmerge Macro
' Macro recorded 11/27/01 by David Schmidt
'
ChangeFileOpenDirectory "C:\HYFC database\"
Documents.Open FileName:="""merge document.doc""", ConfirmConversions:= _
False, ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto
End Sub

This macro only opens the document the the users needs to actually click on merge to perform the function, or you can add that to your word macro.

This works great for us we us it to create custom letters to one or many receipiants, we also use it to create labels from within word.

Hope this helps,

Dave
 
Another way is to make your Word document a Template (that allows you to have multiple instances at the same time). Where you need to transfer information from your database, create a field (right click on the tool bar and select the Forms toolbar), add a TextForm Field, right click on the shaded field that is inserted, select Properties and in the bookmark field of the Properties box, add a Bookmark name.

Word.ActiveDocument.FormFields.Item(BookmarkName).Result:= Database field

Since you are working from access the syntax may be a little different, but this function inserts the database field value into the bookmark.

I also do this check (since I have several templates and want to make sure I'm not using the wrong one!) to make sure the bookmark exists

Word.ActiveDocument.Bookmarks.Exists(BookmarkName) = True

Feel free to contact me if you have any questions about this process. Good luck!
Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
Dave do you have an email address I can write to you at to ask a few more questions on your method?

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top