From VB6, call a MS WORD Mail-Merge feature 
Author Message
 From VB6, call a MS WORD Mail-Merge feature

Hi,
I have a MS Word document that is a 'mail merge main
document'.  From within WORD, I can restore this document
by choosing:  Tools/Mail Merge/Main Document/Create

then choose:  "Restore to Normal Word Document"

This detaches this document from the data source, but most
importantly, keeps the existing data on the document.

Now for my question:  I would like to do this same thing
to this word doc, from a VB6 application.  DOes anyone
know if this is possible?  Can anyone refer me to any
documentation anywhere on this?  Do I use VBA?
Thank you.  Lino Vlacic,  New York



Mon, 11 Apr 2005 06:03:56 GMT  
 From VB6, call a MS WORD Mail-Merge feature
Hi Lino,

This can be done from within VB, try:
    oDoc.MailMerge.State = wdNormalDocument
where oDoc is the objectvariable that holds the Word document.

For more info on automating Word, see
http://www.mvps.org/word/FAQs/InterDev/index.html

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/


Quote:
> Hi,
> I have a MS Word document that is a 'mail merge main
> document'.  From within WORD, I can restore this document
> by choosing:  Tools/Mail Merge/Main Document/Create

> then choose:  "Restore to Normal Word Document"

> This detaches this document from the data source, but most
> importantly, keeps the existing data on the document.

> Now for my question:  I would like to do this same thing
> to this word doc, from a VB6 application.  DOes anyone
> know if this is possible?  Can anyone refer me to any
> documentation anywhere on this?  Do I use VBA?
> Thank you.  Lino Vlacic,  New York



Mon, 11 Apr 2005 07:20:08 GMT  
 From VB6, call a MS WORD Mail-Merge feature
Thank you Astrid.  But I need more help.
From VB, trying to access WORD, using Object Browser (F2),
I get tons of classes for the WORD object.  Which one do I
choose? Assuming I choose Application.  My VB code:

Dim MyWordAppObj As Word.Application
Set MyWordAppObj = CreateObject("Word.Application")

or do I do this:
Dim MyWordMergeObj As Word.MailMerge
or this:
Dim MyWordDocObj As Word.Document

Than after I dim the object and set it,  what next?
Each object has it's own bunch of properties and methods.
I tried:
MyWordDocObj.FullName
 "C:\DATA\Palletized\PalletSegmentSheets.doc"

VB did'nt like that.  I'm trying to tell VB which document
I am trying to work with.  How do we do this?

Can anyone give me some sample code please?  Thank you.

Quote:
>-----Original Message-----
>Hi Lino,

>This can be done from within VB, try:
>    oDoc.MailMerge.State = wdNormalDocument
>where oDoc is the objectvariable that holds the Word
document.

>For more info on automating Word, see
>http://www.mvps.org/word/FAQs/InterDev/index.html

>Hope this helps,
>regards,
>Astrid

>So that all can benefit from the discussion, please post

all follow-ups to the newsgroup.
Quote:
>Visit the MVP Word FAQ site at http://www.mvps.org/word/




- Show quoted text -

Quote:
>> Hi,
>> I have a MS Word document that is a 'mail merge main
>> document'.  From within WORD, I can restore this
document
>> by choosing:  Tools/Mail Merge/Main Document/Create

>> then choose:  "Restore to Normal Word Document"

>> This detaches this document from the data source, but
most
>> importantly, keeps the existing data on the document.

>> Now for my question:  I would like to do this same
thing
>> to this word doc, from a VB6 application.  DOes anyone
>> know if this is possible?  Can anyone refer me to any
>> documentation anywhere on this?  Do I use VBA?
>> Thank you.  Lino Vlacic,  New York
>.



Mon, 11 Apr 2005 22:47:20 GMT  
 From VB6, call a MS WORD Mail-Merge feature
Hi Lino,

Quote:
> From VB, trying to access WORD, using Object Browser (F2),
> I get tons of classes for the WORD object.  Which one do I
> choose?

It's not so much a question of which one; it's "which ones".
Ideally, you use an object variable for each Word object
you'll be manipulating (more than once) - it's more
efficient, and more accurate. For example:

Quote:
> Assuming I choose Application.  My VB code:
> Dim MyWordAppObj As Word.Application
> Set MyWordAppObj = CreateObject("Word.Application")

> or do I do this:
> Dim MyWordMergeObj As Word.MailMerge
> or this:
> Dim MyWordDocObj As Word.Document

Dim o_WdApp as Word.Application
dim o_doc as Word.Document
dim o_MM as Word.MailMerge

