Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Merging og a word doc

Posted on 2002-04-04
4
Medium Priority
?
185 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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Suggested Courses

972 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