Solved

Merging og a word doc

Posted on 2002-04-04
4
181 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Suggested Courses
Course of the Month9 days, 18 hours left to enroll

623 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