Is this possible? Access to Word Again

jubb

Registered User.
Local time
Today, 20:54
Joined
Jul 27, 2005
Messages
50
I am insterting data into a word document with bookmarks and it works fine.

Can it be done in a loop. That is if I have a table (bookmarks) as follows.

BKMark, Text ' contains the name of a bookmark
BKMarkField, Text ' contains the name of the control the bookmark data is entered from

then loop through the records in bookmarks until EOF transferring the data to word?

Finding the bookmark stored in BKMark, works fine but at the moment I am just getting the name of the controls I have entered in BKMarkField rather than the information from the form.

I am now trying experimenting with pointers to achieve this.

Any help or a simple no it cant be done would be greatly appreciated.

Thanks

Jubb
 
Thanks for the pointer, but it is not what I was really after, I will try posting my code and see if that helps people see what I am trying to do.

Code:
Private Sub cmdWord_Click()
    On Error GoTo ErrHandler
    Dim objWord As Word.Application
    Dim bmrs As Recordset
    Dim bmrsSQL As String
    Dim docsavelocation As String

    bmrsSQL = "SELECT [bkmark], [BMfield] from Bookmarks;"
    Set bmrs = CurrentDb.OpenRecordset(bmrsSQL)


    Set objWord = CreateObject("Word.Application") 
    objWord.Visible = False 
    objWord.Documents.Add (Me.DocTemplate) 

'copy all single entries to word document
'bmrs![bkmark] is the name of the bookmark
'bmrs![BMField] control name information to be taken from on my form
    bmrs.MoveFirst
    Do Until bmrs.EOF
        If objWord.ActiveDocument.Bookmarks.Exists(bmrs![bkmark]) Then
            objWord.ActiveDocument.Bookmarks(bmrs![bkmark]).Select
            objWord.Selection.Text = bmrs![BMField]
        End If
        bmrs.MoveNext
    Loop
    bmrs.Close

    objWord.Visible = True
    
'code to save document
    docsavelocation = CurrentProject.Path & "\Sent Letters\" & Me.ContactId & ".DOC"
    objWord.ActiveDocument.SaveAs (docsavelocation)
    Set objWord = Nothing
    Set objtable = Nothing
    Exit Sub
    
ErrHandler:
    Set objWord = Nothing

End Sub

I know this can be done by typing all of the entries seperately but I am looking to streamline the process so the code can easily be copied from one project to another.

At this stage I am getting the control names transferreed to my word document which I know should be happening based on my code, want to change it to actually bring information from the control on my form named in [BMField]

Thanks

Jubb
 
Thanks to all who looked, I have worked it out.

A slightly different approach to my original idea.
I have altered my Boomarks Table as follows

BMname - text 'conatins the start of the bookmark name
BMcount - number ' contains the amount of times a data can be used
BMtext - string 'to hold the data to be transferred to the word doc

I then have a recordset that updated BMtext to equal the controls on my form.

I then have the following code to transfer the data to my word document.

Code:
rs.movefirst
do until rs.EOF
    for cntr = 1 to rs![BMcount]
        bkmarkname = rs![BMname] & cntr 'actual bookmark name used in word doc
        If objWord.ActiveDocument.Bookmarks.Exists(bkmarkname) Then
            objWord.ActiveDocument.Bookmarks(bkmarkname).Select
            objWord.Selection.Text = rs![BMtext]
        End If
    next
    rs.movenext
loop
rs.close

I can now simply copy the bookmarks table and the code accross any db and alter the data in the bookmarks table and my recordset that updates the BMtext to complete a merge to word bookmarks.

Jubb.
 

Users who are viewing this thread

Back
Top Bottom