Solved

Merging og a word doc

Posted on 2002-04-04
4
180 Views
Last Modified: 2010-05-02
I have a word doc which is my main doc and then I have a csv file which is my datasorce. I want to know what object already build into VB, I can use to merge the two documents. The result must be files which will be mail away. I have Office 2000 wich also contains objects that I can use.  Can someone please give me sample code on how to do this.  Thank you
0
Comment
Question by:phantomh
[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
  • 2
4 Comments
 
LVL 1

Expert Comment

by:Madmarlin
ID: 6917729
Listening...
0
 
LVL 3

Accepted Solution

by:
daffyduck14mil earned 100 total points
ID: 6918568
Hi,

If I understand you correctly, you have a word document in some form, a letter perhaps, that you want to use as a base, and a csv file, addresses perhaps, with data.

You want your programma to automaticly merge the two files, and print the resulting documents.

If I assumed all this correctly, it is simple. But not build into VB. These things have to be set up in three steps. Namely, you need to make a word template, wich is a merge document, with all the text and fields in the right place. Then you need to attach that .csv to it. Next, you can save that template, and use the following code to automaticly open that file, and execute the merge.

Private sub cmdMerge_click()
  Dim oDocument as Word.Document
  set oDocument = g_oWord.Documents.Add("C:\temp\MainmergeDoc.dot")
  With oDocument.MailMerge
    .Destination = wdSendToNewDocument
    .Execute
  End With
end sub

Now, this was the easy method. There are some assumptions I made here, like you already have a MS-Word object instantiated and standing by and you have the template all top-notch in place.

This approach has some drawbacks, the most obvious one is the .CSV, wich always has to be on the same spot. Also, your data must have some format that will "fit" with the thing you are merging.

For the hardcoded path, you can solve it by using the MailMerge object of the document object. It has a method named OpenDataSource that takes a name etc. Also, this has some restrictions, in that fields you have defined must be present in the new file you assign as the merge data file.

I hope you can understand all this, at my company this approach has worked for over 3 years, and has been tried and tested under extreme stress (chistmas relation mailing to over 2300 entities, and it worked like a charm, in less then 2 minutes the entire mailing was present on their screens, yielding ohhh's and aaahhh's from even the CEO).

Should you have any questions, feel free to ask them. As further pointer, you can visit the MSDN library and read up on the MS-Word reference, especially the objects you have at your disposal. You can do very powerfull things with it, it just takes a little research.

Good luck, and success.

Grtz.©

D.
0
 
LVL 3

Expert Comment

by:daffyduck14mil
ID: 6918577
* scratches his head and points out a few more things *

- You need to reference the Microsoft Word objects in your project references, otherwise you need to use late binding in your program, which slows down things.

Grtz.©

D.
0
 

Author Comment

by:phantomh
ID: 6924895
Thanx, I took you code and mod it a bit and got what I was looking for. This is what I used.

Private Sub PrintLetter()
Dim oDocument As Word.Document
Set oDocument = New Word.Document

Set oDocument = Word.Documents.Add_("C:\WINDOWS\Desktop\Letter.doc")

With oDocument.MailMerge
  .Destination = wdSendToEmail
  .MailAsAttachment = True
  .MailSubject = "Test Letters"
  .SuppressBlankLines = True
  .Execute
End With

oDocument.Close wdDoNotSaveChanges
Set oDocument = Nothing

End Sub
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

738 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