Link to home
Start Free TrialLog in
Avatar of wayne_carlisle
wayne_carlisle

asked on

Using VB to add data to MSWord documents

I am attempting to add variable data to several different word documents that reside on a local machine. I wish to use these documents as templates and add data on the fly changing from one template to another as the data file requests. I have written code to select different documents and print data on them but it is painfully slow.

The number of templates I am using will be up to 10. Is there a way to load the documents up and select them as and when required? I also need a fix to append all the documents together as the code I have only sends down 1 sheet at a time not sheets (1 of 3) etc

Thanking you for your forth coming help
Wayne
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

hearing...
Well, you can load the different templates into different Word Document Objects, and then switch back and forth between the Word Document objects as the data file requests.

Dim wrdApp As Word.Application
Dim wrdDocTemplate1 As Word.Document
Dim wrdDocTemplate2 As Word.Document


    Set wrdApp = New Word.Application
    Set wrdDocTemplate1 = wrdApp.Documents.Open("C:\Code\Template1.doc")
    Set wrdDocTemplate2 = wrdApp.Documents.Open("C:\Code\Template2.doc")


Then, you can select and write to whichever document you choose, as often as you choose to.

I'm not sure about appending the documents together.  I'd guess that you'd need to create a new document, and perhaps select all of one document and paste it into the new document at a selection point at the end of the document, and continue doing that with each other document, and then print the new document.  

Find something very useful integrates vb with word doc where we can set the variables value in vb then pass to the word doc.

Do a search on freevbcode.com as where i found the code, but unfortunately i forget the exact url.

<listening..>
Avatar of wayne_carlisle
wayne_carlisle

ASKER

I have tried the above but it appears to be unworkable. On a laser printer it took 12 minutes to print 27 sheets?

I would like to import the data from the word document and somehow put it into the printers object but I'm not sure if this is going to be possible. These templates have bitmap signatures as well!
I guess I'm still a little confused about what you are doing.  If you could show any relevent code, that would be helpful.  Also, I'm not sure that it's the code that is slowing you down, I'm curious about your templates, maybe there is something there that is causing the problem... again, if we could see a snippit of code, then we might be able to test against other templates and we could see which area (code or templates) is bogging you down.  Then, we might be better able to help.

You can bring data into VB from Word, and then you can issue Printer.Print statements, but I doubt that you want to do that, as it's a painful process, difficult to format things properly, and will probably be as slow to print as anything that Word would do.
I have changed my tact on how I am going to achieve my objective. I have been working on saving word documents as .rtf files and importing the text into RichTextBoxes. Once these files are into these RichTextBoxes I can do what I want quite successfully. However I have another problem that I would be happy to give the points away for.

This is that I now need to create RichTextBoxes at run time depending on how many .rtf files exist. At the moment I have an array of 10 RichTextBoxes on the form which I fill when the form loads. This obviously restricts me to 10 templates.
Any ideas?
Thanks
Wayne
I have changed my tact on how I am going to achieve my objective. I have been working on saving word documents as .rtf files and importing the text into RichTextBoxes. Once these files are into these RichTextBoxes I can do what I want quite successfully. However I have another problem that I would be happy to give the points away for.

This is that I now need to create RichTextBoxes at run time depending on how many .rtf files exist. At the moment I have an array of 10 RichTextBoxes on the form which I fill when the form loads. This obviously restricts me to 10 templates.
Any ideas?
Thanks
Wayne
Hi wayne_carlisle,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Refund points and save as a 0-pt PAQ.

wayne_carlisle, Please DO NOT accept this comment as an answer.
EXPERTS: Post a comment if you are certain that an expert deserves credit.  Explain why.
==========
DanRollins -- EE database cleanup volunteer
ASKER CERTIFIED SOLUTION
Avatar of SpideyMod
SpideyMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial