Solved

Merging og a word doc

Posted on 2002-04-04
4
175 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
Comment Utility
Listening...
0
 
LVL 3

Accepted Solution

by:
daffyduck14mil earned 100 total points
Comment Utility
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
Comment Utility
* 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now