• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

VB - Word Merge

I need to be able to generate word documents based on results generated in my VB app.  What is the quickest and best way to do this?
1 Solution
you mean DOC files?? not sure what your question is.. please explain further what your needs are... thank you!

The best way is to use the word as an active com server.
Reference the msword8 object library in your project and then use the methods and properties that this exposes to programitically create a word .doc

Here follows a very simple routine that creates a geniune. doc file and then saves the contents "Hello world"

This is the easiest solution I think but remember it is actually loading and saving from word an so has a performance overhead.

Private Sub Command1_Click()   ' Declare object variables for Microsoft Word,
   ' application and document objects
   Dim wdApp As Word.Application
   Dim wdDoc  As Word.Document

   ' Assign object references to the variables. Use
   ' Add methods to create new document   ' objects.
   Set wdApp = New Word.Application
   Set wdDoc = wdApp.Documents.Add
   ' Assign some text    ' Microsoft Word cells.
   wdDoc.Sentences(1).Text = "Hello World"
  'Save the doc
   wdDoc.SaveAs "c:\Temp.Doc"
   ' Close the Doc
   ' Close Microsoft Word with the Quit method.
   ' Release the objects.
   Set wdApp = Nothing
   Set wdDoc = Nothing
   End Sub
jimmy3Author Commented:
Thanks for the response.  However I still need some more info if you don't mind.  I need to merge the VB manipulated data with an existing MSWord template.  
Value1 in slot1 .... How do I reference the slots? (blocks where you enter text into a word template)  Can I name them in Word?  My need for knowledge is on the MSWord side.

Thanks again.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now