Solved

Merging og a word doc

Posted on 2002-04-04
4
179 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
  • 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

713 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