Database to MS Word

J

John

Hi
I am still having trouble with this function, I want to take data from a
text boxes and import it in to word, I can call a new document but have
failed to transfer the data, please help
Below is what i got so far. I have tried it different ways and am now
getting confused.
Please try to include an example.

Thanks


Option Explicit


Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Public Function Print_to_Word()

' Dim wrdSelection As Word.Selection
' Dim wrdMailMerge As Word.MailMerge
Dim wrdMergeFields As Word.MailMergeFields
Dim Bk As Word.Bookmark


' On Error GoTo Error_Handler

Screen.MousePointer = vbHourglass


' Create an instance of Word and make it visible
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True

' Add a new document
Set wrdDoc = wrdApp.Documents.Add
wrdDoc.Select
' Set wrdSelection = wrdApp.Selection
' Set wrdMailMerge = wrdDoc.MailMerge

For Each Bk In wrdDoc.Bookmarks
Select Case Bk.Name
Case "Salutation"
Bk.Range.Text = Clients.txtFields(0).Text
Case "WhatToSend"
Bk.Range.Text = "Xxxxxxxxxxxxxxxx"

End Select
Next


Screen.MousePointer = vbDefault



End Function
 
J

Jezebel

Unless you've set up your normal.dot as a letter template, the document you
create won't have "Salutation" or "WhatToSend" bookmarks defined in it, so
your code doesn't know where to put the information.

Create a new template that does have these bookmarks, then create your new
document using that template:

Set wrdDoc = wrdApp.Documents.Add(Template:="LetterTemplate.dot")
 
J

John

I don't want to use a template I just want to copy the text from each text
box in to word.
I will worry about where on the page it goes later.
 
J

Jezebel

Sure; but the problem you've got is that Word doesn't know where to put the
text because the bookmarks are undefined. If you don't want to use a
template, then you'll need to define the bookmarks in some other way; and
since a bookmark refers to a range of the document, you'll still have to put
the text *somewhere*, even if you're going to move to bookmark later. Cludgy
approach.

A better approach is to use CustomDocumentProperties or DocVariables. These
can be set by your code without reference to document layout; then later you
display the text within the document using DocProperty or DocVariable
fields.
 
A

Alex Ivanov

You don't need MailMerge object at all. Try this:
with doc.characters.last
.collapse wdcollapseend' substitute to numeric value if a reference to
Word is not set
.text=textbox1.text & vbcr
.collapse wdcollapseend
.text=textbox1.text & vbcr
' .....
end with

Creating a template with bookmarks is a good idea though. See an example at
http://www.aleksoft.net/samples/Access_Word.zip It is for Access, but the
same concept applies to VB.
 

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