Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Access VBA and values to fields in MS-Word

Posted on 1999-01-08
5
Medium Priority
?
478 Views
Last Modified: 2012-03-24
Tools used...Access97, VBA and MSWord.
Want to create a template in Word. In the text there must
be some fields defined (or something else) that can receive
values from Access97 (with VBA). (I create the template from
within MSWord).
I want to start word from access, open the template-file
in word and to fill out the fields in the template. All the
automation process must be done from within the access VBA
code.
I also wonder how to check if a certain defined field in
word actually exist?

Eks. Let say that this is the content in the template-file
test.dot:
--------- start of text in template----------
  To: *ToName*
  From: *FromName*
 
  This is just some title text.

  Dear *ToName* I'm happy to inform you that we've just
  transmitted *AmountDollar* to your account.
------end of text in template --------

In MS-Word:
Q1: What kind of field do I have to create in MS-Word?

In Access97 VBA code:
Q2: How do I start MS-Word and open the word-template?
Q2: How do I test if a specific defined field acually
    exist in the opened template-file from within Access
    VBA?
Q3: How do I put a value in a specific field in the word-
    file from Access VBA?
0
Comment
Question by:erot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 

Author Comment

by:erot
ID: 1973047
working example code must follow to get
 the points
0
 
LVL 10

Expert Comment

by:brewdog
ID: 1973048
While it can be done the way you want, my question is: why? Here's what I would do instead:

1. Create your Word document, but make it a document, not a template. Use the Mail Merge tools to make this a Mail Merge main document.
2. Create a temporary table in your Access database that has all the fields you need for your letter/document.
3. Use this temporary table as your data source for the Word document, inserting the fields where you need them.
4. From VB or Access, use an SQL string or action query to place the appropriate records into the temporary.
5. Use the Shell function to launch Word and open the mail merge document

Would that do what you want also, or are you set on using a template (which might really be a form with protected sections then) and strictly VB code?

brewdog
0
 

Author Comment

by:erot
ID: 1973049
Thanks for the try but cannot use it!
It has to work the way I told in the question.
Have to use VBA from within access, has to work against
defined fields/objects or whatever, and has to be able to check
in the VBA-code if the fields exists.

Erot / Norway
0
 
LVL 8

Accepted Solution

by:
Helicopter earned 600 total points
ID: 1973050
Try this as a start. I adapted it from Access Developer's Handbook, which I suggest you get a copy of if you can. It deals with your question quite extensively.

This will just do one bookmark....the rest is just a question of looping through.

Create a bookmark in a word template and run this code.

This should all be placed in a module.


Dim mobjWord As New Word.Application


Function getstarted()


 Dim strtemplate As String
 strtemplate = "c:\program files\microsoft office\templates\test.dot"
 opentemplate mobjword, strtemplate

End Function


Function opentemplate(objWord As Word.Application, strtemplate As String)
 
 Dim objWordDoc As Word.Document
 Dim strbookmarkname As String
 Dim strtext As String

 Set objWordDoc = objWord.Documents.Add(strtemplate)
 objWord.Visible = True
   
 strbookmarkname = "test" 'obviously replace this with your bookmark
 strtext = "stufftoputinthere"
   
fillBookmark objWordDoc, strbookmarkname, strtext 'call the routine to add the text
       
End Function


Function fillBookmark(objWordDoc As Word.Document, strBookmark As String, strtext As String)
   
    With objWordDoc.Bookmarks
        If .Exists(strBookmark) Then
            .Item(strBookmark).Range.Text = strtext
        End If
    End With
End Function




0
 

Author Comment

by:erot
ID: 1973051
Thanks a lot...works perfectly well.


0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question