Set o_WdApp = New Word.Application
Set o_doc = WdApp.Documents.Open(szFileName)
Set o_MM = o_doc.MailMerge

Then, when you need to work with a property or method of the
object, you use that object. To execute the mail merge

    o_MM.Execute

To close the main merge document

    o_doc.Close

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister
http://www.mvps.org/word
http://go.compuserve.com/MSOfficeForum

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :-)



Tue, 12 Apr 2005 22:15:33 GMT  
 From VB6, call a MS WORD Mail-Merge feature
Thank you Cindy.   I figured it out, with alot of trial-
and error.  In the end this works for me:

Dim oWord As Word.Application
Dim oDoc As New Word.Document

Set oWord = CreateObject("Word.Application")
Set oDoc = Word.Documents.Open"C:\MainTemplate.doc")

oWord.ActiveDocument.MailMerge.Destination =
wdSendToNewDocument

oWord.ActiveDocument.MailMerge.Execute
oWord.ActiveDocument.SaveAs FileName:="C:\NewLetter.doc")

TIP:  I used WORD's macro feature.  While in WORD, I
recorded the actions that I wanted done, esentially
creating a MACRO.  Then I "edited" that MACRO, cut/pasted
the MACRO code, into VB.

Without that, I would still be scrathing my head and
cussing up a storm!

Thanks again Cindy.

Lino.

Quote:
>-----Original Message-----
>Hi Lino,

>> From VB, trying to access WORD, using Object Browser
(F2),
>> I get tons of classes for the WORD object.  Which one
do I
>> choose?

>It's not so much a question of which one; it's "which
ones".
>Ideally, you use an object variable for each Word object
>you'll be manipulating (more than once) - it's more
>efficient, and more accurate. For example:

>> Assuming I choose Application.  My VB code:
>> Dim MyWordAppObj As Word.Application
>> Set MyWordAppObj = CreateObject("Word.Application")

>> or do I do this:
>> Dim MyWordMergeObj As Word.MailMerge
>> or this:
>> Dim MyWordDocObj As Word.Document

>Dim o_WdApp as Word.Application
>dim o_doc as Word.Document
>dim o_MM as Word.MailMerge

>Set o_WdApp = New Word.Application
>Set o_doc = WdApp.Documents.Open(szFileName)
>Set o_MM = o_doc.MailMerge

>Then, when you need to work with a property or method of
the
>object, you use that object. To execute the mail merge

>    o_MM.Execute

>To close the main merge document

>    o_doc.Close

>Cindy Meister
>INTER-Solutions, Switzerland
>http://homepage.swissonline.ch/cindymeister
>http://www.mvps.org/word
>http://go.compuserve.com/MSOfficeForum

>This reply is posted in the Newsgroup; please post any
follow
>question or reply in the newsgroup and not by e-mail :-)

>.



Tue, 12 Apr 2005 23:45:28 GMT  
 From VB6, call a MS WORD Mail-Merge feature
Hi Lino,

I hope you're still around, because you don't want to do
what you show here :-) Do NOT use ActiveDocument
(especially not once you've declared oDoc). Substitute oDoc
for ActiveDocument EVERYWHERE in your code (except where
you may be dealing with a merge result document). Can you
see where you could get into trouble, and Word get
confused, if you use ActiveDocument all the time?

The macro recorder is fine to give you an idea of which
properties and methods you require. But you should always
apply what you know of working in an object-oriented
environment in order to get the most efficient and reliable
results :-)

Quote:
> Set oDoc = Word.Documents.Open"C:\MainTemplate.doc")

> oWord.ActiveDocument.MailMerge.Destination =
> wdSendToNewDocument

> oWord.ActiveDocument.MailMerge.Execute
> oWord.ActiveDocument.SaveAs FileName:="C:\NewLetter.doc")

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister
http://www.mvps.org/word
http://go.compuserve.com/MSOfficeForum

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:-)



Fri, 15 Apr 2005 18:12:33 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. mail-merge MS Word and MS Access via ASP

2. Word Mail Merge Err - Word could not merge...

3. Word Mail merge field TO MS Access???

4. VB to MS Word Mail merge functionallity

5. Mail Merge: MS-Word/Access

6. Mail merge to Ms Word

7. MS Word 97 Mail Merge with Oracle Datasource?

8. SLOW MS Word Mail Merge with VB5

9. Mail Merge in MS Word?

10. MS-Word mail merge

11. OLE Control and MS Word Mail Merge

12. MS Word 97 Mail Merge with Oracle Datasource?

 

 
Powered by phpBB® Forum Software