Merge to MS Word

J

John

Hi

I wonder if someone could help me with the module below?
I use this moduel to merge data from my client database to MS Word to print
letters and stuff, however when it merges it leaves out all the menues and
icons so you only see the document alone ( the child form), can someone
please tell my what I am doing wrong?

Sorry for cross posting but was not sure where to direct this one.


Public MSWORD As Object

Public Function Print_to_Word()

Set MSWORD = CreateObject("word.basic")
With MSWORD
.FileNew Template:="", NewTemplate:=0
.Insert vbCrLf
.Insert vbCrLf
.Insert vbCrLf
.Insert vbCrLf
.Insert Clients.txtFields(0).Text & " "
.Insert Clients.txtFields(1).Text & " "
.Insert vbCrLf
.Insert Clients.txtFields(2).Text
.Insert vbCrLf
.Insert Clients.txtFields(3).Text
.Insert vbCrLf
.Insert Clients.txtFields(4).Text
.Insert vbCrLf
.Insert Clients.txtFields(5).Text
.Insert vbCrLf
.Insert Clients.txtFields(6).Text
.Insert vbCrLf
.Insert vbCrLf
.Insert Format$(Date, "DD MMMM YYYY")
.AppMaximize
End With
 
N

Norman Yuan

Why create "Word.Basic" object?, are you still using Word 95?

For Word 97 or later, you are supposed to do

Set wdApp=CreateObject("Word.Application").

Do not know if it is the case in Word95, never tried automation on that
antique software.

BTW(oh, it is only applies to Word97 or later), it is better to create
template with bookmarks inserted, so your automated letter will look much
better than your purely code generated one, and a lot easier to handle with
code, too. Like

Dear <bookmark1>,

This is to send you <bookmark2>. Please reply to <Bookmark3>.

Best regards

<Bookmark4>

Here you only need to
1. Have Word open the template
2. Save it as desirable document name
3. Loop through Bookmarks collection of the document to assign text data to
the bookmak
4. Save the document
5. Leave Word App on to allow user to do further editing, or close the
document and Word app.
 
N

Norman Yuan

Assume you use Word97 or later (as I said, I never used Word 95).

Dim wdApp As Word.Application
Dim doc As Word.Document
Dim bk As Word.Bookmark

Set wdApp=New Word.Application 'Launch Word, you can also use
CreateObject()
wdApp.Visible=True
Set doc=wdApp.Open("C:\MyFolder\MyWordTemplate.doc")

For Each bk In doc.Bookmarks
Select Case bk.Name
Case "Salutation"
bk.Range.Text="Mr. Xxxxx" 'or the data from your
app/database
Case "WhatToSend"
bk.Range.Text="Xxxxxxxxxxxxxxxx"
....
End Select
Next

doc.Close True 'Save and close document
wdApp.Quit 'Close Word App

So, the code to put dynamic data onto Word document is fairly simple. You
just need to pre-design a template letter, and insert bookmark to the places
where you want to put dynamic data. If you do not know how to add bookmark,
look at Word's Help.

HTH
 
J

John

Ok Norman that works fine to open a new document, but.....
what I want to do is take the information in a textbox and put it in that
document

e.g

Mr Smith
21 Road Street
etc...
 
M

Mark Alexander Bertenshaw

John said:
Ok Norman that works fine to open a new document, but.....
what I want to do is take the information in a textbox and put it in that
document

e.g

Mr Smith
21 Road Street
etc...

bk.Range.Text = txtMyAddress.Text
 

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